Class: SeismicAPI::Response

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/seismic_api/response.rb

Overview

Response represents a response from Seismic.

It is returned when using methods in Request.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

def_delegators :@raw_response, :code


23
24
25
# File 'lib/seismic_api/response.rb', line 23

def initialize(response)
  @raw_response = response
end

Instance Attribute Details

#raw_responseObject (readonly)

Returns the value of attribute raw_response


19
20
21
# File 'lib/seismic_api/response.rb', line 19

def raw_response
  @raw_response
end

Instance Method Details

#bodyHash

Body of the request

Returns:

  • (Hash)

    request body


41
42
43
44
45
# File 'lib/seismic_api/response.rb', line 41

def body
  return {} if @raw_response.body.strip.empty?

  JSON.parse(@raw_response.body)
end

#codeObject


34
35
36
# File 'lib/seismic_api/response.rb', line 34

def code
  Integer(@raw_response.code)
end

#raise_on_errorObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/seismic_api/response.rb', line 47

def raise_on_error
  exception_class =
    case code
    when 400; HTTPBadRequest
    when 401; HTTPUnauthorized
    when 403; HTTPForbidden
    when (400...500); HTTPClientError
    when (500...600); HTTPServerError
    end

  if exception_class
    raise exception_class.new(
      "HTTP code: #{code}, message: #{body.dig("error", "message")}"
    )
  end

  self
end

#success?Boolean

Was the response code 2xx

Returns:

  • (Boolean)

30
31
32
# File 'lib/seismic_api/response.rb', line 30

def success?
  @raw_response.is_a?(Net::HTTPSuccess)
end