Sha256: 833f5654d3481acc6794a58031c93de4218e2398b5a91e0d5889de2c221677d4

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 KB

Contents

module LedgerSync
  module Ledgers
    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

3 entries across 3 versions & 1 rubygems

Version Path
ledger_sync-1.4.2 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.4.1 lib/ledger_sync/ledgers/response.rb
ledger_sync-1.4.0 lib/ledger_sync/ledgers/response.rb