Sha256: 840a5cc76a040c8ae331c8b78258927699b75c1607dac68fb34dbcd6100ee2ff

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

module LedgerSync
  module Adaptors
    class Response
      attr_reader :body,
                  :headers,
                  :raw,
                  :request,
                  :status

      def initialize(body: nil, headers: {}, raw: nil, request:, status:)
        @body = parse_json(body)
        @headers = headers
        @raw = raw
        @request = request
        @status = status
      end

      def failure?
        !success?
      end

      def success?
        (200..299).include?(status)
      end

      def self.new_from_faraday_response(faraday_response:, request:)
        new(
          body: faraday_response.body,
          headers: faraday_response.headers,
          raw: faraday_response,
          request: request,
          status: faraday_response.status
        )
      end

      def self.new_from_oauth_response(oauth_response:, request:)
        # Uses the same API
        new_from_faraday_response(
          faraday_response: oauth_response,
          request: request
        )
      end

      private

      def parse_json(json)
        return if json.nil?

        JSON.parse(json)
      rescue JSON::ParserError
        nil
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ledger_sync-1.3.5 lib/ledger_sync/adaptors/response.rb
ledger_sync-1.3.4 lib/ledger_sync/adaptors/response.rb
ledger_sync-1.3.3 lib/ledger_sync/adaptors/response.rb
ledger_sync-1.3.2 lib/ledger_sync/adaptors/response.rb
ledger_sync-1.3.1 lib/ledger_sync/adaptors/response.rb