Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Sample rails application using twitter oauth
Ruby
tree: 279e3361d0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
config
db
doc
lib
log
public
script
test
.gems
.gitignore
README.rdoc
Rakefile

README.rdoc

RAILS-TWITTER-OAUTH-SAMPLE

Prepared by: paul gallagher - tardate.com Repository: github.com/tardate/rails-twitter-oauth-sample/tree/master Demonstration: rails-twitter-oauth-sample.heroku.com

Purpose

Demonstrates the use of rails with the Twitter OAuth 1.0a API. Uses the oauth ruby gem.

Other References

  1. Twitter API documentation - apiwiki.twitter.com/

  2. OAuth gem github.com/pelle/oauth/tree/master

  3. Twitter OAuth gem (another REST API client library for Ruby - not used in this example) github.com/moomerman/twitter_oauth/tree/master

Required gems

These need to be installed in addition to all standard gems required by rails:

json (1.1.6 at time of writing) oauth (0.3.5 at time of writing)

NB: for heroku deployment, these are specified in the .gems file in the root of the project

STEP-BY-STEP (how the app was created)

  1. Install oauth gem

gem install oauth

  1. create the application shell

rails rails-twitter-oauth-sample cd rails-twitter-oauth-sample rake db:create

  1. create a member scaffold

ruby script/generate scaffold member twitter_id:integer screen_name:string token:string secret:string profile_image_url:string

Member model updated to use screen_name as the key: def to_param screen_name end

  1. Prepare the database

rake db:migrate

  1. Create the oauth support in ./lib

twitter_oauth.rb Implements TwitterOauth class, which is a wrapper around the oauth gem, providing specific support for twitter. As a design principle, the TwitterOauth class logs and re-raises any errors that occur; some custom error classes are defined to suit. It includes implementations for many of the twitter api methods (but not all at this point) oauth_system.rb A controller mixin module to provide twitter oauth support in an application. Uses the TwitterOauth class for oauth functionality. Works specifically with the Member ActiveRecord class to update/verify user details. It includes wrappers for many of the twitter api methods, basically to reroute errors into the flash hash.

  1. Modify MembersController to use OAuth

# include the oauth_system mixin include OauthSystem # specify oauth to be on all user-specific actions before_filter :oauth_login_required, :except => [ :callback, :signout, :index ]

  1. Specify routes

Map members resources Hook /members/callback method to module OauthSystem.callback map.resources :members, :collection => { :callback => :get }

Hook /signout method to module OauthSystem.signout: map.signout '/signout', :controller => 'members', :action => 'signout'

For the sample app, use MembersController.index as the landing page: map.root :controller => “members”

  1. Customise views and controller methods for some basic functionality

MembersController.index a basic landing page

MembersController.show main page for logged-in user

  1. Add rake task to demonstrate proxy-login

see lib/tasks/test.rake: demo_proxy_login task connects as the last member and exercises the API a bit to execute: rake demo_proxy_login

Something went wrong with that request. Please try again.