This library is released under the terms of the LGPL. Developers interested in contributing should proceed to its SourceForge project page.
The current development version can be accessed directly via Subversion:
svn co https://svn.sourceforge.net/svnroot/facebook-ruby/trunk
require 'facebook' Facebook.setup(MY_API_KEY, MY_SECRET)
# 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')
# Instantiate a Facebook session using the authorization token returned. session = Facebook::get_session(auth_token)
# 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
# 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])
# 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
# 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