Class: CoinbaseCommerceClient::CoinbaseCommerceClientResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase_commerce_client/api_response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/coinbase_commerce_client/api_response.rb', line 3

def data
  @data
end

#http_bodyObject

Returns the value of attribute http_body.



3
4
5
# File 'lib/coinbase_commerce_client/api_response.rb', line 3

def http_body
  @http_body
end

#http_headersObject

Returns the value of attribute http_headers.



3
4
5
# File 'lib/coinbase_commerce_client/api_response.rb', line 3

def http_headers
  @http_headers
end

#http_statusObject

Returns the value of attribute http_status.



3
4
5
# File 'lib/coinbase_commerce_client/api_response.rb', line 3

def http_status
  @http_status
end

#request_idObject

Returns the value of attribute request_id.



3
4
5
# File 'lib/coinbase_commerce_client/api_response.rb', line 3

def request_id
  @request_id
end

Class Method Details

.from_faraday_hash(http_resp) ⇒ Object

Initializes a CoinbaseCommerceResponse object from a Hash like the kind returned as part of a Faraday exception.



7
8
9
10
11
12
13
14
15
# File 'lib/coinbase_commerce_client/api_response.rb', line 7

def self.from_faraday_hash(http_resp)
  resp = CoinbaseCommerceClientResponse.new
  resp.data = JSON.parse(http_resp[:body], symbolize_names: true)
  resp.http_body = http_resp[:body]
  resp.http_headers = http_resp[:headers]
  resp.http_status = http_resp[:status]
  resp.request_id = http_resp[:headers]['x-request-id']
  resp
end

.from_faraday_response(http_resp) ⇒ Object

Initializes a CoinbaseCommerceResponse object from a Faraday HTTP response object.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/coinbase_commerce_client/api_response.rb', line 19

def self.from_faraday_response(http_resp)
  resp = CoinbaseCommerceClientResponse.new
  resp.data = JSON.parse(http_resp.body, symbolize_names: true)
  resp.http_body = http_resp.body
  resp.http_headers = http_resp.headers
  resp.http_status = http_resp.status
  resp.request_id = http_resp.headers['x-request-id']

  # unpack nested data field if it exist
  resp.data.update(resp.data.delete(:data)) if resp.data.is_a?(Hash) && resp.data.fetch(:data, nil).is_a?(Hash)

  # warn in there warnings in response
  warn(resp.data[:warnings].first.to_s) if resp.data.is_a?(Hash) && resp.data.fetch(:warnings, nil).is_a?(Array)

  resp
end