class GunBroker::Response

Wrapper class for the GunBroker API response JSON.

Public Class Methods

new(response) click to toggle source

@param response [Net::HTTPResponse] Response returned from the API.

# File lib/gun_broker/response.rb, line 6
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

Public Instance Methods

[](key) click to toggle source

@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.

# File lib/gun_broker/response.rb, line 23
def [](key)
  @data[key]
end
body() click to toggle source

@return [Hash] The response body as a Hash.

# File lib/gun_broker/response.rb, line 28
def body
  @data
end
fetch(key) click to toggle source

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`.

# File lib/gun_broker/response.rb, line 36
def fetch(key)
  @data.fetch(key)
end