Old dogs, new tricks

Rails, Ruby, Facebook and tests — my own itch scratched

with 8 comments

So I wanted to add a Facebook interface to Autopendium :: Stuff about old cars, the classic car community website that I’ve recently launched (that’s written using Ruby on Rails)

First thought, there must be a Ruby library for Facebook of some sort, probably even a Rails plugin — and there is. But almost from the get-go I had a few problems with it.

1) It was a gem. No problem with that as such, although I tend to be a bit more wary about installing them — they tend to make the code a bit more opaque (i.e. it’s not in my Textmate project drawer), they’re harder to tweak (plugins are dead easy in that respect, especially if you’re using piston), and they can add deployment issues when your in production and deployed across several servers (and yes, I know you can freeze the gems using gemsonrails)

2) There were no tests. Not one. And for dealing with something like Facebook, where authentication and user info was involved that seemed not such a good idea.

It also made it more difficult to understand how the code (and the Facebook API) worked (see 3, below). Perhaps I’d been spoilt by Technoweenie’s plugins that are pretty much solely documented by their tests, but this felt a bit unrailsy to me. So I decided to take a look at the code to see what was going on, perhaps even write a few tests, and I found that…

3) I didn’t understand the code. No, seriously. Felt like I was a 10-year-old reading Joyce. There’s a couple of pretty good posts out there walking you through using the library and setting up a Rails app to power a Facebook app, and by playing around a bit I could get some things working.

But it felt like trying to write a Rails app before I’d read David Black’s excellent Ruby for Rails. I could make things work, but didn’t understand why or how they worked, or why they sometimes didn’t, and that didn’t feel good. Plus, looking at the gem’s code made me more confused because it didn’t really feel like the Ruby I’d come to love and (to an extent) know.

So, I decided that as this must be my problem (nobody else seemed to have a big issue with it), so I should solve it myself. So I sent a couple of days reading the Facebook API, the dummy php app and library that Facebook provides, and doing some tests and some code.

The result is a (very) basic Facebook library that Works For Me. No, really, I mean basic. It’s a single file that I’ve put in my Rails lib folder and include in my facebook controller, and provides access to a Facebook session object.

I haven’t even made a plugin of it yet. Didn’t need to (though it shouldn’t take many minutes, and if there’s any interest in it, I will). But I have written a fair number of tests for it (in fact the tests came first, before the code), and I do feel a have a better understanding of Facebook’s Request/Response cycle, and how to tie that in to an app, especially an existing one like Autopendium. And my itch is now pretty much scratched.

Tomorrow I’ll do a brief walk through the hows and whys of the code, but if anyone wants the code in the meantime (and please do let me know via the comments if/when you find bugs, or generally want to sound off about the code), I’ve pasted it here and the test suite here.

Update: The walkthrough of the library is here


Written by ctagg

August 14, 2007 at 4:23 pm

Posted in facebook, rails, ruby, testing

8 Responses

