Sha256: 5a5f39fefc0c8b4232a13ed8e3af09bee6ef2bd42b0a64bd46182c9848c6ef53

Contents?: true

Size: 1.12 KB

Versions: 25

Compression:

Stored size: 1.12 KB

Contents

module GunBroker
  # Wrapper class for the GunBroker API response JSON.
  class Response

    # @param response [Net::HTTPResponse] Response returned from the API.
    def initialize(response)
      @response = response

      case @response
      when Net::HTTPOK, Net::HTTPSuccess
        @data = JSON.parse(@response.body)
      when Net::HTTPUnauthorized
        raise GunBroker::Error::NotAuthorized.new(@response.body)
      when Net::HTTPNotFound
        raise GunBroker::Error::NotFound.new(@response.body)
      else
        raise GunBroker::Error::RequestError.new(@response.body)
      end
    end

    # @param key [String] Key from the response JSON to read.
    # @return [String, Array, Hash] Whatever object is the value of `key` or `nil` if the key doesn't exist.
    def [](key)
      @data[key]
    end

    # @return [Hash] The response body as a Hash.
    def body
      @data
    end

    # Like Hash#fetch
    # @param [Object] A key from the response JSON.
    # @raise [KeyError] If `key` is not in the response.
    # @return [Object] The value for `key`.
    def fetch(key)
      @data.fetch(key)
    end

  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
gun_broker-1.4.8 lib/gun_broker/response.rb
gun_broker-1.4.7 lib/gun_broker/response.rb
gun_broker-1.4.6 lib/gun_broker/response.rb
gun_broker-1.4.5 lib/gun_broker/response.rb
gun_broker-1.4.4 lib/gun_broker/response.rb
gun_broker-1.4.3 lib/gun_broker/response.rb
gun_broker-1.4.2 lib/gun_broker/response.rb
gun_broker-1.4.1 lib/gun_broker/response.rb
gun_broker-1.4.0 lib/gun_broker/response.rb
gun_broker-1.3.2 lib/gun_broker/response.rb
gun_broker-1.3.1 lib/gun_broker/response.rb
gun_broker-1.3.0 lib/gun_broker/response.rb
gun_broker-1.2.1.1 lib/gun_broker/response.rb
gun_broker-1.2.1 lib/gun_broker/response.rb
gun_broker-1.2.0 lib/gun_broker/response.rb
gun_broker-1.1.3.1 lib/gun_broker/response.rb
gun_broker-1.1.2 lib/gun_broker/response.rb
gun_broker-1.1.1 lib/gun_broker/response.rb
gun_broker-1.1.0 lib/gun_broker/response.rb
gun_broker-1.0.0 lib/gun_broker/response.rb