class Koala::Facebook::GraphBatchAPI

@private

Constants

MAX_CALLS

Limits from @see developers.facebook.com/docs/marketing-api/batch-requests/v2.8

Attributes

original_api[R]

Public Class Methods

new(api) click to toggle source
   # File lib/koala/api/graph_batch_api.rb
15 def initialize(api)
16   @original_api = api
17 end

Public Instance Methods

access_token() click to toggle source
    # File lib/koala/api/graph_batch_api.rb
145 def access_token
146   original_api.access_token
147 end
app_secret() click to toggle source
    # File lib/koala/api/graph_batch_api.rb
149 def app_secret
150   original_api.app_secret
151 end
bad_response() click to toggle source
   # File lib/koala/api/graph_batch_api.rb
84 def bad_response
85   # Facebook sometimes reportedly returns an empty body at times
86   BadFacebookResponse.new(200, "", "Facebook returned an empty body")
87 end
batch_args(calls_for_batch) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
118 def batch_args(calls_for_batch)
119   calls = calls_for_batch.map do |batch_op|
120     batch_op.to_batch_params(access_token, app_secret)
121   end
122 
123   JSON.dump calls
124 end
batch_calls() click to toggle source
   # File lib/koala/api/graph_batch_api.rb
19 def batch_calls
20   @batch_calls ||= []
21 end
desired_component(component:, response:, headers:) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
132 def desired_component(component:, response:, headers:)
133   result = Koala::HTTPService::Response.new(response['status'], response['body'], headers)
134 
135   # Get the HTTP component they want
136   case component
137   when :status  then response["code"].to_i
138   # facebook returns the headers as an array of k/v pairs, but we want a regular hash
139   when :headers then headers
140   # (see note in regular api method about JSON parsing)
141   else GraphCollection.evaluate(result, original_api)
142   end
143 end
error_from_response(response, headers) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
111 def error_from_response(response, headers)
112   code = response["code"]
113   body = response["body"].to_s
114 
115   GraphErrorChecker.new(code, body, headers).error_if_appropriate
116 end
execute(http_options = {}) click to toggle source

execute the queued batch calls. limits it to 50 requests per call. NOTE: if you use `name` and JsonPath references, you should ensure to call `execute` for each co-reference group and that the group size is not greater than the above limits.

   # File lib/koala/api/graph_batch_api.rb
44 def execute(http_options = {})
45   return [] if batch_calls.empty?
46 
47   batch_results = []
48   batch_calls.each_slice(MAX_CALLS) do |batch|
49     # Turn the call args collected into what facebook expects
50     args = {"batch" => batch_args(batch)}
51     batch.each do |call|
52       args.merge!(call.files || {})
53     end
54 
55     original_api.graph_call("/", args, "post", http_options) do |response|
56       raise bad_response if response.nil?
57 
58       batch_results += generate_results(response, batch)
59     end
60   end
61 
62   batch_results
63 end
generate_results(response, batch) click to toggle source
   # File lib/koala/api/graph_batch_api.rb
65 def generate_results(response, batch)
66   index = 0
67   response.map do |call_result|
68     batch_op = batch[index]
69     index += 1
70     post_process = batch_op.post_processing
71 
72     # turn any results that are pageable into GraphCollections
73     result = result_from_response(call_result, batch_op)
74 
75     # and pass to post-processing callback if given
76     if post_process
77       post_process.call(result)
78     else
79       result
80     end
81   end
82 end
graph_call(path, args = {}, verb = "get", options = {}, &post_processing) click to toggle source

Enqueue a call into the batch for later processing. See API#graph_call

   # File lib/koala/api/graph_batch_api.rb
25 def graph_call(path, args = {}, verb = "get", options = {}, &post_processing)
26   # normalize options for consistency
27   options = Koala::Utils.symbolize_hash(options)
28 
29   # for batch APIs, we queue up the call details (incl. post-processing)
30   batch_calls << BatchOperation.new(
31     :url => path,
32     :args => args,
33     :method => verb,
34     :access_token => options[:access_token] || access_token,
35     :http_options => options,
36     :post_processing => post_processing
37   )
38   nil # batch operations return nothing immediately
39 end
headers_from_response(response) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
103 def headers_from_response(response)
104   headers = response.fetch("headers", [])
105 
106   headers.inject({}) do |compiled_headers, header|
107     compiled_headers.merge(header.fetch("name") => header.fetch("value"))
108   end
109 end
json_body(response) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
126 def json_body(response)
127   # quirks_mode is needed because Facebook sometimes returns a raw true or false value --
128   # in Ruby 2.4 we can drop that.
129   JSON.parse(response.fetch("body"), quirks_mode: true)
130 end
result_from_response(response, options) click to toggle source
    # File lib/koala/api/graph_batch_api.rb
 89 def result_from_response(response, options)
 90   return nil if response.nil?
 91 
 92   headers   = headers_from_response(response)
 93   error     = error_from_response(response, headers)
 94   component = options.http_options[:http_component]
 95 
 96   error || desired_component(
 97     component: component,
 98     response: response,
 99     headers: headers
100   )
101 end