Sha256: c612a2a2558b25f6fb63300e1652f2e52cecd707e5a5deb4065c0a54dde19e9b

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

# typed: strict
# frozen_string_literal: true

module Httpsensible
  class Client
    class Response
      attr_reader :response

      def initialize(response)
        @response = response
      end

      # sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(String) }
      def to_s
        case @response
        when HTTPX::Response
          "#{@response.status}, #{@response.headers}, #{@response.body}"
        when HTTPX::ErrorResponse
          @response.error.message
        else
          ""
        end
      end

      # sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(T::Boolean) }
      def parsed_json_body
        JSON.parse(raw_body)
      rescue JSON::ParserError
        {}
      end

      def raw_body
        @raw_body ||= @response.body.read
      end

      # sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(T::Boolean) }
      def unavailable?
        return true if @response.nil?
        return false if (200..299).cover?(@response.status)

        case @response
        when HTTPX::Response
          raw_body.nil? || raw_body.empty? || raw_body == "{}"
        when HTTPX::ErrorResponse
          true
        end
      end

      def available?
        !unavailable?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
httpsensible-0.2.1 lib/httpsensible/client/response.rb
httpsensible-0.1.1 lib/httpsensible/client/response.rb