Saturday, November 01, 2008

Sending Tweets from your Rails app

Use the Twitter4R gem. I tried using Twitter, but could never install it properly in the vendor directory - probably my own gem naivete, though, YMMV.

Like Chris Wanstrath says - "vendor everything". Unpack it into your vendor/gem directory. If the gem is already installed, cd to vendor/gem and use this command:

Add Chris' code to the Rails::Initializer.run block in your config/environment.rb:

Add this line of code at the bottom of the Rails::Initializer.run block in your config/environment.rb - it tells Rails that the "twitter" gem is found in "twitter4r.rb":

(Optional) Register your app with Twitter. Otherwise, tweets you send will display on the Twitter home page with a link to twitter4r. Once Twitter registers your app, you'll get an email with a "source token" (usually the name of your app in lowercase). Add an init file (i.e., set_twitter4r_source.rb) to config/initializers/ to initialize twitter4r with that source token. Don't bother grepping for Twitter4R and changing URLs randomly - it won't work (I tried):

Using the following code snippets, you should be able to authenticate and to send a tweet: