Class: FacebookClient::RestApi

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

Instance Method Summary collapse

Constructor Details

#initialize(fb) ⇒ RestApi

Returns a new instance of RestApi.



10
11
12
# File 'lib/rest_api.rb', line 10

def initialize(fb)
  @fb = fb
end

Instance Method Details

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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rest_api.rb', line 14

def call(method, params={})
  params[:format]  = 'JSON'
  params[:v]       = '1.0'
  params[:method]  = 'facebook.' + method
  params[:call_id] = Time.now.to_f.to_s
  params[:timeout] ||= 8 # seconds
  params[:api_key] = @fb.api_key
  
  timeout = params.delete(:timeout)

  raw_string = params.inject([]) { |args, pair| args << pair.join('=') }.sort.join
  params[:sig] = Digest::MD5.hexdigest(raw_string + @fb.secret)
  
  response = connection.post do |request|
    request.body = params
  end
  response.body
end

#connectionObject



33
34
35
36
37
38
# File 'lib/rest_api.rb', line 33

def connection
  @connection ||= Faraday::Connection.new(:url => 'http://api.facebook.com/restserver.php') do |builder|
    builder.adapter :net_http
    builder.response :yajl
  end
end