Nicolas Alpi, Web developer

A blog about productivity, startups and me.

Tutorial, How to Use Twitter_auth Gem in Development Mode

It was the big things weeks ago, but I didn’t had time to test it. Twitter_auth, allows you to user the twitter authentication on you application. And it’s really sweet!.
Let’s do a small implementation together.

h2. Step 1: Prepare your development box

Because Twitter doesn’t allow the callback parameter anymore, you’ll need to setup your workstation.
I recommend to install passenger in your development box, and use the hosts file to manage your different applications.

For that, just follow the official passenger doc. Easy peasy.

Then you’ll need to prepare your localhost. I like to work with [applicationName].local.

sudo nano /etc/hosts

Then add an extra line    twitterauth.local

Now create your rails app

rails -d mysql twitterauth

or take my source code my github twitterauth-test ;)

Finally add the site to apache

sudo nano /etc/apache2/sites-enabled/twitterauth

Then copy (and modify) these lines on it

<VirtualHost *:80>

ServerName twitterauth.local
DocumentRoot "/home/[REPLACE WITH YOU PATH]/twitterauth/public"
RailsEnv development
RailsAllowModRewrite off
<directory "/home/[REPLACE WITH YOU PATH]/twitterauth/public">
Order allow,deny
Allow from all

Restart apache

sudo /etc/init.d/apache2 restart

You now have your development box working with passenger. Next application you’ll develop, just add a new line in the /etc/hosts file, a new site-enabled file restart apache and you’re ready.
h2. Step 2: Install twitter_auth as a depency

Modify you environement.rb by adding

config.gem 'twitter-auth', :lib =&gt; 'twitter_auth'

Then sudo rake gems:install, and you’ll have the gem installed.
h2. Step 3: Prepare the application

script/generate twitter_auth

rake db:create
rake db:migrate
touch tmp/restart.txt

In order to work properly, your app (even on development mode) need to be registered on twitter. Visit Twitter apps and form with your informations.

Twitter doesn’t accept the callback param anyone, so you’ll need to provide http://twitterauth.local/oauth_callback as a callback in the configuration form.


When you save the informations, Twitter will give you 2 keys that need to be copied in config/twitter_auth.yml.


touch tmp/restart.txt

Your app is now ready!
h2. Step 4: Let’s do some code

We are going to create a twitter controller, with an index action

./script/generate controller Twitter index

Twitter_auth use the REST twitter api style, so to the loggued user friend time line copy and paste this in twitter_controller.rb, and returns a Json array. (visit the twitter api wiki for more informations)

Before calling functions that need you to be logged in, you’ll have to use the login_required method on before_filter.

This is going to be our root method so add in routes.rb

map.root :controller =&gt; &quot;twitter&quot;

This code goes into twitter_controller.rb

before_filter :login_required

#Retreive the friend timeline
def index
@tweets = current_user.twitter.get(‘/statuses/friends_timeline’) @tweets

Then in your views/twitter/index.html.rb

&lt;ul class=&quot;tweets&quot;&gt;

<% @tweets.each do |tweet| >
= link_to(tweet[‘user’][‘screen_name’] + ‘:’, ‘’ + tweet[‘user’][‘screen_name’], :target => ‘_blank’) + tweet[‘text’] ></li>
end -%></ul>

After a bit of CSS, if you open http://twitterauth.local you should have something like this