Integrating Twitter into Rails

To view this screencast, add it to your cart and checkout. You can buy this screencast for any price, including FREE!

So, back when all I needed to do with many Twitter apps was to authenticate against Twitter’s OAuth and that was it, I created and screencasted the original Rack::OAuth gem.

After coding a few Twitter apps that needed more integration, and in response to feedback about the first gem, I’ve totally revamped Rack::OAuth so it now …

The usage is nearly the same! Here’s a partial Rails-based example …

 1 # in config/environment.rb
 2 config.middleware.use Rack::OAuth, :site => 'http://twitterr.com',
 3                                    :key => '123', :secret => '123'
 4 
 5 class ApplicationController < ActionController::Base
 6   include Rack::OAuth::Methods
 7 
 8   helper_methods :oauth_login_path
 9 
10   def print_users_recent_tweets
11     tweets = JSON.parse get_access_token.get('/statuses/user_timeline.json').body
12     tweets.each do |tweet|
13       puts tweet['user']['screen_name'] + " tweeted " + tweet['text']
14     end
15   end
16 
17   def tweet message
18     get_access_token.post '/statuses/update.json', :status => message
19   end
20 
21   def logged_in?
22     !! get_access_token
23   end
24 end

To watch the building of a Rails app that can login as a Twitter user, view the user’s timeline, and shows how to tweet as that user … check out the screencast!