Class: ReFacebook::API

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

Overview

All API calls go through this class. To request a method just modify the call such that . become _. If you create a session update the session_key with the session value so that all the calls become authenticated.

Example calls: <pre><code>

# This is without a parameter
@api = API.new 'MY_API_KEY, 'MY_SECRET_KEY'
token = @api.auth_createToken
# This is with a parameter
app_properties = @api.admin_getAppProperties :properties => ['application_name','callback_url']

</pre></code>

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret) ⇒ API

Create a new API. api_key and secret are the Facebook API and Secret keys.



84
85
86
87
88
# File 'lib/refacebook.rb', line 84

def initialize api_key, secret
  @api_key = api_key
  @secret = secret
  @session_key = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/refacebook.rb', line 95

def method_missing method, *args
  request = {}

  args[0].each do |k,v| 
    request[k.to_s] = v.kind_of?(Array) ? v.to_json : v
  end if args[0]

  request['api_key'] = @api_key
  request['format'] = 'json' unless request['format']
  request['method'] = method.to_s.gsub(/_/, '.')
  request['session_key'] = @session_key if @session_key
  request['v'] = '1.0' unless request['v']

  request['sig'] = generate_sig(request.sort)

  req = Net::HTTP.post_form(URI.parse(APIRestServer), request)
  ret = JSON.parse("[#{req.body}]")[0]

  if ret.class == Hash && ret.has_key?('error_code')
    raise APIError.new(ret), ret['error_msg']
  end

  ret
end

Instance Attribute Details

#session_keyObject

Returns the value of attribute session_key.



80
81
82
# File 'lib/refacebook.rb', line 80

def session_key
  @session_key
end

Instance Method Details

#batch_run(*args) ⇒ Object

FIXME: This is not implemented yet. It is just a placeholder.



91
92
93
# File 'lib/refacebook.rb', line 91

def batch_run *args
  raise 
end