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) ⇒ Response

Returns a new instance of Response.

Parameters:

  • net_http_response (Net::HTTPResponse)

    Raw Net::HTTP response



13
14
15
16
17
18
# File 'lib/factorix/http/response.rb', line 13

def initialize(net_http_response)
  @code = Integer(net_http_response.code, 10)
  @body = net_http_response.body
  @headers = net_http_response.to_hash
  @raw_response = net_http_response
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

Instance Method Details

#content_lengthInteger?

Get Content-Length from headers

Returns:

  • (Integer, nil)

    content length in bytes, or nil if not present



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

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

#success?Boolean

Check if response is successful (2xx)

Returns:

  • (Boolean)

    true if 2xx response



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

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