Class: Collavre::HttpClient::Response

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre/http_client.rb

Overview

A minimal, read-only view over a Net::HTTP response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_response) ⇒ Response

Returns a new instance of Response.



45
46
47
48
49
50
51
# File 'app/services/collavre/http_client.rb', line 45

def initialize(net_response)
  @net_response = net_response
  @code = net_response.code.to_i
  @message = net_response.message
  @body = net_response.body
  @headers = net_response.to_hash
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



43
44
45
# File 'app/services/collavre/http_client.rb', line 43

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



43
44
45
# File 'app/services/collavre/http_client.rb', line 43

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



43
44
45
# File 'app/services/collavre/http_client.rb', line 43

def headers
  @headers
end

#messageObject (readonly)

Returns the value of attribute message.



43
44
45
# File 'app/services/collavre/http_client.rb', line 43

def message
  @message
end

Instance Method Details

#jsonObject

Parse the response body as JSON, or nil when the body is empty.



58
59
60
61
62
# File 'app/services/collavre/http_client.rb', line 58

def json
  return nil if body.nil? || body.empty?

  JSON.parse(body)
end

#success?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'app/services/collavre/http_client.rb', line 53

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