Class: SecondAmendmentWholesale::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/second_amendment_wholesale/response.rb

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/second_amendment_wholesale/response.rb', line 4

def initialize(response)
  @response = response

  case @response
  when Net::HTTPUnauthorized
    raise SecondAmendmentWholesale::Error::NotAuthorized.new(@response.body)
  when Net::HTTPNotFound
    raise SecondAmendmentWholesale::Error::NotFound.new(@response.body)
  when Net::HTTPBadRequest
    raise SecondAmendmentWholesale::Error::BadRequest.new(@response.body)
  when Net::HTTPOK, Net::HTTPSuccess
    _data = (JSON.parse(@response.body) if @response.body.present?)

    @data = case
    when _data.is_a?(Hash)
      _data.deep_symbolize_keys
    when _data.is_a?(Array)
      _data.map(&:deep_symbolize_keys)
    else
      _data
    end
  else
    raise SecondAmendmentWholesale::Error::RequestError.new(@response.body)
  end

end

Instance Method Details

#[](key) ⇒ Object



31
32
33
# File 'lib/second_amendment_wholesale/response.rb', line 31

def [](key)
  @data&.[](key)
end

#bodyObject



35
36
37
# File 'lib/second_amendment_wholesale/response.rb', line 35

def body
  @data
end