Twitter4R

Twitter4R is an open-source library for one of the most popular APIs available, the Twitter REST API. With this library you can create more easily your own twitter clients or mashups, using ruby. I’m going to give a little overview on how to install and use it.

Installation

To install Twitter4R, as a gem, simply run:

gem install twitter4r

After that, you’re ready to start coding.

Usage

Let’s start by importing the library with:

require 'twitter'

Now we can create our Twitter client

twitter = Twitter::Client.new( :login => 'sdsantos' , :password => 'xxxxxxx' )

You can also import you login data from an external YAML file. Just create a file named data.yml for example with the following format:

user:
    login: sdsantos
    password: xxxxxxx

and then create the client with it

twitter = Twitter::Client.from_config( 'data.yml' , 'user' )

All your and your friends data and messages are available now. For your data you use

twitter.my( :info )

and for your friends or followers list use :friends or :followers respectively.

To access the available timelines you can use

timeline = client.timeline_for( :me )

Options:
  • :me – your own timeline
  • :friend , :id => 12345 – a specific friend timeline (:id => friend’s id)
  • :friends – a timeline for all your friends
  • :public – the last 20 status from the public timeline

But the API isn’t read-only so, to update your personal status use

twitter.status(:post, 'KKTHXBYE from ruby.')

As a final example I’ll show you how to add me as a friend:

sergio = Twitter::User.find( 'sdsantos' , 'twitter' )
twitter.friend( :add , sergio )

Reference

You can find more information on this list of articles or just dive into the project’s documentation. You can also follow its releases on their twitter page.

I’m planning on using this library to create mashups for events or other purposes made, for example, with Ruby on Rails. This way you can easily add new friends to your event twitter page and show their messages, as well as report the things happening there. There are many ways to use this library so I would like to hear what are you planning with it.

If you enjoyed this post, consider subscribing to my RSS Feed RSS feed icon

2 Comments

Just to let you know before you get stuck in a rut integrating Twitter4R into a Rails application you should check out this Google Groups thread: http://groups.google.com/group/twitter4r-users/browse_thread/thread/3d159ec5b85d225d/ef3ee5369543945e#ef3ee5369543945e There is a workaround and a more permanent solution is on its way out tonight in the form of Twitter4R version 0.2.4 EBF. Hope that helps.
Thanks for the information.

The implementation I'm planning is only needed for an event next year. Probably, at that time, you've probably came up with a good solution. I'm already following Twitter4R's twitter profile, so when the solution comes up, I'll noticed it.

Leave a Comment

Follow this post comments with this RSS feed.