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.
26 27 28 29 30 31 32 33 |
# File 'lib/facebook_ads/api_request.rb', line 26 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.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def batch_proxy @batch_proxy end |
#callback ⇒ Object (readonly)
Returns the value of attribute callback.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def callback @callback end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def path @path end |
#session ⇒ Object (readonly)
Returns the value of attribute session.
23 24 25 |
# File 'lib/facebook_ads/api_request.rb', line 23 def session @session end |
Instance Method Details
#batch_body ⇒ Object
For Batch API
103 104 105 106 107 |
# File 'lib/facebook_ads/api_request.rb', line 103 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
75 76 77 |
# File 'lib/facebook_ads/api_request.rb', line 75 def batch_name @batch_name ||= (.dig(:batch_args, :name) || generate_batch_name) end |
#create_response(status, headers, body) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/facebook_ads/api_request.rb', line 61 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
83 84 85 |
# File 'lib/facebook_ads/api_request.rb', line 83 def current_batch .dig(:batch_args, :batch) || Batch.current_batch end |
#enqueue_to_batch ⇒ Object
56 57 58 59 |
# File 'lib/facebook_ads/api_request.rb', line 56 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
46 47 48 49 |
# File 'lib/facebook_ads/api_request.rb', line 46 def execute(&block) @callback = block if block is_in_batch? ? enqueue_to_batch : execute_now end |
#execute_now ⇒ Object
51 52 53 54 |
# File 'lib/facebook_ads/api_request.rb', line 51 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
117 118 119 120 121 |
# File 'lib/facebook_ads/api_request.rb', line 117 def files params.select do |_,v| v.is_a?(UploadIO) end end |
#generate_batch_name ⇒ Object
79 80 81 |
# File 'lib/facebook_ads/api_request.rb', line 79 def generate_batch_name SecureRandom.hex(4) end |
#is_in_batch? ⇒ Boolean
87 88 89 |
# File 'lib/facebook_ads/api_request.rb', line 87 def is_in_batch? !current_batch.nil? end |
#params_without_files ⇒ Object
For Batch API
110 111 112 113 114 |
# File 'lib/facebook_ads/api_request.rb', line 110 def params_without_files params.reject do |_,v| v.is_a?(UploadIO) end end |
#to_batch_params ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/facebook_ads/api_request.rb', line 91 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 |