Class: Factorix::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/factorix/http/response.rb,
sig/factorix/http/response.rbs

Overview

Simple response wrapper for Net::HTTP responses

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(net_http_response, uri: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • net_http_response (Net::HTTPResponse) —

    Raw Net::HTTP response



15
16
17
18
19
20
21
# File 'lib/factorix/http/response.rb', line 15

def initialize(net_http_response, uri: nil)
  @code = Integer(net_http_response.code, 10)
  @body = net_http_response.body
  @headers = net_http_response.to_hash
  @raw_response = net_http_response
  @uri = uri
end

Instance Attribute Details

#body ⇒ String (readonly)

Returns the value of attribute body.

Returns:

  • (String)


8
9
10
# File 'lib/factorix/http/response.rb', line 8

def body
  @body
end

#code ⇒ Integer (readonly)

Returns the value of attribute code.

Returns:

  • (Integer)


7
8
9
# File 'lib/factorix/http/response.rb', line 7

def code
  @code
end

#headers ⇒ Hash[String, Array[String]] (readonly)

Returns the value of attribute headers.

Returns:

  • (Hash[String, Array[String]])


9
10
11
# File 'lib/factorix/http/response.rb', line 9

def headers
  @headers
end

#raw_response ⇒ Net::HTTPResponse (readonly)

Returns the value of attribute raw_response.

Returns:

  • (Net::HTTPResponse)


10
11
12
# File 'lib/factorix/http/response.rb', line 10

def raw_response
  @raw_response
end

#uri ⇒ Object (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/factorix/http/response.rb', line 11

def uri
  @uri
end

Instance Method Details

#content_length ⇒ Integer?

Get Content-Length from headers

Returns:

  • (Integer, nil) —

    content length in bytes, or nil if not present



31
# File 'lib/factorix/http/response.rb', line 31

def content_length = Integer(@headers["content-length"]&.first, 10)

#success? ⇒ Boolean

Check if response is successful (2xx)

Returns:

  • (Boolean) —

    true if 2xx response



26
# File 'lib/factorix/http/response.rb', line 26

def success? = (200..299).cover?(@code)