Sha256: 7548a7593b5c816b719d9e5e8511fda3a432ea9009c658ca627e70ce3c400d74

Contents?: true

Size: 1.63 KB

Versions: 59

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module Stripe
  # StripeResponse encapsulates some vitals of a response that came back from
  # the Stripe API.
  class StripeResponse
    # The data contained by the HTTP body of the response deserialized from
    # JSON.
    attr_accessor :data

    # The raw HTTP body of the response.
    attr_accessor :http_body

    # A Hash of the HTTP headers of the response.
    attr_accessor :http_headers

    # The integer HTTP status code of the response.
    attr_accessor :http_status

    # The Stripe request ID of the response.
    attr_accessor :request_id

    # Initializes a StripeResponse object from a Hash like the kind returned as
    # part of a Faraday exception.
    #
    # This may throw JSON::ParserError if the response body is not valid JSON.
    def self.from_faraday_hash(http_resp)
      resp = StripeResponse.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]["Request-Id"]
      resp
    end

    # Initializes a StripeResponse object from a Faraday HTTP response object.
    #
    # This may throw JSON::ParserError if the response body is not valid JSON.
    def self.from_faraday_response(http_resp)
      resp = StripeResponse.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["Request-Id"]
      resp
    end
  end
end

Version data entries

59 entries across 59 versions & 2 rubygems

Version Path
stripe-4.24.0 lib/stripe/stripe_response.rb
stripe-4.23.0 lib/stripe/stripe_response.rb
stripe-4.22.1 lib/stripe/stripe_response.rb
stripe-4.22.0 lib/stripe/stripe_response.rb
bongloy-4.21.3 lib/stripe/stripe_response.rb
stripe-4.21.3 lib/stripe/stripe_response.rb
stripe-4.21.2 lib/stripe/stripe_response.rb
stripe-4.21.1 lib/stripe/stripe_response.rb
stripe-4.21.0 lib/stripe/stripe_response.rb
stripe-4.20.1 lib/stripe/stripe_response.rb
stripe-4.20.0 lib/stripe/stripe_response.rb
stripe-4.19.0 lib/stripe/stripe_response.rb
stripe-4.18.1 lib/stripe/stripe_response.rb
stripe-4.18.0 lib/stripe/stripe_response.rb
stripe-4.17.0 lib/stripe/stripe_response.rb
stripe-4.16.0 lib/stripe/stripe_response.rb
stripe-4.15.0 lib/stripe/stripe_response.rb
stripe-4.14.0 lib/stripe/stripe_response.rb
stripe-4.13.0 lib/stripe/stripe_response.rb
stripe-4.12.0 lib/stripe/stripe_response.rb