(09/25/10) Edit: A working version of this source code is uploaded here. Download it and follow the README to try it out!

While working on our church website, integrating with our church’s facebook fan page became a desired feature so our site maintenance team can just add an event on facebook and have it synced up to our website.

After learning about the facebook graph api, I was planning to write a library for ruby that sent out the queries and parsed the results to display on our site. However, I found the fb_graph gem written by Nov Matake and decided to try it out. The experience was extremely positive and Nov was very helpful and prompt in responding to my emails. Here’s how I integrated the facebook graph api into a ruby on rails app:

First things first, lets create a new rails application, and we’ll name it “fb_graph_app”.

Now that fb_graph is installed we can start making facebook graph API! Lets first create a controller and get modify the home page. We will first generate a simple controller called home with one action called index:

With that command the controller and view for “home” will be generated. Now we can map the root of the server to this action by changing the config/route.rb file:

1#config/routes.tb2FbGraphApp::Application.routes.draw do34#...truncated commented stuff...56# You can have the root of your site routed with "root"7# just remember to delete public/index.html.8root :to => "home#index"910# See how all your routes lay out with "rake routes"1112# This is a legacy wild controller route that's not recommended for RESTful applications.13# Note: This route will make all actions in every controller accessible via GET requests.14# match ':controller(/:action(/:id(.:format)))'15end

Now log into your facebook account and register for a new app here. Using the app_id, API key and application secret we are going to create a config file facebook.yml.

For this demo purpose we’re just going to pull the events from a facebook “Page”. Our church has the facebook page here, so i’ll grab the page_id from the link and insert in facebook.yml for the page_id. With that setup we can now go into our home controller to add in the calls to grab events. So lets add the following code in our home controller:

The above code first reads the facebook.yml file to obtain the facebook app information (line 5). It then uses the information to obtain an access token (line 7,8) so we can access events and more information [Thanks Nov for pointing this out, for the sake of this example you don’t need an access token because all information accessed from facebook is public. However, if you want to access information that’s not public (i.e. who rsvped for an event) then you need an access token]. Once an access token is obtained, we can use it access information from the page. We store the page information in an instance variable so we can display in the view later. We also use the connections provided by fb_graph to access the events that are posted in for that page (line 15). Once we get the events we sort it by the start time. Then we create 2 instance variables that store the upcoming events and the events that have already passed (line 18 and 21).

By pointing your browser to localhost:3000 you should now see a simple page with your facebook page name and the events listed from your facebook page in your ruby on rails app! This is a very simple example of using fb_graph to access the facebook graph api to get data from facebook. To learn more about the facebook graph api see here. Nov Matake also has a sample facebook app and documentation of the fb_graph api with some examples here. Note that is a simplified example, and there are several optimizations that could be done. I simply wanted a quick and easy tutorial to get people started with using the fb_graph gem. Hopefully this can help you integrate facebook into your next ruby on rails app!

Recent Changes

Image Gallery

Sitemap

About the Site

This site was created using webby, which is a dynamic generator for static websites. The purpose of this site was for me to experiment with website generators, ruby, lightweight markup languages (textile, Haml, Markdown etc). I wanted to generate a blog like website where it was easy to update contents, it had content and layout separation, and it’s only static html pages. Then i plan to integrate more dynamic features with javascript slowly. The source code of my experiment (this website) will be kept public and up to date on github. By doing so, i can also easily deploy this website (since it’s static and the pages are all in a source control repository) to any webhost. Feel free download the source to play with it and contact me if you have any questions or comments.