Ruby: retrieve tweets that contain a certain #hashtag

by tofucoder

Interacting with Twitter is pretty easy in Ruby. First you have to install the twitter gem

gem install twitter

To use this gem you need to register your application with Twitter here to get your CONSUMER_KEY and CONSUMER_SECRET.

The connection to Twitter is established in the following manner

client = do |config|
  config.consumer_key = CONSUMER_KEY
  config.consumer_secret = CONSUMER_SECRET

Now it is very easy to do a search on all recent tweets. The following loop prints all tweets of the last 24 hours containing #hashtag

now =
catch :done do"#hashtag", :result_type=>"recent").each do |tweet|
    diff = now - tweet.created_at
    throw :done if diff > 24*60*60
    p tweet

Based on this I built a little webpage that shows how much is tweeted about german soccer clubs: