Sha256: 228b60eadf74dd6b5adc04c69ccd0537fd19f8e42abf037adb19316df050bc89

Contents?: true

Size: 979 Bytes

Versions: 5

Compression:

Stored size: 979 Bytes

Contents

module SecondAmendmentWholesale
  class Response

    def initialize(response)
      @response = response

      case @response
      when Net::HTTPUnauthorized
        raise SecondAmendmentWholesale::Error::NotAuthorized.new(@response.body)
      when Net::HTTPNotFound
        raise SecondAmendmentWholesale::Error::NotFound.new(@response.body)
      when Net::HTTPBadRequest
        raise SecondAmendmentWholesale::Error::BadRequest.new(@response.body)
      when Net::HTTPOK, Net::HTTPSuccess
        _data = (JSON.parse(@response.body) if @response.body.present?)

        @data = case
        when _data.is_a?(Hash)
          _data.deep_symbolize_keys
        when _data.is_a?(Array)
          _data.map(&:deep_symbolize_keys)
        else
          _data
        end
      else
        raise SecondAmendmentWholesale::Error::RequestError.new(@response.body)
      end

    end

    def [](key)
      @data&.[](key)
    end

    def body
      @data
    end
    
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
second_amendment_wholesale-1.0.5 lib/second_amendment_wholesale/response.rb
second_amendment_wholesale-1.0.3 lib/second_amendment_wholesale/response.rb
second_amendment_wholesale-1.0.2 lib/second_amendment_wholesale/response.rb
second_amendment_wholesale-1.0.1 lib/second_amendment_wholesale/response.rb
second_amendment_wholesale-1.0.0 lib/second_amendment_wholesale/response.rb