Class: FacebookApi::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_api/session.rb

Overview

FacebookApi::Session is your window to the Facebook REST API. Once you have a valid session, you can make API calls with #call and fql calls with #call_fql.

Example usage:

session = FacebookApi::Session.new(session_key, uid)

# Make REST API calls
response = session.call('Friends.get', :uid => '12345')
# Make calls with file attachments
response = session.call('Photos.upload', {:uid => '12345', :aid => '67890', :caption => 'your caption'}, File.new('/path/to/image.jpg))
# Make fql calls
response = session.call_fql('SELECT page_id FROM page_admin WHERE uid="12345"')

The response from an API #call will usually be a hash, converted from the JSON returned by Facebook. For some API calls however, Facebook returns literal values such as ‘true’, ‘false’ or an identifier (e.g. ‘12334234’). In these cases, #call returns either true, false or the literal respectively.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session_key, uid) ⇒ Session

Initialise a FacebookApi::Session with a valid session key and uid.



25
26
27
28
# File 'lib/facebook_api/session.rb', line 25

def initialize(session_key, uid)
  @session_key = session_key
  @uid = uid
end

Instance Attribute Details

#session_keyObject (readonly)

:nodoc:



22
23
24
# File 'lib/facebook_api/session.rb', line 22

def session_key
  @session_key
end

#uidObject (readonly)

:nodoc:



22
23
24
# File 'lib/facebook_api/session.rb', line 22

def uid
  @uid
end

Instance Method Details

#call(method, params = {}, file = nil) ⇒ Object

Makes a Facebook API REST call. If a file is specified, this will be included in the call, e.g. when calling Photos.upload. Example usage:

response = session.call('Friends.get', :uid => '12345')
response = session.call('Photos.upload', {:uid => '12345', :aid => '67890', :caption => 'your caption'}, File.new('/path/to/image.jpg))

Returns the response from Facebook as either a hash, boolean or literal, depending on what Facebook returns. Raises FacebookApi::Error if Facebook returns with an error.



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/facebook_api/session.rb', line 44

def call(method, params = {}, file = nil)
  params[:method] = method
  begin
    params = prepare_params(params)
    logger.debug "Sending request to facebook: #{params.inspect}"
    params[nil] = file if file
    response = RestClient.post(FacebookApi::REST_URL, params)
  rescue SocketError => e
    raise IOError.new("Cannot connect to facebook: #{e}")
  end
  logger.debug "Receiving response from facebook: \"#{response.body}\""
  parse_facebook_json response
end

#call_fql(query) ⇒ Object

Makes a Facebook API REST FQL call. Returns the response from Facebook as either a hash, boolean or literal, depending on what Facebook returns. Example:

response = session.call('SELECT page_id FROM page_admin WHERE uid="12345"')

Raises FacebookApi::Error if Facebook returns with an error.



65
66
67
# File 'lib/facebook_api/session.rb', line 65

def call_fql(query)
  call('Fql.query', :query => query, :uid => uid, :session_key => session_key)
end

#loggerObject

Alias for the FacebookApi.logger.



31
32
33
# File 'lib/facebook_api/session.rb', line 31

def logger
  FacebookApi.logger
end

#parse_facebook_json(response) ⇒ Object

Because Facebook does not always return valid JSON, we need to pre-parse it and catch the special cases. If the response is valid JSON, this returns the parsed response. Otherwise it catches “true”, “false” and string letirals, returning true, false or the string respectively. Raises Facebook::APIError if the response from Facebook is an error message.



86
87
88
89
90
91
92
93
94
95
# File 'lib/facebook_api/session.rb', line 86

def parse_facebook_json(response)
  body = response.body
  if looks_like_json? body
    data = JSON.parse body
    raise FacebookApi::Error.new(data['error_msg'], data['error_code']) if data.include?('error_msg')
  else
    data = parse_literal body
  end
  data
end

#prepare_params(params) ⇒ Object

Prepares passed in params ready for sending to Facebook with a REST call.



70
71
72
73
74
75
76
77
78
79
# File 'lib/facebook_api/session.rb', line 70

def prepare_params(params)
  s_params = {}
  params.each_pair {|k,v| s_params[k.to_s] = v }
  s_params['api_key'] = FacebookApi.api_key
  s_params['v'] = FacebookApi::API_VERSION
  s_params['call_id'] = Time.now.to_f.to_s
  s_params['format'] = 'JSON'
  s_params['sig'] = FacebookApi.calculate_signature(s_params)
  s_params
end