Sha256: 2d6fe23d46e55d0bfd8a116348ea4a7b1bfafb602e7904ced556b1939b2835ca

Contents?: true

Size: 1.2 KB

Versions: 18

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

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

      def initialize(request:, status:, body: nil, headers: {}, raw: nil)
        @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

18 entries across 18 versions & 1 rubygems

Version Path
ledger_sync-2.6.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.5.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.4.4 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.3.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.2.3 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.2.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.2.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.0.2 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.0.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.0.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-2.0.0.pre.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.8.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.8.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.7.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.6.0 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.5.2 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.5.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.5.0 lib/ledger_sync/ledgers/response.rb