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 ⇒ Object
- #execute ⇒ Object
- #files_args ⇒ Object
-
#initialize ⇒ Batch
constructor
A new instance of Batch.
- #operations_args ⇒ 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.
68 69 70 |
# File 'lib/facebook_ads/batch_api/batch.rb', line 68 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
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_args ⇒ Object
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 |
#execute ⇒ Object
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_args ⇒ Object
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_args ⇒ Object
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 |