Module: Koala::Facebook::GraphBatchAPIMethods

Included in:
GraphBatchAPI
Defined in:
lib/koala/graph_batch_api.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'lib/koala/graph_batch_api.rb', line 5

def self.included(base)
  base.class_eval do
    alias_method :graph_call_outside_batch, :graph_call
    alias_method :graph_call, :graph_call_in_batch
    
    alias_method :check_graph_api_response, :check_response
    alias_method :check_response, :check_graph_batch_api_response
  end
end

Instance Method Details

#batch_callsObject



15
16
17
# File 'lib/koala/graph_batch_api.rb', line 15

def batch_calls
  @batch_calls ||= []
end

#check_graph_batch_api_response(response) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/koala/graph_batch_api.rb', line 32

def check_graph_batch_api_response(response)
  if response.is_a?(Hash) && response["error"] && !response["error"].is_a?(Hash)
    APIError.new("type" => "Error #{response["error"]}", "message" => response["error_description"])
  else
    check_graph_api_response(response)
  end
end

#execute(http_options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/koala/graph_batch_api.rb', line 40

def execute(http_options = {})
  return [] unless batch_calls.length > 0
  # Turn the call args collected into what facebook expects
  args = {}
  args["batch"] = MultiJson.encode(batch_calls.map { |batch_op|
    args.merge!(batch_op.files) if batch_op.files
    batch_op.to_batch_params(access_token)
  })
  
  graph_call_outside_batch('/', args, 'post', http_options) do |response|
    # 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

      if call_result
        # (see note in regular api method about JSON parsing)
        body = MultiJson.decode("[#{call_result['body'].to_s}]")[0]
        
        unless call_result["code"].to_i >= 500 || error = check_response(body)
          # Get the HTTP component they want
          data = 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
            call_result['headers'].inject({}) { |headers, h| headers[h['name']] = h['value']; headers}
          else
            body
          end

          # process it if we are given a block to process with
          batch_op.post_processing ? batch_op.post_processing.call(data) : data
        else
          error || APIError.new({"type" => "HTTP #{call_result["code"].to_s}", "message" => "Response body: #{body}"})
        end
      else
        nil
      end
    end          
  end
end

#graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/koala/graph_batch_api.rb', line 19

def graph_call_in_batch(path, args = {}, verb = "get", options = {}, &post_processing)
    # 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