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, base_path: nil) ⇒ APIRequest

Returns a new instance of APIRequest.



14
15
16
17
18
19
20
21
22
# File 'lib/facebook_ads/api_request.rb', line 14

def initialize(method, path, session: nil, params: nil, options: nil, base_path: nil)
  @method = method
  @path = path
  @session = session
  @params = params
  @options = options
  @batch_proxy = nil
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def base_path
  @base_path
end

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

#callbackObject (readonly)

Returns the value of attribute callback.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def callback
  @callback
end

#methodObject (readonly)

Returns the value of attribute method.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def method
  @method
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def options
  @options
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/facebook_ads/api_request.rb', line 11

def path
  @path
end

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

For Batch API



104
105
106
107
108
# File 'lib/facebook_ads/api_request.rb', line 104

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



70
71
72
# File 'lib/facebook_ads/api_request.rb', line 70

def batch_name
  @batch_name ||= (options.dig(:batch_args, :name) || generate_batch_name)
end

#create_response(status, headers, body) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/facebook_ads/api_request.rb', line 56

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



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

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

#enqueue_to_batchObject



51
52
53
54
# File 'lib/facebook_ads/api_request.rb', line 51

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


35
36
37
38
# File 'lib/facebook_ads/api_request.rb', line 35

def execute(&block)
  @callback = block if block
  is_in_batch? ? enqueue_to_batch : execute_now
end

#execute_nowObject



40
41
42
43
44
45
46
47
48
49
# File 'lib/facebook_ads/api_request.rb', line 40

def execute_now
  request_path = if base_path && !base_path.empty?
    clean_base_path = base_path.gsub(/^\/|\/$/,'')
    "#{clean_base_path}/#{path}"
  else
    path
  end
  faraday_response = session.request(method, request_path, params)
  create_response(faraday_response.status, faraday_response.headers, faraday_response.body)
end

#filesObject

For Batch API



118
119
120
121
122
# File 'lib/facebook_ads/api_request.rb', line 118

def files
  params.select do |_,v|
    v.is_a?(Faraday::UploadIO)
  end
end

#generate_batch_nameObject



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

def generate_batch_name
  SecureRandom.hex(4)
end

#is_in_batch?Boolean

Returns:

  • (Boolean)


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

def is_in_batch?
  !current_batch.nil?
end

#params_without_filesObject

For Batch API



111
112
113
114
115
# File 'lib/facebook_ads/api_request.rb', line 111

def params_without_files
  params.reject do |_,v|
    v.is_a?(Faraday::UploadIO)
  end
end

#to_batch_paramsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/facebook_ads/api_request.rb', line 86

def to_batch_params
  relative_url = if base_path && !base_path.empty?
    clean_base_path = base_path.gsub(/^\/|\/$/,'')
    "#{clean_base_path}/#{path}"
  else
    path
  end
  {
    name: batch_name,
    method: method.to_s.upcase,
    relative_url: relative_url,
    body: batch_body,
    omit_response_on_success: false,
    attached_files: files.empty? ? nil : files.keys.join(','),
  }.compact
end