Class: Httpsensible::Client::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response) ⇒ Response

Returns a new instance of Response.



9
10
11
# File 'lib/httpsensible/client/response.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/httpsensible/client/response.rb', line 7

def response
  @response
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/httpsensible/client/response.rb', line 45

def available?
  !unavailable?
end

#parsed_json_bodyObject

sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(T::Boolean) }



26
27
28
29
30
# File 'lib/httpsensible/client/response.rb', line 26

def parsed_json_body
  JSON.parse(raw_body)
rescue JSON::ParserError
  {}
end

#raw_bodyObject



32
33
34
# File 'lib/httpsensible/client/response.rb', line 32

def raw_body
  @raw_body ||= @response.body.read
end

#to_sObject

sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(String) }



14
15
16
17
18
19
20
21
22
23
# File 'lib/httpsensible/client/response.rb', line 14

def to_s
  case @response
  when HTTPX::Response
    "#{@response.status}, #{@response.headers}, #{@response.body}"
  when HTTPX::ErrorResponse
    @response.error.message
  else
    ""
  end
end

#unavailable?Boolean

sig { params(response: T.any(HTTPX::Response, HTTPX::ErrorResponse)).returns(T::Boolean) }

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/httpsensible/client/response.rb', line 37

def unavailable?
  return true if @response.nil?
  return true if @response.is_a?(HTTPX::ErrorResponse)
  return false if (200..299).cover?(@response.status)

  true if (400..599).cover?(@response.status)
end