Class: CoinbaseCommerce::CoinbaseCommerceResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/coinbase_commerce/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/api_response.rb', line 3

def data
  @data
end

#http_bodyObject

Returns the value of attribute http_body.



5
6
7
# File 'lib/coinbase_commerce/api_response.rb', line 5

def http_body
  @http_body
end

#http_headersObject

Returns the value of attribute http_headers.



7
8
9
# File 'lib/coinbase_commerce/api_response.rb', line 7

def http_headers
  @http_headers
end

#http_statusObject

Returns the value of attribute http_status.



9
10
11
# File 'lib/coinbase_commerce/api_response.rb', line 9

def http_status
  @http_status
end

#request_idObject

Returns the value of attribute request_id.



11
12
13
# File 'lib/coinbase_commerce/api_response.rb', line 11

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.



15
16
17
18
19
20
21
22
23
# File 'lib/coinbase_commerce/api_response.rb', line 15

def self.from_faraday_hash(http_resp)
  resp = CoinbaseCommerceResponse.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.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/coinbase_commerce/api_response.rb', line 27

def self.from_faraday_response(http_resp)
  resp = CoinbaseCommerceResponse.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
  if resp.data.is_a? Hash and resp.data.fetch(:data, nil).is_a? Hash
    resp.data.update(resp.data.delete(:data))
  end

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

  resp
end