class Koala::Facebook::GraphBatchAPI

@private

Attributes

original_api[R]

Public Instance Methods

batch_calls() click to toggle source
# File lib/koala/api/graph_batch_api.rb, line 17
def batch_calls
  @batch_calls ||= []
end
execute(http_options = {}) click to toggle source

execute the queued batch calls

# File lib/koala/api/graph_batch_api.rb, line 43
def execute(http_options = {})
  return [] unless batch_calls.length > 0
  # Turn the call args collected into what facebook expects
  args = {}
  args["batch"] = JSON.dump(batch_calls.map { |batch_op|
    args.merge!(batch_op.files) if batch_op.files
    batch_op.to_batch_params(access_token, app_secret)
  })

  batch_result = graph_call_outside_batch('/', args, 'post', http_options) do |response|
    unless response
      # Facebook sometimes reportedly returns an empty body at times
      # see https://github.com/arsduo/koala/issues/184
      raise BadFacebookResponse.new(200, '', "Facebook returned an empty body")
    end

    # map the results with post-processing included
    index = 0 # keep compat with ruby 1.8 - no with_index for map
    response.map do |call_result|
      # Get the options hash
      batch_op = batch_calls[index]
      index += 1

      raw_result = nil
      if call_result
        parsed_headers = if call_result.has_key?('headers')
          call_result['headers'].inject({}) { |headers, h| headers[h['name']] = h['value']; headers}
        else
          {}
        end

        if (error = check_response(call_result['code'], call_result['body'].to_s, parsed_headers))
          raw_result = error
        else
          # (see note in regular api method about JSON parsing)
          body = JSON.load("[#{call_result['body'].to_s}]")[0]

          # Get the HTTP component they want
          raw_result = case batch_op.http_options[:http_component]
          when :status
            call_result["code"].to_i
          when :headers
            # facebook returns the headers as an array of k/v pairs, but we want a regular hash
            parsed_headers
          else
            body
          end
        end
      end

      # turn any results that are pageable into GraphCollections
      # and pass to post-processing callback if given
      result = GraphCollection.evaluate(raw_result, @original_api)
      if batch_op.post_processing
        batch_op.post_processing.call(result)
      else
        result
      end
    end
  end
end
graph_call(path, args = {}, verb = "get", options = {}, &post_processing) click to toggle source
Also aliased as: graph_call_outside_batch
Alias for: graph_call_in_batch
graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing) click to toggle source
# File lib/koala/api/graph_batch_api.rb, line 21
def graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing)
  # normalize options for consistency
  options = Koala::Utils.symbolize_hash(options)

  # for batch APIs, we queue up the call details (incl. post-processing)
  batch_calls << BatchOperation.new(
    :url => path,
    :args => args,
    :method => verb,
    :access_token => options[:access_token] || access_token,
    :http_options => options,
    :post_processing => post_processing
  )
  nil # batch operations return nothing immediately
end
Also aliased as: graph_call
graph_call_outside_batch(path, args = {}, verb = "get", options = {}, &post_processing) click to toggle source

redefine the #graph_call method so we can use this API inside the batch block just like any regular Graph API

Alias for: graph_call

Public Class Methods

new(api) click to toggle source
# File lib/koala/api/graph_batch_api.rb, line 12
def initialize(api)
  super(api.access_token, api.app_secret)
  @original_api = api
end