Sunday, November 16, 2008

Accessing the FriendFeed API from Rails

I was hacking around with the FriendFeed API last night, and after quite a bit of frustration I got ActiveResource to download the public and user feeds. The main problem I ran into was that ActiveResource assumes a very specific URL format, and I've never actually encountered an API that actually conforms to it.

This tutorial about ActiveResource and the YouTube API tells you how to manipulate the URL format. I didn't go nearly as far as they did though. To get ActiveResource to do what I needed it to do, I only needed to override one method:

class FriendFeed::User < FriendFeed::FriendFeedApi

class << self
def element_path(id, prefix_options = {}, query_options = nil)
prefix_option, query_options = split_options(prefix_options) if query_options.nil?
"/api/feed/user/#{id}"
end
end

end


This class wraps the user feed api. It makes a request to http://friendfeed.com/api/feed/user/{id} and converts the response to a ruby object when you make a call to FriendFeed::User.find(id).

The parent class, FriendFeed::FriendFeedApi, inherits from ActiveResource::Base and takes care of setting some defaults used across each of the API categories. For example, it sets the site variable on ActiveResource::Base to "http://friendfeed.com" and the format variable to :json.

No comments: