class Koala::Facebook::GraphBatchAPI::BatchOperation

@private

Attributes

access_token[R]
batch_api[R]
files[R]
http_options[R]
identifier[R]
post_processing[R]

Public Class Methods

new(options = {}) click to toggle source
   # File lib/koala/api/batch_operation.rb
17 def initialize(options = {})
18   @identifier = self.class.next_identifier
19   @args = (options[:args] || {}).dup # because we modify it below
20   @access_token = options[:access_token]
21   @http_options = (options[:http_options] || {}).dup # dup because we modify it below
22   @batch_args = @http_options.delete(:batch_args) || {}
23   @url = options[:url]
24   @method = options[:method].to_sym
25   @post_processing = options[:post_processing]
26 
27   process_binary_args
28 
29   raise AuthenticationError.new(nil, nil, "Batch operations require an access token, none provided.") unless @access_token
30 end
next_identifier() click to toggle source
   # File lib/koala/api/batch_operation.rb
13 def self.next_identifier
14   @identifier += 1
15 end

Public Instance Methods

to_batch_params(main_access_token, app_secret) click to toggle source
   # File lib/koala/api/batch_operation.rb
32 def to_batch_params(main_access_token, app_secret)
33   # set up the arguments
34   if @access_token != main_access_token
35     @args[:access_token] = @access_token
36     if app_secret
37       @args[:appsecret_proof] = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new("sha256"), app_secret, @access_token)
38     end
39   end
40   args_string = Koala.http_service.encode_params(@args)
41 
42   response = {
43     :method => @method.to_s,
44     :relative_url => @url,
45   }
46 
47   # handle batch-level arguments, such as name, depends_on, and attached_files
48   @batch_args[:attached_files] = @files.keys.join(",") if @files
49   response.merge!(@batch_args) if @batch_args
50 
51   # for get and delete, we append args to the URL string
52   # otherwise, they go in the body
53   if args_string.length > 0
54     if args_in_url?
55       response[:relative_url] += (@url =~ /\?/ ? "&" : "?") + args_string if args_string.length > 0
56     else
57       response[:body] = args_string if args_string.length > 0
58     end
59   end
60 
61   response
62 end

Protected Instance Methods

args_in_url?() click to toggle source
   # File lib/koala/api/batch_operation.rb
80 def args_in_url?
81   @method == :get || @method == :delete
82 end
process_binary_args() click to toggle source
   # File lib/koala/api/batch_operation.rb
66 def process_binary_args
67   # collect binary files
68   @args.each_pair do |key, value|
69     if HTTPService::UploadableIO.binary_content?(value)
70       @files ||= {}
71       # we use a class-level counter to ensure unique file identifiers across multiple batch operations
72       # (this is thread safe, since we just care about uniqueness)
73       # so remove the file from the original hash and add it to the file store
74       id = "op#{identifier}_file#{@files.keys.length}"
75       @files[id] = @args.delete(key).is_a?(HTTPService::UploadableIO) ? value : HTTPService::UploadableIO.new(value)
76     end
77   end
78 end