Module: MiniFB

Defined in:
lib/mini_fb.rb

Defined Under Namespace

Classes: BadJSONDataError, FaceBookError, FaceBookSecret, Photos, Session, User

Constant Summary collapse

FB_URL =
"http://api.facebook.com/restserver.php"
FB_API_VERSION =
"1.0"
RETRY_ATTEMPTS =
10
BAD_JSON_METHODS =
%w{ users.getLoggedInUser }
@@logging =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(api_key, secret, method, kwargs) ⇒ Object

The secret argument should be an instance of FacebookSecret to hide value from simple introspection.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/mini_fb.rb', line 140

def MiniFB.call( api_key, secret, method, kwargs )

    kwargs = Hashie::Mash.new kwargs

    puts 'kwargs=' + kwargs.inspect if @@logging

    if secret.is_a? String
        secret = FaceBookSecret.new(secret)
    end

    # Prepare arguments for call
    call_id = kwargs.fetch("call_id", true)
    if call_id == true then
        kwargs["call_id"] = Time.now.tv_sec.to_s
    else
        kwargs.delete("call_id")
    end

    custom_format = kwargs.include?("format") or kwargs.include?("callback")
    kwargs["format"] ||= "JSON"
    kwargs["v"] ||= FB_API_VERSION
    kwargs["api_key"]||= api_key
    kwargs["method"] ||= method

    # Hash with secret
    arg_string = String.new
    kwargs.sort.each { |kv| arg_string << kv[0] << "=" << kv[1].to_s }
    kwargs["sig"] = Digest::MD5.hexdigest( arg_string + secret.value.call )

    # Call website with POST request
    attempt = 0
    begin
      response = Net::HTTP.post_form( URI.parse(FB_URL), kwargs )
    rescue SocketError, Errno::ECONNRESET, EOFError => err
      if attempt < RETRY_ATTEMPTS
        attempt += 1
        retry
      else
        raise
      end
      # raise IOError.new( "Cannot connect to the facebook server: " + err )
    rescue
      raise
    end
    
    # Handle response
    
    body = response.body
    return body if custom_format
    unescaped_attempt = false

    begin
      data = JSON.parse body
      
      data.collect! {|datum| Hashie::Mash.new datum } if data.is_a?(Array) && data.length > 0 && data[0].is_a?(Hash)
      
      puts 'response=' + data.inspect if @@logging
      if data.include?( "error_msg" ) then
          raise FaceBookError.new( data["error_code"] || 1, data["error_msg"] )
      end
    rescue JSON::ParserError => ex
      return body if BAD_JSON_METHODS.include? kwargs["method"]
      return (body == 'true') if %w{true false}.include?(body) # Hack for Facebook boolean API calls with BAD JSON.
      
      unescaped_body = body[/\A"(.*)"\z/m,1]
      if unescaped_body && !unescaped_attempt
        unescaped_attempt = true
        body = unescaped_body.gsub('\\', '')
        retry
      end
      
      raise BadJSONDataError, "#{kwargs["method"]} returned \"#{body}\""
    end
    
    data.is_a?(Hash) ? Hashie::Mash.new(data) : data
end

.login_url(api_key, options = {}) ⇒ Object

Returns the login/add app url for your application.

options:

- :next => a relative next page to go to. relative to your facebook connect url or if :canvas is true, then relative to facebook app url
- :canvas => true/false - to say whether this is a canvas app or not


246
247
248
249
250
251
# File 'lib/mini_fb.rb', line 246

def self.(api_key, options={})
   = "http://api.facebook.com/login.php?api_key=#{api_key}"
   << "&next=#{defined?(Rack) ? Rack::Utils.escape(Rack::Utils.unescape(options[:next])) : CGI.escape(CGI.unescape(options[:next])) if options[:next]}" if options[:next]
   << "&canvas" if options[:canvas]
  
end

.verify_signature(secret, arguments) ⇒ Object

Returns true is signature is valid, false otherwise.



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/mini_fb.rb', line 218

def MiniFB.verify_signature( secret, arguments )
    signature = arguments.delete( "fb_sig" )
    return false if signature.nil?

    unsigned = Hash.new
    signed = Hash.new

    arguments.each do |k, v|
        if k =~ /^fb_sig_(.*)/ then
            signed[$1] = v
        else
            unsigned[k] = v
        end
    end

    arg_string = String.new
    signed.sort.each { |kv| arg_string << kv[0] << "=" << kv[1] }
    if Digest::MD5.hexdigest( arg_string + secret ) == signature
        return true
    end
    return false
end

Instance Method Details

#disable_loggingObject



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

def disable_logging
    @@logging = false
end

#enable_loggingObject



18
19
20
# File 'lib/mini_fb.rb', line 18

def enable_logging
    @@logging = true
end