Class: ReFacebook::API
- Inherits:
-
Object
- Object
- ReFacebook::API
- 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 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']
Instance Attribute Summary collapse
-
#session_key ⇒ Object
Returns the value of attribute session_key.
Instance Method Summary collapse
-
#batch(&block) ⇒ Object
Run a batch call to the API.
-
#initialize(api_key, secret) ⇒ API
constructor
Create a new API.
- #method_missing(method, *args) ⇒ Object
Constructor Details
#initialize(api_key, secret) ⇒ API
Create a new API. api_key and secret are the Facebook API and Secret keys.
83 84 85 86 87 88 89 90 91 |
# File 'lib/refacebook.rb', line 83 def initialize api_key, secret @api_key = api_key @secret = secret @session_key = nil # For batch operations. @in_batch = false @method_feed = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/refacebook.rb', line 114 def method_missing method, *args request = {} args[0].each do |k,v| request[k.to_s] = \ if v.kind_of?(Array) && @in_batch # If we are in a batch should return a escaped string # since we will be within an another array CGI.escape(v.to_json) elsif v.kind_of?(Array) && !@in_batch v.to_json elsif v.kind_of?(Time) v.to_f else v end 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) if @in_batch # If we are in a batch call just return the formatted request for method_feed @method_feed << request.collect { |k,v| "#{k}=#{v}" }.join('&') return end req = Net::HTTP.post_form(URI.parse(APIRestServer), request) ret = JSON.parse("[#{req.body}]")[0] if ret.kind_of?(Hash) and ret.has_key?('error_code') raise APIError.new(ret), ret['error_msg'] end return ret end |
Instance Attribute Details
#session_key ⇒ Object
Returns the value of attribute session_key.
79 80 81 |
# File 'lib/refacebook.rb', line 79 def session_key @session_key end |
Instance Method Details
#batch(&block) ⇒ Object
Run a batch call to the API. This doesn’t return a APIError if there are errors since some of the calls could have valid responses. Please catch the errors yourselves!
Example code:
ret = @api.batch do |b|
b.auth_createToken
b.admin_getAppProperties :properties => ['application_name','callback_url']
end
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/refacebook.rb', line 102 def batch &block @in_batch = true block.call(self) @in_batch = false ret = self.batch_run :call_id => Time.now, :method_feed => @method_feed.to_json @method_feed = [] ret end |