Every tech blog should have a post about a Twitter client. Here’s mine. To be fair, my client has only eight seven lines of code, so it rocks.
To update a Twitter status in a Ruby on Rails app, you might resort to using some kind of plugin or library. You link that library, maybe include a parser or two, an http lib, who knows what. After you know your Tweet has ballooned into 1200 lines of code and is a mess to manage.
It can be so much simpler, provided you’re inside a Ruby on Rails application.
RoR has a RESTful service builtin, called ActiveResource. Turns out it is quite capable of tweeting:
class Tweet < ActiveResource::Base
self.site = "http://twitter.com/"
self.element_name = 'status'
self.timeout = 5
self.user = USER
self.password = PASS
end
To tweet, simply execute this line:
Tweet.post(:update, :status => "7 LOC Twitter API client FTW") if Rails.env.production?
Did you notice the check for production environment? That's just how leet this client is. Also, you might want to put a rescue block after it. Twitter tends to timeout, which raises an exception in ActiveResource.