Class: Koala::Facebook::API

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

Overview

Ruby client library for the Facebook Platform. Copyright 2010-2011 Alex Koppel Contributors: Alex Koppel, Chris Baclig, Rafi Jacoby, and the team at Context Optional github.com/arsduo/koala

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(access_token = nil) ⇒ API

initialize with an access token



36
37
38
# File 'lib/koala.rb', line 36

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

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



39
40
41
# File 'lib/koala.rb', line 39

def access_token
  @access_token
end

Instance Method Details

#api(path, args = {}, verb = "get", options = {}) {|body| ... } ⇒ Object

Yields:

  • (body)

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/koala.rb', line 41

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
  
  # add a leading /
  path = "/#{path}" unless path =~ /^\//

  # 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 run it through the error checker (if provided)
  # Note: Facebook sometimes sends results like "true" and "false", which aren't strictly objects
  # and cause MultiJson.decode to fail -- so we account for that by wrapping the result in []
  body = MultiJson.decode("[#{result.body.to_s}]")[0]
  yield body if error_checking_block

  # if we want a component other than the body (e.g. redirect header for images), return that
  options[:http_component] ? result.send(options[:http_component]) : body
end