Class: Koala::Facebook::BatchOperation

Inherits:
Object
  • Object
show all
Defined in:
lib/koala/batch_operation.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BatchOperation

Returns a new instance of BatchOperation.

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/koala/batch_operation.rb', line 12

def initialize(options = {})
  @identifier = self.class.next_identifier
  @args = (options[:args] || {}).dup # because we modify it below
  @access_token = options[:access_token]
  @http_options = (options[:http_options] || {}).dup # dup because we modify it below
  @batch_args = @http_options.delete(:batch_args) || {}
  @url = options[:url]
  @method = options[:method].to_sym
  @post_processing = options[:post_processing]
  
  process_binary_args
  
  raise Koala::KoalaError, "Batch operations require an access token, none provided." unless @access_token
end

Instance Attribute Details

#access_tokenObject (readonly)

Returns the value of attribute access_token.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def access_token
  @access_token
end

#batch_apiObject (readonly)

Returns the value of attribute batch_api.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def batch_api
  @batch_api
end

#filesObject (readonly)

Returns the value of attribute files.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def files
  @files
end

#http_optionsObject (readonly)

Returns the value of attribute http_options.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def http_options
  @http_options
end

#identifierObject (readonly)

Returns the value of attribute identifier.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def identifier
  @identifier
end

#post_processingObject (readonly)

Returns the value of attribute post_processing.



4
5
6
# File 'lib/koala/batch_operation.rb', line 4

def post_processing
  @post_processing
end

Class Method Details

.next_identifierObject



8
9
10
# File 'lib/koala/batch_operation.rb', line 8

def self.next_identifier
  @identifier += 1
end

Instance Method Details

#to_batch_params(main_access_token) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/koala/batch_operation.rb', line 27

def to_batch_params(main_access_token)
  # set up the arguments
  args_string = Koala.http_service.encode_params(@access_token == main_access_token ? @args : @args.merge(:access_token => @access_token))
  
  response = {
    :method => @method.to_s, 
    :relative_url => @url,
  }
   
  # handle batch-level arguments, such as name, depends_on, and attached_files
  @batch_args[:attached_files] = @files.keys.join(",") if @files
  response.merge!(@batch_args) if @batch_args
  
  # for get and delete, we append args to the URL string
  # otherwise, they go in the body
  if args_string.length > 0
    if args_in_url?
      response[:relative_url] += (@url =~ /\?/ ? "&" : "?") + args_string if args_string.length > 0
    else
      response[:body] = args_string if args_string.length > 0
    end
  end
  
  response
end