Class: FacebookAds::Batch
- Inherits:
-
Object
- Object
- FacebookAds::Batch
- Defined in:
- lib/facebook_ads/batch_api/batch.rb
Class Attribute Summary collapse
-
.current_batch ⇒ Object
Returns the value of attribute current_batch.
Instance Attribute Summary collapse
-
#last_api_response ⇒ Object
Returns the value of attribute last_api_response.
-
#operations ⇒ Object
Returns the value of attribute operations.
-
#session ⇒ Object
Returns the value of attribute session.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(api_req) ⇒ Object
- #batch_args(slice = operations) ⇒ Object
- #execute ⇒ Object
- #files_args ⇒ Object
-
#initialize ⇒ Batch
constructor
A new instance of Batch.
- #operations_args(slice) ⇒ Object
Constructor Details
#initialize ⇒ Batch
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_batch ⇒ Object
Returns the value of attribute current_batch.
70 71 72 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 70 def current_batch @current_batch end |
Instance Attribute Details
#last_api_response ⇒ Object
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 |
#operations ⇒ Object
Returns the value of attribute operations.
21 22 23 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 21 def operations @operations end |
#session ⇒ Object
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_batch ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 72 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_args(slice = operations) ⇒ Object
53 54 55 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 53 def batch_args(slice = operations) {batch: JSON.dump(operations_args(slice))}.merge(files_args) end |
#execute ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 33 def execute return [] if operations.empty? operations.each_slice(50) do |slice| api_response = APIRequest.new(:post, '', session: session, params: batch_args(slice)).execute_now self.last_api_response = api_response slice.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 end |
#files_args ⇒ Object
63 64 65 66 67 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 63 def files_args operations.map do |api_req| api_req.files end.reduce(&:merge) end |
#operations_args(slice) ⇒ Object
57 58 59 60 61 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 57 def operations_args(slice) slice.map do |api_req| api_req.to_batch_params end end |