Archive for the ‘ruby’ Category

inotify support for autotest

Friday, October 26th, 2007

autotest is a really great tool. But my laptop is too old and the current autotest method to detect modifications is a bit heavy for it.

Like Mark Scottishclimbs, I thought about using ruby-inotify to detect changes in source files.

Add to your .autotest file this piece of code to add inotify support. A small change in Autotest#wait_for_changes replaces the simple sleep by a waiting of an inotify event.

Update: ruby-inotify seems buggy. I’ve switched successfully the implementation to inotify. Just download INotify-0.3.0.rb into your load path (such /usr/local/lib/site_ruby/1.8). No native library is required.

RailsCasts on my Nokia 800

Wednesday, October 24th, 2007

I’ve just discovered RailsCasts. I have now more than 70 videos to watch … A good occasion to run the n800 mplayer :-)

A great software combinaison : DownloadHelper to download the movies in a directory, 770-encode.pl to re-encode them for the n800.

A small example with the last RailsCasts episode, here is a 30 second sample of RailsCasts - episode 76 - scope_out for Nokia 800.

I wrote a small wrapper maemo-video-encode-all to encode video files in batch.

A small scp to copy reencoded video files on your nokia SD and enjoy :-)

Optional locals to partials

Sunday, August 19th, 2007

One of the AudioBank partials supports several locals which can be optional. As using an unspecified local leads to a no such symbol error, working with optional local isn’t as easy as wanted ..

After reading several posts like Passing options to partials or An Explicit Locals Hash, I found a simple way for our partial. I define the symbol with its default value if needed :

< % limit ||= 7 %>

.. says like that .. it looks simple.

Polymorphic :through associations

Sunday, August 19th, 2007

AudioBank Subscription need a polymorphic has_many association with either User or Group. Nothing very difficult with Rails.

But we need a has_many :through to find users/groups associated to a Document via a subscription. Unfortunately or naturally, has_many :through are not supported on polymorphic associations. Read this interesting post of Josh Susser about polymorphic :through associations.

So … I replace the has_many association by a simple method which collects users/documents.

soap4r and keep-alive

Tuesday, July 24th, 2007

I’m working on a webservice for AudioBank to provide a command line tool which can create and upload documents.

Nothing very difficult for the server ride. With Rails ActionWebService, it requires only few lines of code.

For the client side, I use soap4r. The first steps were easy. Many guys describe how they invoke their Rails webservices with soar4r like Myles Eftos does.

Things become harder when my ruby client makes two webservices invocations .. The SOAP response of the second invocation is given empty by soar4r. After a long debug session in the ruby soap library, I found that the problem comes from HttpAccess2. Its socket says that it is eof when reading the second post response ?!

I’ve looked a way to disable the keep-alive support .. The uniq solution was to override HTTP.keep_alive_enabled at the beggining of my script with :

module HTTP
  class < < self
    def keep_alive_enabled?(version)
      false
    end
  end
end

The webservice invocations work fine with this workaround. The first release of the audiobank-client script is finished.

Creating email containing images in Ruby

Friday, April 13th, 2007

To display images embedded in html emails, you need cid URIs. A very complete article from Marek de Heus explains how creating email containing images in Rails. It describes the needed patch to TMail and ActionMailer to define the Content-Id of mail attachements.

After several unsuccessful tests, I patiently read the related RFC : Content-ID and Message-ID Uniform Resource Locators. The missing piece is the mail content type. It must be multipart/related.

This piece of code should create the right attachement with a given image file :

  # ...
  mail.set_content_type 'multipart', 'related'

  image_part = TMail::Mail.new
  image_part.content_transfer_encoding = "base64"
  image_part.body = TMail::Base64.folding_encode(IO.read(image))
  image_part.set_content_type 'image', 'jpeg', {"name" => image }
  image_part.set_content_disposition("inline", { "filename" => image } )
  image_part.set_content_id "< #{image}>"

  mail.parts.push(image_part)
  # ...


(requires the TMail patch linked above)

You can use this image in your html body via a simple

.