Makes a request to the appropriate Facebook API. @note You’ll rarely need to call this method directly.
@see Koala::Facebook::GraphAPIMethods#graph_call @see Koala::Facebook::RestAPIMethods#rest_call
@param path the server path for this request (leading / is prepended if not present) @param args arguments to be sent to Facebook @param verb the HTTP method to use @param options request-related options for Koala and Faraday.
See https://github.com/arsduo/koala/wiki/HTTP-Services for additional options.
@option options [Symbol] :http_component which part of the response (headers, body, or status) to return @option options [Symbol] :format which request format to use. Currently, :json is supported @option options [Symbol] :preserve_form_arguments preserve arrays in arguments, which are
expected by certain FB APIs (see the ads API in particular, https://developers.facebook.com/docs/marketing-api/adgroup/v2.4)
@option options [Boolean] :beta use Facebook’s beta tier @option options [Boolean] :use_ssl force SSL for this request, even if it’s tokenless.
(All API requests with access tokens use SSL.)
@param error_checking_block a block to evaluate the response status for additional JSON-encoded errors
@yield The response for evaluation
@raise [Koala::Facebook::ServerError] if Facebook returns an error (response status >= 500)
@return the body of the response from Facebook (unless another http_component is requested)
# File lib/koala/api.rb, line 54 def api(path, args = {}, verb = "get", options = {}, &error_checking_block) # we make a copy of args so the modifications (added access_token & appsecret_proof) # do not affect the received argument args = args.dup # If a access token is explicitly provided, use that # This is explicitly needed in batch requests so GraphCollection # results preserve any specific access tokens provided args["access_token"] ||= @access_token || @app_access_token if @access_token || @app_access_token if options.delete(:appsecret_proof) && args["access_token"] && @app_secret args["appsecret_proof"] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), @app_secret, args["access_token"]) end # Translate any arrays in the params into comma-separated strings args = sanitize_request_parameters(args) unless preserve_form_arguments?(options) # add a leading / if needed... path = "/#{path}" unless path =~ /^\// # make the request via the provided service result = Koala.make_request(path, args, verb, options) if result.status.to_i >= 500 raise Koala::Facebook::ServerError.new(result.status.to_i, result.body) end yield result if error_checking_block # if we want a component other than the body (e.g. redirect header for images), return that if component = options[:http_component] component == :response ? result : result.send(options[:http_component]) else # parse the body as JSON and run it through the error checker (if provided) # Note: Facebook sometimes sends results like "true" and "false", which are valid[RFC7159] # but unsupported by Ruby's stdlib[RFC4627] and cause JSON.load to fail -- so we account for # that by wrapping the result in [] JSON.load("[#{result.body.to_s}]")[0] end end
Creates a new API client. @param [String] #access_token access token @param [String] #app_secret app secret, for tying your access tokens to your app secret
If you provide an app secret, your requests will be signed by default, unless you pass appsecret_proof: false as an option to the API call. (See https://developers.facebook.com/docs/graph-api/securing-requests/)
@note If no access token is provided, you can only access some public information. @return [Koala::Facebook::API] the API client
# File lib/koala/api.rb, line 18 def initialize(access_token = nil, app_secret = nil) @access_token = access_token @app_secret = app_secret end