1. Computing

5 Tips for Using Twitter With Ruby

Writing Twitter Clients in Ruby

By

As the fastest growing social-networking tool out there, there's no doubt you know someone who's on Twitter, if you're not on there yourself. After all, there's something incredibly appealing about this microblogging site that allows you to provide glimpses into your personal and professional lives 140-characters at a time.

There are a number of different interfaces to use to get to your Twitter account, read other people's updates and keep track of followers. That's because Twitter provides an easy Application Programming Interface (API) with which programmers can interface. This is one of the reasons why there are so many Twitter clients, they're just so easy to make! Making Twitter clients that both follow your friends and post tweets is very easy in Ruby.

  • Explore the Twitter API

    Before you can dive in and use Twitter from Ruby, you'll want to take a little time to explore the Twitter API manually. You can use the CURL command-line tool to access the Twitter API and examine the data Twitter returns.

  • Parse Twitter's XML

    The Twitter API returns data in XML format, so in order to make a program you'll need to be able to extract information from the XML. Parsing this data to extract the information you need is essential to making a Twitter program in Ruby.

  • Tweeting from Ruby

    Once you have a handle on the Twitter API and parsing the data returned by Twitter, it's time to start tweeting! Tweeting from Ruby requires a POST request, instead of a GET request. This is almost the same, the only difference is the Net::HTTP::Post class is used and there's an extra step to set the form data. Once you get the hang of it though, you'll have your tweeps up to date in no time.

  • Following People with Ruby

    Following your friends is just as easy as tweeting in Ruby. How do you tell if a status update is new or not? Should we save each one and compare them? That's neither efficient, nor also very nice to Twitter. Why request all the status updates, when what we really mean is all the status updates since our last request? When requesting data, you should save Twitter's bandwidth by requesting only the data you need.

  • Using the Twitter Gem

    Even though interfacing with Twitter yourself is quite easy, there are several gems that make it even easier. John Nunemaker has wrapped all the Twitter goodness of Ruby into a RubyGem. Using this gem, making API requests to Twitter is even easier than using the twitter method. Perhaps more importantly, the Twitter gem also parses the output for you, and gives you a very easy interface to the data. The twitter gem is absolutely dead simple to use, you'll be interfacing with Twitter in no time!

  1. About.com
  2. Computing
  3. Ruby
  4. Networking
  5. Twitter--5 Tips for Using Twitter With Ruby

©2014 About.com. All rights reserved.