Class: AnswerLayer::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/answerlayer/request.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  "Accept" => "application/json",
  "Content-Type" => "application/json"
}.freeze
FORM_HEADERS =
{
  "Accept" => "application/json",
  "Content-Type" => "application/x-www-form-urlencoded"
}.freeze
MULTIPART_HEADERS =
{
  "Accept" => "application/json",
  "Content-Type" => "multipart/form-data"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, authentication: Authentication.new(configuration)) ⇒ Request

Returns a new instance of Request.



14
15
16
17
# File 'lib/answerlayer/request.rb', line 14

def initialize(configuration:, authentication: Authentication.new(configuration))
  @configuration = configuration
  @authentication = authentication
end

Instance Method Details

#call(method:, path:, params: nil, body: nil, headers: {}, auth: :api_key, subject: false, form: nil, multipart: nil, download: false) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/answerlayer/request.rb', line 29

def call(method:, path:, params: nil, body: nil, headers: {}, auth: :api_key, subject: false, form: nil, multipart: nil, download: false)
  uri = build_uri(path, params)
  request = build_request(method, uri, body, headers: headers, auth: auth, subject: subject, form: form, multipart: multipart)
  response = perform(uri, request)
  parsed_response(response, download: download)
rescue Timeout::Error, Errno::ECONNREFUSED, SocketError => error
  raise RequestError, "request failed: #{error.message}"
end