Class: FacebookAds::APIRequest
- Inherits:
-
Object
- Object
- FacebookAds::APIRequest
- Defined in:
- lib/facebook_ads/api_request.rb
Instance Attribute Summary collapse
-
#batch_proxy ⇒ Object
readonly
Returns the value of attribute batch_proxy.
-
#callback ⇒ Object
readonly
Returns the value of attribute callback.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
-
#batch_body ⇒ Object
For Batch API.
- #batch_name ⇒ Object
- #create_response(status, headers, body) ⇒ Object
- #current_batch ⇒ Object
- #enqueue_to_batch ⇒ Object
-
#execute(&block) ⇒ Object
Returns either APIResponse instantly if not within a batch, or a Proxy object to the result if a batch is present.
- #execute_now ⇒ Object
-
#files ⇒ Object
For Batch API.
- #generate_batch_name ⇒ Object
-
#initialize(method, path, session: nil, params: nil, options: nil) ⇒ APIRequest
constructor
A new instance of APIRequest.
- #is_in_batch? ⇒ Boolean
-
#params_without_files ⇒ Object
For Batch API.
- #to_batch_params ⇒ Object
Constructor Details
#initialize(method, path, session: nil, params: nil, options: nil) ⇒ APIRequest
Returns a new instance of APIRequest.
14 15 16 17 18 19 20 21 |
# File 'lib/facebook_ads/api_request.rb', line 14 def initialize(method, path, session: nil, params: nil, options: nil) @method = method @path = path @session = session @params = params @options = @batch_proxy = nil end |
Instance Attribute Details
#batch_proxy ⇒ Object (readonly)
Returns the value of attribute batch_proxy.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def batch_proxy @batch_proxy end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def callback @callback end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def path @path end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
11 12 13 |
# File 'lib/facebook_ads/api_request.rb', line 11 def session @session end |
Instance Method Details
#batch_body ⇒ Object
For Batch API
91 92 93 94 95 |
# File 'lib/facebook_ads/api_request.rb', line 91 def batch_body # TODO Have our own encoders or param flattener? params = Faraday::Utils::ParamsHash[params_without_files] params.to_query(Faraday::FlatParamsEncoder) end |
#batch_name ⇒ Object
63 64 65 |
# File 'lib/facebook_ads/api_request.rb', line 63 def batch_name @batch_name ||= (.dig(:batch_args, :name) || generate_batch_name) end |
#create_response(status, headers, body) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/facebook_ads/api_request.rb', line 49 def create_response(status, headers, body) api_response = APIResponse.new(status, headers, body) if status.to_i >= 500 raise ServerError.new(api_response) elsif status.to_i >= 400 raise ClientError.new(api_response) end (callback ? callback[api_response] : api_response).tap do |result| batch_proxy.set_result(result) if batch_proxy end end |
#current_batch ⇒ Object
71 72 73 |
# File 'lib/facebook_ads/api_request.rb', line 71 def current_batch .dig(:batch_args, :batch) || Batch.current_batch end |
#enqueue_to_batch ⇒ Object
44 45 46 47 |
# File 'lib/facebook_ads/api_request.rb', line 44 def enqueue_to_batch current_batch << self @batch_proxy = BatchProxy.new(self) end |
#execute(&block) ⇒ Object
Returns either APIResponse instantly if not within a batch, or a Proxy object to the result if a batch is present.
Examples
Illustrate the behaviour of the method using examples. Indent examples:
api_request APIRequest.new(:get, '123545') do |response|
update_attributes(response)
end
34 35 36 37 |
# File 'lib/facebook_ads/api_request.rb', line 34 def execute(&block) @callback = block if block is_in_batch? ? enqueue_to_batch : execute_now end |
#execute_now ⇒ Object
39 40 41 42 |
# File 'lib/facebook_ads/api_request.rb', line 39 def execute_now faraday_response = session.request(method, path, params) create_response(faraday_response.status, faraday_response.headers, faraday_response.body) end |
#files ⇒ Object
For Batch API
105 106 107 108 109 |
# File 'lib/facebook_ads/api_request.rb', line 105 def files params.select do |_,v| v.is_a?(UploadIO) end end |
#generate_batch_name ⇒ Object
67 68 69 |
# File 'lib/facebook_ads/api_request.rb', line 67 def generate_batch_name SecureRandom.hex(4) end |
#is_in_batch? ⇒ Boolean
75 76 77 |
# File 'lib/facebook_ads/api_request.rb', line 75 def is_in_batch? !current_batch.nil? end |
#params_without_files ⇒ Object
For Batch API
98 99 100 101 102 |
# File 'lib/facebook_ads/api_request.rb', line 98 def params_without_files params.reject do |_,v| v.is_a?(UploadIO) end end |
#to_batch_params ⇒ Object
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/facebook_ads/api_request.rb', line 79 def to_batch_params { name: batch_name, method: method.to_s.upcase, relative_url: path, body: batch_body, omit_response_on_success: false, attached_files: files.empty? ? nil : files.keys.join(','), }.compact end |