Skip to content

Commit

Permalink
initial working example
Browse files Browse the repository at this point in the history
  • Loading branch information
tardate committed Jun 29, 2009
1 parent d9f9645 commit 279e336
Show file tree
Hide file tree
Showing 28 changed files with 1,061 additions and 532 deletions.
2 changes: 2 additions & 0 deletions .gems
@@ -0,0 +1,2 @@
json
oauth
8 changes: 8 additions & 0 deletions .gitignore
@@ -0,0 +1,8 @@
RoRED.cfg
tmp
log
vendor/gems
db/*.sqlite3
data.dump
db/data.yml
config/database.yml
256 changes: 0 additions & 256 deletions README

This file was deleted.

120 changes: 120 additions & 0 deletions README.rdoc
@@ -0,0 +1,120 @@
= RAILS-TWITTER-OAUTH-SAMPLE

Prepared by: paul gallagher - http://tardate.com
Repository: http://github.com/tardate/rails-twitter-oauth-sample/tree/master
Demonstration: http://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 - http://apiwiki.twitter.com/
2. OAuth gem http://github.com/pelle/oauth/tree/master
3. Twitter OAuth gem (another REST API client library for Ruby - not used in this example) http://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

2. create the application shell

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

3. 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

4. Prepare the database

rake db:migrate

5. 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.


6. 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 ]

7. 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"


8. Customise views and controller methods for some basic functionality

MembersController.index
a basic landing page

MembersController.show
main page for logged-in user


9. 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








0 comments on commit 279e336

Please sign in to comment.