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!
First of all, you will need to install the twitter gem. This step is very simple:
|
1 |
gem install twitter |
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.

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

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:

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:
|
1 2 3 4 5 6 7 8 |
require 'twitter' client = Twitter::REST::Client.new do |config| config.consumer_key = "YOUR_CONSUMER_KEY" config.consumer_secret = "YOUR_CONSUMER_SECRET" config.access_token = "YOUR_ACCESS_TOKEN" config.access_token_secret = "YOUR_ACCESS_SECRET" end |
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.
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:
|
1 |
tweets = client.user_timeline('rubyinside', count: 20) |
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:

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.
|
1 |
tweets.each { |tweet| puts tweet.full_text } |
You can also try some of the other methods for fun 
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.
|
1 2 3 4 5 |
require 'yaml' # ... rest of the code here ... File.write('tweets.yml', YAML.dump(tweets)) |
Then you can load these tweets using the YAML.load_file method.
|
1 2 3 4 |
require 'yaml' require 'twitter' tweets = YAML.load_file('tweets.yml') |
The tweets will be ready to use in their original form, just like if you just requested them again. Isn’t that cool? 
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.
|
1 |
client.update("I'm having some fun with the Twitter gem!") |
So you could do something like this:
|
1 2 3 |
client.search('#ruby').take(3).each do |tweet| client.update("@#{tweet.user} Hey I love Ruby too, what are your favorite blogs? :)") end |
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.
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 
APIs are great because they let you interact with other applications, but they can be a bit intimidating if you never used one before.
In this post I will show you how to get started using your favorite APIs with Ruby.
To use an API you will need some form of HTTP client. I like RestClient because it’s easy to use.
Example: Random Chuck Norris joke
|
1 2 3 4 5 6 7 8 9 |
require 'rest-client' require 'json' url = 'http://api.icndb.com/jokes/random' response = RestClient.get(url) json = JSON.parse(response) puts json.fetch("value").fetch("joke") |
This code sends an HTTP ‘GET’ request to the server using RestClient.get(url), then it receives a json string as a response which needs to be parsed by JSON.parse(response). The parsed json is just a regular Ruby hash that we can use to get the data we want, in this case the joke.
The documentation for this API is available here.
Api responses can be pretty complex, and it can be hard to read the resulting json to find what you need. Fortunately, the json library has the jj method to pretty-print the output.
In the example above you can do this:
|
1 2 3 4 |
# rest of the code ... json = JSON.parse(response) jj json |
And this is what you get:
|
1 2 3 4 5 6 7 8 9 |
{ "type": "success", "value": { "id": 311, "joke": "When Chuck Norris sends in his taxes, he sends blank forms and includes only a picture of himself, crouched and ready to attack. Chuck Norris has not had to pay taxes, ever.", "categories": [ ] } } |
As you can see the json structure is more clear now. You can even go a bit further than this with the awesome_print gem, which will add some syntax coloring to the output and even better formatting than jj.
If you want to use an API from a popular site like Github or Reddit there are gems you can use to make your life a lot easier.
Example: Using the Octokit gem to get the top 10 Ruby projects on github.
|
1 2 3 4 5 6 7 8 9 10 11 |
require 'octokit' # Query the API repos = Octokit.search_repos "language:ruby", sort: "stars", order: "desc", per_page: 10 # Print the names p repos.items.map(&:name) # Get the next 10 repos = Octokit.search_repos "language:ruby", sort: "stars", order: "desc", per_page: 10, page: 2 |
The gem’s documentation should have all the possible API calls you can do with it. Also you can use pry’s ls command to help you explore the available options.
APIs are fun! Now open your editor, use some APIs and create something nice 