Sha256: 7c0d413dd4b992dbd6a660089d862943129af88702e9757d4e1d376111a3dfae

Contents?: true

Size: 1.2 KB

Versions: 1

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(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

1 entries across 1 versions & 1 rubygems

Version Path
ledger_sync-1.4.4 lib/ledger_sync/ledgers/response.rb