Class: Koala::Facebook::API
- Inherits:
-
Object
- Object
- Koala::Facebook::API
- 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
Direct Known Subclasses
GraphAPI, GraphAndRestAPI, RealtimeUpdates, RestAPI, TestUsers
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
Instance Method Summary collapse
- #api(path, args = {}, verb = "get", options = {}) {|body| ... } ⇒ Object
-
#initialize(access_token = nil) ⇒ API
constructor
initialize with an access token.
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_token ⇒ Object (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
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", = {}, &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, ) # 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 [:http_component] ? result.send([:http_component]) : body end |