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.



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

Instance Attribute Details

#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



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_nameObject



63
64
65
# File 'lib/facebook_ads/api_request.rb', line 63

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



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

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

#enqueue_to_batchObject



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_nowObject



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

#filesObject

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_nameObject



67
68
69
# File 'lib/facebook_ads/api_request.rb', line 67

def generate_batch_name
  SecureRandom.hex(4)
end

#is_in_batch?Boolean

Returns:

  • (Boolean)


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

def is_in_batch?
  !current_batch.nil?
end

#params_without_filesObject

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_paramsObject



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