Twitter Ruby Gem Experiments ..

Posted by dave
on April 02, 2007 @ 12:39 PM
Curious about the recent fuss about Twitter, I installed the Ruby Twitter gem over the weekend.


>gem install Hpricot
>gem install twitter

It has a dependency on the Ruby XML parsing API Hpricot. Its the first time I've played around with Hpricot, and the API is nice. (ActiveResource would do well to use it also as ActiveResources current XML to hash implementation is quite brittle).

In terms of configuration, the gem creates a /.twitter file in your home directory. On windows this is below %HOMEPATH%. Once you add your username and password to the config file, you can then post to twitter from a command shell anytime you like:


>twitter post "Post to my twitter account using ruby"

The only gotcha you have to look out for is that theres a bug in Hpricot when processing XML that contains <text> elements. I just commented out the offending line below:

<ruby>/lib/ruby/gems/1.8/gems/twitter-0.1.0/lib/twitter/status.rb


     def new_from_xml(xml)
        Status.new do |s|
          s.id         = (xml).at('id').innerHTML
          s.created_at = (xml).at('created_at').innerHTML
          #s.text       = (xml).at('text').innerHTML
          s.user       = User.new_from_xml(xml) if (xml).at('user')
        end
     end

I think you should be able to use the Twitter gem with the new direct messaging Twitter API to develop automated SMS response agents as described in TechCrunch's article "Web Services coming to twitter".

This would allow you to implement your own Twitter SMS agents (similar to the Google SMS API). I wonder will Google open up their SMS API on the backend?