Class: Nfe::Http::Response

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

Overview

Immutable value object describing a single HTTP response.

A Response carries the raw status code, normalized headers (lowercase string keys), and a binary-safe body. HTTP errors (4xx/5xx) are returned as ordinary responses — never raised — so the retry decorator and ErrorFactory can act on the status. Redirects and 202 are not followed by the transport; the Location header is preserved here.

response = Nfe::Http::Response.new(status: 200, headers: { "content-type" => "application/json" }, body: "{}")
response.success?               # => true
response.header("Content-Type") # => "application/json"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers: {}, body: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status (Integer)

    HTTP status code.

  • headers (Hash{String=>String}) (defaults to: {})

    lowercase-keyed response headers.

  • body (String, nil) (defaults to: nil)

    ASCII-8BIT response body, or nil.



20
21
22
# File 'lib/nfe/http/response.rb', line 20

def initialize(status:, headers: {}, body: nil)
  super
end

Instance Attribute Details

#bodyString? (readonly)

Returns the value of attribute body.

Returns:

  • (String, nil)


6
7
8
# File 'sig/nfe/http/response.rbs', line 6

def body
  @body
end

#headersHash[String, String] (readonly)

Returns the value of attribute headers.

Returns:

  • (Hash[String, String])


5
6
7
# File 'sig/nfe/http/response.rbs', line 5

def headers
  @headers
end

#statusInteger (readonly)

Returns the value of attribute status.

Returns:

  • (Integer)


4
5
6
# File 'sig/nfe/http/response.rbs', line 4

def status
  @status
end

Class Method Details

.newObject



8
# File 'sig/nfe/http/response.rbs', line 8

def self.new: (

Instance Method Details

#header(name) ⇒ String?

Case-insensitive header lookup.

Parameters:

  • name (String)

    header name in any casing.

Returns:

  • (String, nil)

    the header value, or nil when absent.



28
29
30
# File 'lib/nfe/http/response.rb', line 28

def header(name)
  headers[name.downcase]
end

#locationString?

Returns the Location header, or nil.

Returns:

  • (String, nil)

    the Location header, or nil.



38
39
40
# File 'lib/nfe/http/response.rb', line 38

def location
  header("location")
end

#success?Boolean

Returns true when the status is in the 2xx range.

Returns:

  • (Boolean)

    true when the status is in the 2xx range.



33
34
35
# File 'lib/nfe/http/response.rb', line 33

def success?
  (200..299).cover?(status)
end