My Life As A Blog

Ruby Tuesday #7 Part 1: Even Twittier

with 2 comments

Having managed to download the public timeline and output in a friendly format, the next task was to access some other methods of the Twitter API.  A couple of other timeline methods exist: friends_timeline and user_timeline.  Both can take the username as part of the URL.  How hard will it be to update the code to accommodate these calls?

One of the first things I noticed was that each of the URL’s for the Twitter API have the same base (, so it’d be good to split this out.  I updated the get_url method accordingly:

def get_url(base_address, path)
  url = URI.parse(base_address + path)
  req =
  res = Net::HTTP.start(, url.port) {|http| http.request(req)}

I tried to use the join method of URI, but it seemed to strip out the http:// part of the URL.  The next step was to create a new class called Client (in the Twitter file.)  It looks like this:

class Client

  def download_public_timeline

  def download_friends_timeline(username)

  def download_user_timeline(username)


  def download_timeline(path)
    get_url(BASE_ADDRESS, path)


For those of us learning Ruby, there’s a couple of interesting things to note here.  The first is the constant – it’s denoted by the use of capital letters.  It’s not really a constant – I could change the value if I liked, but Ruby convention is that if it begins with a capital letter then it’s a constant.  Those of us coming from a C# background may be surprised to learn that this constant could be used anywhere in the program – it is not limited to the scope of the class in which it is defined.  The constant can be qualified by the class name (e.g. Client::BASE_ADDRESS) for clarity.  The second point of interest is the use of the word private.  Any method defined after the use of private will be have private visibility (instances of the class and subclasses) until the end of the class or the words public or protected.  Methods are public by default.  Once again, private is not a keyword but a method specifies that the following method definitions are private.

All that’s left is to call these methods from the main body – which looks like this:

require 'Twitter'

$KCODE = "u"

translator =
client =

translator.xml_to_tweets(client.download_public_timeline).each do |tweet|
  puts "#{tweet.user.screen_name} says #{tweet.text}"

translator.xml_to_tweets(client.download_friends_timeline('put_your_username_here')).each do |tweet|
  puts "#{tweet.user.screen_name} says #{tweet.text}"

translator.xml_to_tweets(client.download_user_timeline('put_your_username_here')).each do |tweet|
  puts "#{tweet.user.screen_name} says #{tweet.text}"

The funny looking line near the top ($KCODE…) sets the text encoding to be UTF-8.   I set this because there is some character data in the timeline that would look funny otherwise.  (I was using Netbeans when I put this in and it had no effect – garbled output for some character data – whereas at a terminal calling the program I got the results I expected, I haven’t tried this yet with Sapphire in Steel.)

It was fairly straightforward to add this extra functionality.  I fell into the trap of using C# style names, which I corrected before writing this post, so I’m clearly still thinking in C#.   And even though the Client class is fairly concise, I wonder if it could be made more concise yet.  The next step is to add the ability to tweet directly from the Ruby program.


Written by remark

May 27, 2008 at 6:28 pm

2 Responses

Subscribe to comments with RSS.

  1. Mark,

    What tool do you use to format your code for your blog?
    Are you gonna add me to your blogroll?

    I’ll email soon
    Pete C


    May 30, 2008 at 12:35 am

  2. Pete,

    The code formatting is all done by the theme I use (I just mark the code regions with a <pre> tag.) There is a “paste from Visual Studio” add-in (there may be more than one) for Windows Live Writer that’s worth looking at. You’ll find a new link in the blogroll…



    May 30, 2008 at 8:03 am

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: