jump to navigation

OpenURI returns two different objects September 24, 2008

Posted by John Dewey in Code, Ruby.
1 comment so far

I am unfortunate in using ImageMagick to watermark images passed into a service. However, I noticed occasional nils when trying to read images returned by OpenURI.

Magick::Image.read(open(url).path).first

Interesting… OpenURI returns a StringIO object rather than Tempfile when data on the other end exceeds 10240 bytes.

StringMax = 10240
def <<(str)
  @io << str
  @size += str.length
  if StringIO === @io && StringMax < @size
    require 'tempfile'
    io = Tempfile.new('open-uri')
    io.binmode
    Meta.init io, @io if @io.respond_to? :meta
    io << @io.string
    @io = io
  end
end

There are several approaches to this problem, but I opted for the easy one. I simply added the following to my model to force a Tempfile.

##
# OpenURI::Buffer::StringMax = 10240
OpenURI::Buffer::StringMax = 0

Since this particular application is not under active development, and Rails _only_ uses OpenURI for ‘script/plugin’ and a couple generators — I didn’t loose too much sleep.

Advertisements

Railsolver – now with wildcard host resolution September 12, 2008

Posted by John Dewey in Monkey Patch, Plugins & Gems, Rails, Ruby.
add a comment

I was watching the Subdomains Railscast. Ryan listed a few ways to point subdomains at your development system, and I thought Railsolver should support wildcards.

Updates are on github, along with an updated README and bugs :). I also added some Railsolver props on the Railscast page.


This plugin will “hijack” Ruby host(s) resolver aka(resolv-replace.rb), allowing programmatic host file resolution (including wildcard host resolution).™

Railsolver – programmatic Rails host resolution September 9, 2008

Posted by John Dewey in Plugins & Gems, Rails, Ruby.
add a comment

Ever want to to programmatically control your applications name resolution? Railsolver takes a stab at doing just that.


This plugin will “hijack” Ruby host(s) resolver aka(resolv-replace.rb),
allowing programmatic host file entries.

Hosts can be added globally or overridden per environment inside a YAML configuration file. You can also embed ERB.

See the README for usage.

controlling memcached with Phusion’s daemon_controller September 6, 2008

Posted by John Dewey in Code, Deployment, Memcached, Plugins & Gems, Rails, Ruby.
add a comment

I am working on an application that utilizes memcached, and want to develop against a locally running instance. Previously, we pointed our development environment to a sandboxed memcached cluster, which can be slow depending on time of day, location, and network. I typically mock external resources, however, I like a living memcache.

This post describes a way to start memcached automatically with script/server, and is a continuation of the daemon_controller memcached configuration post.

As the Phusion guys pointed out. Pulling code — and not worrying about configuration is AWESOME! This is especially true in the development environment.

phusion-daemon-controller.png

Adding this to development.rb will automatically start and stop memcached via ‘script/console’ and ‘script/server’. SAs maintain the daemons in the other environments via SMF, nor am I interested in maintaining those environments (did that … done that).

daemon_controller memcached configuration September 4, 2008

Posted by John Dewey in Configuration, Deployment, Memcached, Plugins & Gems, Rails, Ruby.
2 comments

The Phusion guys created a sweeet library (daemon_controller) to control servers or daemons. I created a daemon_memcache class which controls memcached.

Memcached can be controlled programatically by:

DaemonMemcache.instance.start
DaemonMemcache.instance.running?
DaemonMemcache.instance.stop

Will show you how to utilize this class to start/stop Memcached via ‘script/server’ in a follow-up post.