Class: Factorix::HTTP::Response

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

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

  • uri (URI, nil) (defaults to: nil)

    Final URI after following redirects



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

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



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

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#raw_responseObject (readonly)

Returns the value of attribute raw_response.



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

def raw_response
  @raw_response
end

#uriObject (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_lengthInteger?

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)