Sha256: a649df9733f7cb14131c3e516f65fa682b31e838ba611fba5ff4aace6c09b0b8

Contents?: true

Size: 1.44 KB

Versions: 4

Compression:

Stored size: 1.44 KB

Contents

module CoinbaseCommerceClient
  class CoinbaseCommerceClientResponse
    attr_accessor :data, :http_body, :http_headers, :http_status, :request_id

    # Initializes a CoinbaseCommerceResponse object
    # from a Hash like the kind returned as part of a Faraday exception.
    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

    # Initializes a CoinbaseCommerceResponse object
    # from a Faraday HTTP response object.
    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
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
coinbase_commerce_client-0.4.2 lib/coinbase_commerce_client/api_response.rb
coinbase_commerce_client-0.3.0 lib/coinbase_commerce_client/api_response.rb
coinbase_commerce_client-0.2.0 lib/coinbase_commerce_client/api_response.rb
coinbase_commerce_client-0.1.0 lib/coinbase_commerce_client/api_response.rb