Searching Tweets with Ruby

Searching Tweets with Ruby

(Twitter circle: Wikimedia Commons/Gabriel Chi Hong Lee, Ruby logo: Wikimedia Commons/Yukihiro Matsumoto)

I’ve been working with Twitter a lot for the past couple of months. We use it for marketing, and it’s part of a software product we’re building.

In our software development project, almost all of my Twitter interactions start with a search. Luckily, most of these searches can be automated. Since I haven’t found many good examples of automated searches, I thought I would write one.

Twitter Developer Account

First, you need a Twitter Developer account. After signing up for an account, create an “application.” The application will give you access to the credentials needed to authenticate with Twitter so you can use the API.

Twitter API

Twitter’s API documentation is great. They have a service for almost every possible action on Twitter.

The API is broken up into RESTful and Streaming. If you are doing anything that involves posting to Twitter (sending a tweet, retweeting, or favoriting), use the RESTful API. While posting, ensure that your application has permissions to write. On the other hand, if you are retrieving tweets (by searching or watching your stream), use the Streaming API.

Pick your language of choice and start making calls to the Twitter API from your application. My example uses Ruby.

Twitter Gem

There is one Ruby interface to the Twitter API via the Twitter Ruby Gem; it’s fantastic! There isn’t much to say that isn’t already documented in the gem. So my recommendation is to read the documentation, and keep it open while writing code.

The gem supports the full Twitter API. The Streaming support is still “beta” but is production quality. In fact, we have been using the gem’s Streaming features for a while and have not found any show stopper bugs.

Searching Tweets

Now let’s get to the meat of searching for Tweets using specific keywords. On to the code!

@stream = Twitter::Streaming::Client.new do |config|
config.consumer_key = ENV[TWITTER_CONSUMER_KEY]
config.consumer_secret = ENV[TWITTER_CONSUMER_SECRET]
config.access_token = ENV[TWITTER_ACCESS_TOKEN]
config.access_token_secret = ENV[TWITTER_ACCESS_TOKEN_SECRET]
end
keywords = [sochi, olympics]
@stream.filter(:track => keywords.join(,)) do |object|
if object.is_a?(Twitter::Tweet)
puts #{object.id} #{object.user.screen_name}#{object.text}
puts ———————————————
end
end

As you can see on lines 2-5, I prefer to keep my keys in environment variables. So I have a script that sets the variables for me depending on my current project.

Line 8 shows that searching tweets is as easy as passing your keywords into the #filter method.

Wrapping Up

I hope this example of searching tweets using Ruby is helpful for your application. Let us know your tips for using the Twitter API in the comments below.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *