Class: Koala::Facebook::API

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

Overview

Ruby client library for the Facebook Platform. Copyright 2010 Facebook Adapted from the Python library by Alex Koppel, Rafi Jacoby, and the team at Context Optional

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

This client library is designed to support the Graph API and the official Facebook JavaScript SDK, which is the canonical way to implement Facebook authentication. Read more about the Graph API at developers.facebook.com/docs/api. You can download the Facebook JavaScript SDK at github.com/facebook/connect-js/.

Direct Known Subclasses

GraphAPI, GraphAndRestAPI, RealtimeUpdates, RestAPI

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ API

initialize with an access token



48
49
50
# File 'lib/koala.rb', line 48

def initialize(access_token = nil)
  @access_token = access_token
end

Instance Method Details

#api(path, args = {}, verb = "get", options = {}, &error_checking_block) ⇒ Object

Raises:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/koala.rb', line 52

def api(path, args = {}, verb = "get", options = {}, &error_checking_block)
  # Fetches the given path in the Graph API.
  args["access_token"] = @access_token || @app_access_token if @access_token || @app_access_token
  # make the request via the provided service
  result = Koala.make_request(path, args, verb, options)
  
  # Check for any 500 errors before parsing the body
  # since we're not guaranteed that the body is valid JSON
  # in the case of a server error
  raise APIError.new({"type" => "HTTP #{result.status.to_s}", "message" => "Response body: #{result.body}"}) if result.status >= 500
  
  # Parse the body as JSON and check for errors if provided a mechanism to do so 
  # Note: Facebook sometimes sends results like "true" and "false", which aren't strictly objects
  # and cause JSON.parse to fail -- so we account for that by wrapping the result in []
  body = response = JSON.parse("[#{result.body.to_s}]")[0]
  if error_checking_block
    yield(body)
  end
  
  # now return the desired information
  if options[:http_component]
    result.send(options[:http_component])
  else
    body
  end
end