Sha256: e241c746d5922c79e1485643d3b6cafd37322d784a130bfa748d76dcf524c2e3

Contents?: true

Size: 964 Bytes

Versions: 1

Compression:

Stored size: 964 Bytes

Contents

module Lipseys
  class Response

    def initialize(response)
      @response = response

      case @response
      when Net::HTTPUnauthorized
        Lipseys::Error::NotAuthorized.new(@response.body)
      when Net::HTTPNotFound
        Lipseys::Error::NotFound.new(@response.body)
      when Net::HTTPNoContent
        Lipseys::Error::NoContent.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)
        end
      else
        raise Lipseys::Error::RequestError.new(@response.body)
      end
    end

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

    def body
      @data
    end

    def fetch(key)
      @data.fetch(key)
    end

    def success?
      !!self[:success] && self[:errors].empty?
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lipseys-6.1.1 lib/lipseys/response.rb