Facebook Application Development with Ruby on Rails

by Chang Sau Sheong | May 2008 | Open Source

In this article by Chang Sau Sheong, we learn how to create a Job Board Mashup Application using Ruby on Rails and Facebook. This application allows the user to perform some functions and features of a job board. Through this article, we will be able to acquire candidates through Facebook and search for jobs.

Technologies needed for this article

RFacebook

RFacebook (http://rfacebook.rubyforge.org/index.html) is a Ruby interface to the Facebook APIs. There are two parts to RFacebook—the gem and the plug-in. The plug-in is a stub that calls RFacebook on the Rails library packaged in the gem. RFacebook on Rails library extends the default Rails controller, model, and view. RFacebook also provides a simple interface through an RFacebook session to call any Facebook API. RFacebook uses some meta-programming idioms in Ruby to call Facebook APIs.

Indeed

Indeed is a job search engine that allows users to search for jobs based on keywords and location. It includes job listings from major job boards and newspapers and even company career pages.

Acquiring candidates through Facebook

We will be creating a Facebook application and displaying it through Facebook. This application, when added into the list of a user's applications, allows the user to search for jobs using information in his or her Facebook profile. Facebook applications, though displayed within the Facebook interface, are actually hosted and processed somewhere else. To display it within Facebook, you need to host the application in a publicly available website and then register the application. We will go through these steps in creating the Job Board Facebook application.

Creating a Rails application

Next, create a Facebook application. To do this, you will need to first add a special application in your Facebook account—the Developer application. Go to http://www.facebook.com/developers and you will be asked to allow Developer to be installed in your Facebook account.

Facebook Application Development with Ruby on Rails

Add the Developer application and agree to everything in the permissions list.

You will not have any applications yet, so click on the create one link to create a new application. Next you will be asked for the name of the application you want to create. Enter a suitable name; in our case, enter 'Job Board' and you will be redirected to the Developer application main page, where you are shown your newly created application with its API key and secret.

Facebook Application Development with Ruby on Rails

You will need the API key and secret in a while.

Installing and configuring RFacebook

RFacebook consists of two components—the gem and the plug-in. The gem contains the libraries needed to communicate with Facebook while the plug-in enables your Rails application to integrate with Facebook. As mentioned earlier, the plug-in is basically a stub to the gem. The gem is installed like any other gem in Ruby:

$gem install rfacebook

To install the plug-in go to your RAILS_ROOT folder and type in:

$./script/plugin install svn://rubyforge.org/var/svn/rfacebook/trunk/rfacebook/plugins/rfacebook

Next, after the gem and plug-in is installed, run a setup rake script to create the configuration file in the RAILS_ROOT folder:

$rake facebook:setup

This creates a facebook.yml configuration file in RAILS_ROOT/config folder. The facebook.yml file contains three environments that mirror the Rails startup environments. Open it up to configure the necessary environment with the API key and secret that you were given when you created the application in the section above.

development:
key: YOUR_API_KEY_HERE
secret: YOUR_API_SECRET_HERE
canvas_path: /yourAppName/
callback_path: /path/to/your/callback/
tunnel:
username: yourLoginName
host: www.yourexternaldomain.com
port: 1234
local_port: 5678

For now, just fill in the API key and secret. In a later section when we configure the rest of the Facebook application, we will need to revisit this configuration.

Extracting the Facebook user profile

Next we want to extract the user's Facebook user profile and display it on the Facebook application. We do this to let the user confirm that this is the information he or she wants to send as search parameters.

To do this, create a controller named search_controller.rb in the RAILS_ROOT/app/controllers folder.

class SearchController < ApplicationController
before_filter :require_facebook_install
layout 'main'
def index
view
render :action => :view
end
def view
if fbsession.is_valid?
response = fbsession.users_getInfo(:uids =>
[fbsession.session_user_id], :fields =>
["current_location", "education_history",
"work_history"])
@work_history = response.work_history
@education_history = response.education_history
@current_location = response.current_location
end
end

Sign up for a Packt account to see the rest of this article

Now that you've read a few articles, you might want to consider signing up for a Packt account. It takes a matter of seconds, will give you access to all the articles on PacktPub.com, and once you've signed up you'll be returned here to carry on reading your article.

Furthermore, you'll gain access to nine free ebooks, and be offered a free trial of PacktLib, Packt's online library. Simply enter your details here, or log in to your existing account.

Log in

...or register

Post new comment

Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Sort A-Z