How do RFacebook API calls work?

In your Rails controllers, you have an fbsession that you can use to make any call that the Facebook API defines in their documentation. For any API call, RFacebook populates the following fields for you:

Any other parameters that you see in the Facebook documentation must be specified using Ruby hash syntax, for example with users.getInfo:

fbsession.users_getInfo(:uids => [12345, 9876], :fields => ["first_name", "last_name"])

RFacebook then attempts to make the call to Facebook's servers, and throws an exception if Facebook returns an error for that call.

Once you have a response from the API call, you can access the data using Ruby dot syntax. This allows really simple access to the data without needing to parse the XML response directly.

response = fbsession.users_getInfo(:uids => [12345, 9876], :fields => ["first_name", "last_name"])

# the API call returns a set of user elements, let's get that list
users = response.user_list

# get the first name of user 9876
puts users[1].first_name

If you want to parse the XML data directly, you can use Hpricot directly by calling response.hpricot on the response.

Sample API Calls

To get a list of the users' friends full names:

# get an array of uids
friendUIDs = fbsession.friends_get.uid_list

# use those uids to get information about those users
friendNames = []
friendsInfo = fbsession.users_getInfo(:uids => friendUIDs, :fields => ["first_name", "last_name"])
friendsInfo.user_list.each do |userInfo|
	friendNames << userInfo.first_name + " " + userInfo.last_name

More samples coming soon...