Class: Cased::Response

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

Direct Known Subclasses

CollectionResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response: nil, exception: nil) ⇒ Response

Returns a new instance of Response.



7
8
9
10
11
# File 'lib/cased/response.rb', line 7

def initialize(response: nil, exception: nil)
  @response = response
  @body = response&.body
  @exception = exception
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



5
6
7
# File 'lib/cased/response.rb', line 5

def body
  @body
end

#exceptionObject (readonly)

Returns the value of attribute exception.



5
6
7
# File 'lib/cased/response.rb', line 5

def exception
  @exception
end

Instance Method Details

#errorObject



13
14
15
# File 'lib/cased/response.rb', line 13

def error
  @exception.presence || (body && body['error']).presence
end

#error?Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cased/response.rb', line 17

def error?
  # If there was an exception during the execution of the request.
  return true if @exception.present?

  # If the HTTP response was outside of 200-299
  return true unless @response.success?

  # If the HTTP response contained an error key.
  return true if body && body['error'].present?

  false
end

#success?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
# File 'lib/cased/response.rb', line 30

def success?
  return false if @response.nil?

  @response.success?
end