Class: OmiseGO::Request

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

Constant Summary collapse

PARAM_FIELDS =
%i[page per_page search_term search_terms sort_by sort_dir].freeze

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Request

Returns a new instance of Request.



5
6
7
8
# File 'lib/omisego/request.rb', line 5

def initialize(client)
  @client = client
  @config = @client.config
end

Instance Method Details

#send(path, body, params: {}, conn: new_conn) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/omisego/request.rb', line 10

def send(path, body, params: {}, conn: new_conn)
  idempotency_token = body.delete(:idempotency_token)
  body = add_params(body, params)

  response = conn.post do |req|
    req.url path
    req.headers['Authorization'] = authorization
    req.headers['Accept'] = content_type
    req.headers['Content-Type'] = content_type
    req.headers['Idempotency-Token'] = idempotency_token if idempotency_token
    req.body = body.to_json if body
    logger.log_request(req)
  end

  logger.log_response(response)

  unless [200, 500].include?(response.status)
    return error('invalid_status_code',
                 "The server returned an invalid status code: #{response.status}")
  end

  json = JSON.parse(response.body)
  Response.new(json, @client)
rescue JSON::ParserError => e
  error('json_parsing_error',
        "The JSON received from the server could not be parsed: #{e.message}")
rescue Faraday::Error::ConnectionFailed => e
  error('connection_failed', e.message)
end