Sha256: 578f4ed7d2355fbd629a5f8dff5aa5f645f38b28d38e1809a9b0b77712f06acc

Contents?: true

Size: 1.62 KB

Versions: 3

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module Helio
  # HelioResponse encapsulates some vitals of a response that came back from
  # the Helio API.
  class HelioResponse
    # 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 Helio request ID of the response.
    attr_accessor :request_id

    # Initializes a HelioResponse 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 = HelioResponse.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 HelioResponse 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 = HelioResponse.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

3 entries across 3 versions & 1 rubygems

Version Path
helio-ruby-0.3.1 lib/helio/helio_response.rb
helio-ruby-0.3.0 lib/helio/helio_response.rb
helio-ruby-0.2.0 lib/helio/helio_response.rb