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

How to Follow Someone on Twitter With Ruby

Keeping Track of Your Friends


Following With Ruby

To "follow" someone on Ruby is to watch for their updates. We already know how to get the friends timeline on Twitter, so this should be a snap. All that's required is to wait for some time, and to send the request again to look for new status updates, right? Not quite.

How do you tell if a status update is new or not? Should we save each one and compare them? That's horribly inefficient. It's also not being 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.

Twitter helps you out here though. In each status update, there's an XML element for the date and time the update was made called id. Simply save this and pass it as the since_id parameter to the friends_timeline API query. Twitter will only return newly created updates, instead of all the updates. When iterating over the statuses element, be sure to save the id of the newest status update though, which is the first in the list.

The id of the last update seen is stored in the last_id variable. Since this is initially nil, when it's sent to the twitter method, it's checked before assigning it to the since_id parameter.

If there are no new updates, Twitter will return an empty statuses element. The length of the status query is checked before the script tries to iterate over the status elements. When the statuses are finally iterated over, they're iterated in reverse order. This is so the newest updates appear at the bottom of the output.

Finally, at the end of the loop, the script goes to sleep for 5 minutes. You may want to tweak this value to your preference, but setting it to anything less than a minute will generate too much useless traffic for Twitter. Remember, Twitter provides this API openly for all programmers, so use it responsibly.

 #!/usr/bin/env ruby
 require 'rubygems'
 require 'hpricot'
 require 'net/http'
 $username = 'aboutruby'
 $password = 'pass123'
 def twitter(command, opts={}, type=:get)
   # Open an HTTP connection to twitter.com
   twitter = Net::HTTP.start('twitter.com')
   # Depending on the request type, create either
   # an HTTP::Get or HTTP::Post object
   case type
   when :get
     # Append the options to the URL
     command << "?" + opts.map{|k,v| "#{k}=#{v}" }.join('&')
     req = Net::HTTP::Get.new(command)
   when :post
     # Set the form data with options
     req = Net::HTTP::Post.new(command)
   # Set up the authentication and
   # make the request
   req.basic_auth( $username, $password )
   res = twitter.request(req)
   # Raise an exception unless Twitter
   # returned an OK result
   unless res.is_a? Net::HTTPOK
     doc = Hpricot(res.body)
     raise "#{(doc/'request').inner_html}: #{(doc/'error').inner_html}"
   # Return the request body
   return Hpricot(res.body)
 # Keep track of the last update we saw
 last_id = nil
 while true do
   # Make an API query and parse the output
   doc = Hpricot(twitter(
     last_id.nil? ? {} : {'since_id' => last_id}
   # If zero statuses were returned, then
   # there are no new updates
   if (doc/'status').length > 0
     # Get the time of the first update
     last_id = (doc/'status id').first.inner_html
     # Print in reverse order so newest are
     # printed at the bottom of the list
     (doc/'status').reverse.each do|st|
       user = (st/'user name').inner_html
       text = (st/'text').inner_html
       puts "#{user} said #{text}"
   # Wait for 5 minutes
   sleep 300
  1. About.com
  2. Technology
  3. Ruby
  4. Networking
  5. Twitter--How to Follow Someone on Twitter With Ruby

©2014 About.com. All rights reserved.