Friday, July 13, 2007

Twitter4R v0.2.0: User API

The User API provides an easy Ruby API that maps onto a less consistent Twitter REST API, which is good news for Twitter4R users:)

The following example code snippet shows how you would use the Twitter4R User API:


require('rubygems')
gem('twitter4r', '0.2.0')
require('twitter')

# I have a 'twitter.yml' file in subdir 'config' that I will load credential from.
# This feature of Twitter::Client is in the Extras API and you need to require
# twitter/console to access it.
require 'twitter/console'
config_file = File.join('config', 'twitter.yml')
twitter = Twitter::Client.from_config(config_file)

# Gets the Twitter::User object for user with screen name otherlogin.
user = twitter.user('otherlogin')
# Gets an Array of Twitter::User objects that represent the friends of user
# with screen name otherlogin.
friends = twitter.user('otherlogin', :friends)

# Gets the Twitter::User object for the user we used to authenticate above
# using the Twitter::Client.from_config method in the Extras API above.
me = twitter.my(:info)

# Gets the Array of Twitter::User objects that represent the followers of
# the user whose credentials were used to authenticate with the Twitter service.
myfollowers = twitter.my(:followers)
# The same can be done for friends, i.e.
myfriends = twitter.my(:friends)

# Alternative we can use the equivalent Model APIs like so....
myfollowers = me.followers
myfriends = me.friends


Hope you find this helpful. If you want to learn more about the User API you should consult the Twitter4R v0.2.0 RDoc.

2 comments:

Nando Vieira said...

Is there any method that returns me a list of followers that I'm not following?

In practice, is something like FOLLOWERS - FRIENDS.

S. Potter said...

I did a quick sanity check to see if the Twitter REST API exposes such a method, but I couldn't see that they did.

Unfortunately at present going through the official API, Twitter only provides getting followers and friends independently (which is 2 API calls, no matter which binding library you use like Twitter4R) and then take the difference yourself.

You could suggest it to the Twitter developers to include such a method in their API.

If there is a development I do not know about and Twitter has documented this API not on the usual wiki page, then please let me know.