Class: ShippingEasy::Http::ResponseHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shipping_easy/http/response_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



9
10
11
# File 'lib/shipping_easy/http/response_handler.rb', line 9

def initialize(response)
  @response = response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



7
8
9
# File 'lib/shipping_easy/http/response_handler.rb', line 7

def response
  @response
end

Class Method Details

.run(response) ⇒ Object



13
14
15
# File 'lib/shipping_easy/http/response_handler.rb', line 13

def self.run(response)
  new(response).run
end

Instance Method Details

#runObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/shipping_easy/http/response_handler.rb', line 17

def run
  case status
  when 200, 201 then JSON.parse(response.body)
  when 400 then raise ShippingEasy::InvalidRequestError, response.body
  when 401 then raise ShippingEasy::AccessDeniedError, response.body
  when 404 then raise ShippingEasy::ResourceNotFoundError, response.body
  else
    raise ShippingEasy::Error, response.body
  end
end