Skip to content

Facebook API client

tigerlily edited this page Sep 14, 2010 · 3 revisions

The request

The quickly way to call a Facebook API method :


  require 'faceb'
  FaceB.call('api-key', 'secret-key', 'users.getInfo', :fields => 'name', :uids => 123456)

If you want to call many methods it can be a good way to use a persistant session :


  require 'faceb'
  FaceB.new('api-key', 'secret-key')
  FaceB.session.call('users.getInfo', :fields => 'name', :uids => 123456)
  FaceB.session.call('fql.query', :query => 'SELECT first_name FROM user WHERE uid=123456')

And you can also use the other syntax :


  require 'faceb'
  FaceB.new('api-key', 'secret-key')
  FaceB.session.users.getInfo(:fields => 'name', :uids => 123456)
  FaceB.session.fql.query(:query => 'SELECT first_name FROM user WHERE uid=123456')

You can reset the persistent session at any time by calling the reset! method :


  FaceB.session.reset!

The response

Each request returns an instance of FaceB::Api::Response. You can call data on this object to obtain the response object (Hash, Array, etc )

  
  response = FaceB.session.call('users.getInfo', :fields => 'name', :uids => 123456)
  => #<FaceB::Api::Response:0x16cfe70 @data=[{"name"=>"Mathieu Fosse", "uid"=>123456}]

  response.data
  => [{"name"=>"Mathieu Fosse", "uid"=>123456}]
  

Array Response with one Hash element

If the response is an array with one Hash element then you can directly call :

  
  response = FaceB.session.call('users.getInfo', :fields => 'name', :uids => 123456)
  => #<FaceB::Api::Response:0x16cfe70 @data=[{"name"=>"Mathieu Fosse", "uid"=>123456}]

  response.name
  => "Mathieu Fosse"

  response.uid
  => 123456
  

Hash Response

Hash Response works the same as Array Response with one Hash element.

Clone this wiki locally