Sha256: 472f2599144aab1c468ae979d271a1688418815ef729845030db35440ad28261

Contents?: true

Size: 818 Bytes

Versions: 3

Compression:

Stored size: 818 Bytes

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)
      when Net::HTTPNotFound
        raise GunBroker::Error::NotFound.new(@response)
      else
        raise GunBroker::Error::RequestError.new(@response)
      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

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gun_broker-0.4.8 lib/gun_broker/response.rb
gun_broker-0.4.7 lib/gun_broker/response.rb
gun_broker-0.4.6 lib/gun_broker/response.rb