Facebook Ruby

A Ruby library for accessing Facebook's API.

This library is released under the terms of the LGPL. Developers interested in contributing should proceed to its SourceForge project page.

Authors

Getting Started

Register

Setup a Facebook developer account.

Install

The latest release can be downloaded from here. Unzip this file and put the library file somewhere in your Ruby load path.

The current development version can be accessed directly via Subversion:

svn co https://svn.sourceforge.net/svnroot/facebook-ruby/trunk

Setup

Place the following in your source before you use the Facebook API (typically near the start of your program).
require 'facebook'
Facebook.setup(MY_API_KEY, MY_SECRET)

Usage

General usage of the Facebook API is as follows:
  1. Authenticate a user via Facebook to instantiate a session for that user.
  2. Pass the returned session key to all subsequent calls.
  3. When the session expires, repeat.
The first step varies slightly for web and desktop applications.

Web Application

  1. Your web app must direct the user to Facebook to sign in. The following method returns a properly formed URL that you can use.
    # Returns a properly formed login url (e.g. includes your API key)
    Facebook.login_url
    
    # You can pass in a next parameter to force Facebook to redirect the user
    # back to the callback URL you registered + next.
    Facebook.login_url('/foobar')
    
  2. If the sign in is successful, Facebook will redirect the user to the callback URL you registered, and will pass a parameter named auth_token to your web app.
  3. Your web app must then call get_session with this auth_token to instantiate an authenticated Facebook session for this user. Note that you can only do this once; subseqent calls will throw an exception.
    # Instantiate a Facebook session using the authorization token returned.
    session = Facebook::get_session(auth_token)
    
  4. Finally, create a user object from the session. Keep this object around as you will use it to make all future calls to the Facebook API.
    # Get the currently logged in user from this session
    user = session.new_user()
    
    # Go nuts!
    puts "Welcome #{user.info.name}."
    puts "Your friends are:" unless user.friends.size.zero?
    user.friends.each do |friend|
      puts friend.info.name
    end
    

Desktop Application

While the Facebook API does support desktop applications, the current version of this library does not. Future versions will include this functionality.

Examples

User Info

# user.info() calls facebook.users.getInfo and caches the results in memory.
# By default it fetches all info fields.
puts user.info.name
puts user.info.about_me
puts user.info.pic.src_small          # Thumbnail of user's photo.
puts user.info.current_location.city  # Current city.
puts user.info.work_history.size      # Number of work experiences found.

# Types and names of all affiliations.
puts user.info.affiliations.map {|a| a.type_name + ": " + a.name}.join("\n")

# Force info() to make a network call, and only fetch the given fields.
user.info(:reload => true, :fields => [:name, :movies])

Friends

# Fetch the user's friends and print each of their names, and about blurbs.
# Note that this will make one network call to retrieve info for each friend!
user.friends.each do |friend|
  puts friend.info.name
  puts friend.info.about_me
end

# This is more efficient as it only makes two calls total, one to retrive
# the list of friends, and another to retrieve the given fields for all of them.
user.friends(:load_info => [:name, :about_me]).each do |friend|
  puts friend.info.name
  puts friend.info.about_me
end

Photos

# URL to a medium sized image of the current user's profile photo.
puts user.info(:load => :pic).pic.src_medium

# Number of photos that exist for the current user
puts user.photos_of_user.size

# Fetch all the user's albums and show information about these
# albums, and the photos they contain.
user.albums.each do |album|
    puts album.name
    puts album.cover_photo.src_large
    puts album.photos.size
    puts album.photos.map {|p| p.src_small}.join("\n")
end