Class: FacebookAds::APIRequest

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = options
  @batch_proxy = nil
end

Instance Attribute Details

#batch_proxyObject (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

#callbackObject (readonly)

Returns the value of attribute callback.



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

def callback
  @callback
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#sessionObject (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_bodyObject

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_nameObject



75
76
77
# File 'lib/facebook_ads/api_request.rb', line 75

def batch_name
  @batch_name ||= (options.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_batchObject



83
84
85
# File 'lib/facebook_ads/api_request.rb', line 83

def current_batch
  options.dig(:batch_args, :batch) || Batch.current_batch
end

#enqueue_to_batchObject



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_nowObject



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

#filesObject

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_nameObject



79
80
81
# File 'lib/facebook_ads/api_request.rb', line 79

def generate_batch_name
  SecureRandom.hex(4)
end

#is_in_batch?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/facebook_ads/api_request.rb', line 87

def is_in_batch?
  !current_batch.nil?
end

#params_without_filesObject

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_paramsObject



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