RubyGuides
Share this post!

Learn to Use the Twitter API with Ruby

Do you want to learn how to write a Twitter application using Ruby? Then you are in the right place!

In this post I will teach you, step-by-step, how to create a program that can interact with the Twitter API and do things like looking for certain keywords or send automated replies.

Let’s get started!

The Setup

First of all, you will need to install the twitter gem. This step is very simple:

After that you will need to head to https://apps.twitter.com/ and setup a new application. To do that you have to click on the ‘Create New App’ button on the right.

twitter app

Then fill in the form, it doesn’t really matter what data you enter for now. Here is what I did for this example:

twitter form

Don’t worry about the website field, you can just use http://example.com as a placeholder.

Then accept Twitter terms by checking ‘Yes, I agree’ and after that click on ‘Create your Twitter application’.

If everything went right you should see the following screen:

twitter gem

Now you will need to click on ‘Keys and Access Tokens’. This page will have the API keys you need to connect to the Twitter API, which was the purpose of this setup process.

The next step is to fill in this template with your details:

You will need to click on the ‘Generate access tokens’ button to get the last two values.

Once you have completed this setup process you are ready to start working with the API.

Working with Tweets

Now you have access to the whole Twitter API via the client object. As an example, I would like to do the following: download the last 20 tweets from the @rubyinside account and save them into a YAML file for later analysis.

To fetch the timeline for any Twitter user you can use the user_timeline method.

Example:

This method will return an array of Tweet objects that you can interact with, but how can you do that?

You could lookup the documentation, but what I find more fun is to just use pry. If you run you code inside pry you will be able to use the ls Twitter::Tweet command. This command will list all the methods for a specific object or class.

In this case:

pry ls

So now you can see there is a full_text method we can use, let’s do that so we can print the tweet contents.

You can also try some of the other methods for fun 🙂

Saving Your Tweets

So what do you do with all these tweets? For example, you could save them into a file for later analysis. An easy way to do this is to use the YAML format.

Then you can load these tweets using the YAML.load_file method.

The tweets will be ready to use in their original form, just like if you just requested them again. Isn’t that cool? 🙂

Sending Tweets

There are many other things you can do. For example, how about sending a message to users that mentioned a specific keyword?

To send a new tweet you can use the update method.

So you could do something like this:

Another option is to use the streaming API, which will give you ‘live’ events as they happen. You can find more information about that in the documentation.

Conclusion

The Twitter gem makes working with the Twitter API really easy after the initial setup. Now it’s your turn to give it a try and create something fun!

Don’t forget to share this post so I can keep writing more articles like this 🙂