Class: FacebookAds::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/facebook_ads/batch_api/batch.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatch

Returns a new instance of Batch.



23
24
25
# File 'lib/facebook_ads/batch_api/batch.rb', line 23

def initialize
  @operations = []
end

Class Attribute Details

.current_batchObject

Returns the value of attribute current_batch.



68
69
70
# File 'lib/facebook_ads/batch_api/batch.rb', line 68

def current_batch
  @current_batch
end

Instance Attribute Details

#last_api_responseObject

Returns the value of attribute last_api_response.



21
22
23
# File 'lib/facebook_ads/batch_api/batch.rb', line 21

def last_api_response
  @last_api_response
end

#operationsObject

Returns the value of attribute operations.



21
22
23
# File 'lib/facebook_ads/batch_api/batch.rb', line 21

def operations
  @operations
end

#sessionObject

Returns the value of attribute session.



21
22
23
# File 'lib/facebook_ads/batch_api/batch.rb', line 21

def session
  @session
end

Class Method Details

.with_batchObject



70
71
72
73
74
75
76
# File 'lib/facebook_ads/batch_api/batch.rb', line 70

def with_batch
  new.tap do |current_batch|
    self.current_batch = current_batch
    yield if block_given?
    self.current_batch = nil
  end
end

Instance Method Details

#<<(api_req) ⇒ Object



27
28
29
30
31
# File 'lib/facebook_ads/batch_api/batch.rb', line 27

def <<(api_req)
  @operations << api_req
  @session ||= api_req.session
  api_req
end

#batch_argsObject



51
52
53
# File 'lib/facebook_ads/batch_api/batch.rb', line 51

def batch_args
  {batch: JSON.dump(operations_args)}.merge(files_args)
end

#executeObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/facebook_ads/batch_api/batch.rb', line 33

def execute
  return [] if operations.empty?
  api_response = APIRequest.new(:post, '', session: session, params: batch_args).execute_now
  self.last_api_response = api_response
  operations.zip(api_response.result).map do |req, res|
    next unless res

    begin
      req.create_response(
        res['code'],
        convert_headers_to_hash(res['headers']),
        res['body'])
    rescue APIError => e
      e
    end
  end
end

#files_argsObject



61
62
63
64
65
# File 'lib/facebook_ads/batch_api/batch.rb', line 61

def files_args
  operations.map do |api_req|
    api_req.files
  end.reduce(&:merge)
end

#operations_argsObject



55
56
57
58
59
# File 'lib/facebook_ads/batch_api/batch.rb', line 55

def operations_args
  operations.map do |api_req|
    api_req.to_batch_params
  end
end