1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

How to Twitter in Ruby on Rails

Twittering the Ruby Way

By

The Twitter API (Application Programmming Interface) in Ruby on Rails is about as simple as you can get. Twitter is a RESTful Rails application. API calls that query for information are GET requests, while information that changes, creates or deletes information are POST requests. The commands themselves are the URL your program accesses. Twitter will return status codes in the HTTP response code, and data in the format of your choosing (HTML, XML, RSS, etc).

CURL

To explore the Twitter API, the CURL tool will be used. This is a simple tool that allows you to make HTTP queries and examine their results from the command-line. It's available for just about every platform, so you shouldn't have any problems installing and using it.

The only command-line switches to CURL you'll have to worry about are the following.

  • -u username:password - Used to authenticate yourself before making the request.

  • -i - Include the HTTP headers in the response. You may want to examine the HTTP headers returned by Twitter, in particular the Response Code.

  • -w '\n%{response_code}\n' - Print the response code below the response. This is more handy than the -i switch if all you want is the response code, but not as easy to type.

  • -d var=data&var2=data - Send POST data for tweeting. If this switch is present, a POST request is made, otherwise a GET request is made.

  • Authentication

    Most Twitter API commands require authentication so Twitter knows who's making the request. A request to get all updates from people Alice is following will return different results than a request made by Bob. The Twitter API is also used without cookies, so Twitter doesn't remember who you are between requests. You have to pass your username and password in every request.

    This is done with CURL using the -u switch. If my username is aboutruby and my password is pass123, I would add -u aboutruby:pass123 to all my CURL commands.

    Your Friend's Timeline

    Perhaps the most common action on Twitter is to request your friend's timeline. This is what you see when you go to Twitter while you're logged in. Accessing this is as easy as accessing the URL http://twitter.com/statuses/friends_timeline.xml. Note the .xml on the end. To get this in RSS or HTML format, you can use .rss or .html instead.

    The following CURL command-line will execute this command. You should see the XML output scroll by. We'll examine that in more depth later when we get to parsing the XML output.

     $ curl -u aboutruby:pass123 http://twitter.com/statuses/friends_timeline.xml
     

    Tweeting

    The next most common action on Twitter is to tweet, or to post a new update. This CURL command-line is only slightly different. The -d switch is used to make the HTTP action be POST instead of GET and to provide some data to post. The URL for this action is http://twitter.com/statuses/update.xml and it expects a status value to be passed in the POST parameters.

     $ curl -u aboutruby:pass123 -d 'status=Posted from CURL' http://twitter.com/statuses/update.xml
     

    You Already Know Most of What You Need

    The rest of the Twitter commands follow the same pattern. For a full reference of the Twitter commands, see the REST API Documentation on the Twitter API Wiki.

    It's also worth noting that the Twitter API is open to all, free of charge. Please use the service responsibly by leaving ample time between your requests and using the options to prevent Twitter from sending data you've already received.

  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. Twitter--How to Twitter in Ruby on Rails

©2014 About.com. All rights reserved.