Class: GunBroker::Response
- Inherits:
-
Object
- Object
- GunBroker::Response
- Defined in:
- lib/gun_broker/response.rb
Overview
Wrapper class for the GunBroker API response JSON.
Instance Method Summary collapse
-
#[](key) ⇒ String, ...
Whatever object is the value of
key
ornil
if the key doesn't exist. -
#body ⇒ Hash
The response body as a Hash.
-
#fetch(key) ⇒ Object
Like Hash#fetch.
-
#initialize(response) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(response) ⇒ Response
Returns a new instance of Response.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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:: 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 |
Instance Method Details
#[](key) ⇒ String, ...
Returns Whatever object is the value of key
or nil
if the key doesn't exist.
23 24 25 |
# File 'lib/gun_broker/response.rb', line 23 def [](key) @data[key] end |
#body ⇒ Hash
Returns The response body as a Hash.
28 29 30 |
# File 'lib/gun_broker/response.rb', line 28 def body @data end |
#fetch(key) ⇒ Object
Like Hash#fetch
36 37 38 |
# File 'lib/gun_broker/response.rb', line 36 def fetch(key) @data.fetch(key) end |