While there’s a few plugin options for the Rubyist looking to write a Facebook application, none of them quite fit our needs. I opted to write one for internal use at Slantwise. Why? A fundemental difference between this code and the publicly available solutions is that its Rails-centric.
If you’re not planning on writing your app in Rails, this isn’t the library for you. The benefits to the Rails programmer is that they now have a Facebook interface that’s borderline indisguishable from other Rails code—meaning its understandable, enjoyable, and doesn’t require hours of pouring through Facebook’s API documentation.
Ruby, where art thou?
This first sample shows some of the library’s low-level functionality, and its pretty similiar to the other solutions out there. It also demonstrates the inherent problems with simply wrapping API calls.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
# Retrieves photos from the first album that's found for the current user on Facebook
# Then publishes some of the photos as a news update in Facebook
...
#Retrieve the photos
recommended_album = Facebook::API::Albums.get(:uid => current_user.facebook_uid).first
@recommended_photos = Facebook::API::Photos.get(:aid => album[:aid])
# Assign all the necessary API params for publishing to Facebook.
# This is a more complex (and abbreviated) API call
# A typical update call will take at least a few lines of code
feed_body_template = render_to_string(:partial => 'facebook/_body_template')
feed_body_general = render_to_string(:partial => 'facebook/_body_general')
...
recommended_photos_feed = { :actor => current_user.facebook_uid,
:body_template => body_template,
:body_general => ... ,
... }
#Publish the feed to Facebook
Facebook::API::Feed.publish_templatized_action @recommended_photos_feed |
Direct API calls are great in some instances, but not in most. Writing something like the following could easily take someone new to the Facebook API hours to figure out. It also takes the magic out of working in Rails—increasing development time and developer frustration and ultimately resulting in a lesser product. Let’s bring that magic back and put a smile on our developer’s face while we’re at it:
Hello, old friend
1
2
3
4
5
|
recommended_album = current_user.facebook.albums.find_by_name 'Halloween'
@recommended_photos = recommended_album.photos
update_facebook 'recommended_photos.feed' |
The first line of code is a Facebook API call. It utilizes a basic ActiveRecord connection that’s adapted to run through the Facebook API, so the results behave exactly the same as any other ActiveRecord request.
The update_facebook method is a specialized version of render that looks for a template named ‘recommended_photos.feed’, fills it with data, and sends it to Facebook. In this case, the ’.feed’ extension maps to the Facebook API call Feed.publishTemplatizedActionOfUser. The rendering action is easily configurable to support any extension/API call combination.
Interested?
The plan is to release a development version of the plugin within the next week or two to iron out any glaring bugs, followed very shortly thereafter with a production version that you can freely use in your projects. I’ll announce the plugin release here when it’s available.
Get Involved
While I think I’m off to a good start, I’m looking for help. I’m on the look out for highly useful features. If you’re developing a Facebook application and have any problem areas, send me an example of some problem code and I’ll look into incorporating a solution into the plugin.