Download you twitter feed using Ruby on Rails with OAuth

Published 28 June 2013

Twitter provides an easy to use API allowing you to display your tweets on another web site or inside another application.

This article explains how to do this in a Ruby on Rails application using the new OAuth authentication mechanism.

It assumes you already have a Ruby on Rails application. I’m not going to go into tedious OAuth implementation details, just the results!

Register your application on the Twitter developer site

Create your application on Twitter, go to and click Register an app. Fill in the details giving it a name such as “My site tweet feed” and make it read only. The values you enter don’t really matter since no one else will be using your application, so long as a basic description and application website are set.

Copy your consumer key and consumer secret

After registration you should see your Twitter application settings page:

Twitter API settings page

Take note of the Consumer key and Consumer secret values, you will need to copy these into your application later.

Copy your access token

Click the My Access Token link on the right.

Twitter Client Token settings page

Note the Access Token and Access Token Secret, you will also need to copy these later.

Now you’re ready to modify your Ruby on Rails application to connect to the Twitter API.

Install the gem

There are a number of Ruby gems available for connecting to the API. I’ve had success with Grackle so that’s what we’ll use.

Install the Grackle gem:

gem install grackle

Create a table for storing tweets

Create a migration which will create a table for storing your tweets:

rails g migration CreateTweets

Edit that migration, creating the table with 2 fields:

class CreateTweets < ActiveRecord::Migration
  def self.up
    create_table :tweets do |t|
      t.string :content
      t.datetime :created

  def self.down
    drop_table :tweets

Create a model for the new table

Create a Tweet model, and add a method for downloading and storing the tweets:

require 'grackle'

class Tweet < ActiveRecord::Base

  MY_APPLICATION_NAME = "arctickiwi"
  """Connect to the Twitter API and pull down the latest tweets"""
  def self.get_latest
    tweets = client.statuses.user_timeline? :screen_name => MY_APPLICATION_NAME # hit the API
    tweets.each do |t|
      created = DateTime.parse(t.created_at)
      # create the tweet if it doesn't already exist
      unless Tweet.exists?(["created=?", created])
        Tweet.create({:content => t.text, :created => created })
  def self.client>{


Make sure you update your MY_APPLICATION_NAME, unless you want to get our tweets. Also enter your consumer key, consumer secret, token and token secret.

See if it works

You should now be able to download your latest tweets and store them in the database:

On the command line, run script/console


You should see a whole lot of text flash past the screen as the client hits the API, downloads the tweets and sticks them in the database.

If you get an error like this:

Grackle::TwitterError: get => 
401: {"request":"/1/statuses/user_timeline.json?screen_name=arctickiwi","error":"Invalid / expired Token"}

it means one of your keys or secrets is wrong.

If it worked and you’re still in the console, you can see all your tweets now in the database:


Display your tweets

As with any other model in Rails you can do whatever you want with them, like display them on a web page.

You will probably want links in your tweets to be real clickable links on your site, so here’s a helper method for to wrap links with anchor tags (in app/helpers/application_helper.rb)

module ApplicationHelper
  def display_content_with_links(tweet)
    tweet.content.gsub(/(http:\/\/[a-zA-Z0-9\/\.\+\-_:?&=]+)/) {|a| "<a href=\"#{a}\">#{a}</a>"}

Create a cron job

You will want your tweets downloaded automatically all the time. I call a rake task from cron job which runs every 10 minutes for this.

In lib/tasks create a file called download_tweets.rake

task :download_tweets => :environment do

and inside cron:
0,10,20,30,40,50 * * * * cd /path/to/my/app && RAILS_ENV=production /usr/bin/env rake download_tweets >> /path/to/my/app/log/cron.log 2>&1

I output to a log file so if something goes wrong one day I can see why.

FYI using the whenever gem is great for automatically creating cron jobs in conjunction with Capistrano

Good luck, and please leave a comment if you find this useful.