Subscribe to comments with RSS.

  1. […] 15th, 2007 As I wrote in my previous post (Rails, Ruby, Facebook and tests — my own itch scratched), I needed a Facebook library for Autopendium :: Stuff about old cars, the classic car community […]

  2. hey Chris. We’ve spoken before regarding the RFacebook plugin and integration with Rails (and the need for unit tests).

    Seeing as you’ve gotten some experience writing up unit tests for the Facebook request/response cycle, it would be nice if you could contribute that to the RFacebook project. I’ve requested someone with Rails experience at the new homepage:


    There is a great community of people using the library and vetting the corner-cases, and the lastest version (0.9.1) is quite solid. The last major missing piece is, as you correctly observed, the lack of unit tests. Rather than rolling “yet another” Ruby and Rails library for Facebook, the community would really appreciate if you contributed to the RFacebook library directly. The project is open-source just for that reason 🙂 Shoot me an email if you’d like to join.

    Matt P

    August 21, 2007 at 5:41 pm

  3. Matt

    Thanks for dropping by. I’m really not trying to do “yet another” Ruby/Rails library for Facebook (in part, that’s why I haven’t wrapped it us as a plugin). I just needed (for my own piece of mind, if nothing else) to have a library with tests.

    I originally planned to write tests for RFacebook, and then after looking at the code realised I didn’t understand what was going on as far as the request/response cycle goes. It all seemed very complicated.

    The brief library I did (which I’m now using without any probs) came from the opposite direction you took with RFacebook (I’m not saying what I did was better, just that I felt more comfortable with it) — rather than rewriting the php code in Ruby, I wrote the tests, then wrote fresh Ruby to implement the behaviour the tests described.

    In the end it was a lot easier than writing the tests for an existing library, but if there’s anything in there (tests or otherwise) that’s useful to RFacebook do go ahead and use it — I’ve released it under the same license as Rails.


    August 21, 2007 at 6:12 pm

  4. Yea, no hard feelings (sorry if it came off that way at all). Just wanted to see if I could grab another good Rails developer…I’m really hoping to see the community center on a particular project.

    I think the complexity of the RFacebook session classes is perhaps due to the fact that it is actually intended to be able to be used outside of Rails, even in commandline apps (hence the FacebookDesktopSession class). It was not, however, ported directly from PHP – it was definitely written with Ruby in mind, although I was still fairly new to Ruby at the time 😉

    I’m planning on checking through your unit tests to see if they can be added on to RFacebook. Thanks for the permission.

    On a side note, if you’re developing a Canvas app in the future, you may want to give RFacebook another try (after we get unit testing, of course). There are some great features for Canvas apps that automatically rewrite urls for link_to, redirect_to, image_path, as well as automatically enable usage of session and flash.

    Matt P

    August 21, 2007 at 7:40 pm

  5. Yeah, the canvas stuff is tricky (my app is actually a canvas app), and developing for it doubly so (unless you use a SSH connection). I’m a bit averse to overwriting the standard url_for methods (hence the fb_redirect_to method), and overwriting the session starts getting tricky when your dealing with a decent-sized existing app.

    One of the issues for me was the fact that I needed to add Facebook functionality to an existing app, with an existing userbase (many of whom wouldn’t want to use Facebook). For that, a lightweight solution seemed more appropriate than one that allow for desktop usage, console etc. Especially as the code I’ve got is now (wasn’t to begin with though!) fairly well covered with tests 😉

    It actually surprised me how straightforward the library was to write, once I sat down to spec it out with tests — but then that’s the beauty of Ruby. In writing the app (now done, currently being tested), I’ve had to add virtually nothing more to it.

    Anyway, good luck with RFacebook, and good luck with the tests. I’ll try to keep up to date with it, and will do occasional posts here as and when I think of things worth posting — planning on doing one about how I solved the Observer/ActiveRecord/feed.publishActionOfUser/profile.getFBML issue.


    August 21, 2007 at 8:40 pm

  6. Matt P said: “The last major missing piece is, as you correctly observed, the lack of unit tests.”

    Interesting, I’d consider unit tests to be the *first* major missing piece. I agree with Chris’ approach here and that RFacebook has significant weaknesses.

    The ideal Ruby/Rails/Facebook solution still isn’t out there, so I don’t see a problem with a little friendly “competition” based on two different approaches. It will help the whole Ruby community.

    Keep on truckin’ test-first, Chris.

    Ryan Lowe

    August 21, 2007 at 9:36 pm

  7. Ryan – I agree with your sentiment, unit testing should have come first. RFacebook was originally a written when I was new to Ruby and Rails, so I hadn’t been “enlightened” so to speak.

    However, the friendly competition has definitely driven RFacebook to finally get its sorely-missing unit tests:


    Please feel free to check out the new plugin, and see what you think. I hope it puts RFacebook in better standing with the Rails community, and perhaps scratches that itch for other developers.

    Matt P

    August 28, 2007 at 3:37 am

  8. […] there’s no Ruby or Rails library for the Shopping API. So, time to scratch my own itch again. Enter ebay-shopping, a RubyonRails plugin for the ebay Shopping API. It’s a pretty […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: