Class: BSV::Network::ProtocolResponse
- Inherits:
-
Object
- Object
- BSV::Network::ProtocolResponse
- Defined in:
- lib/bsv/network/protocol_response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#error_message ⇒ Object
(also: #message)
readonly
Returns the value of attribute error_message.
Instance Method Summary collapse
-
#body ⇒ Object
Delegated from Net::HTTPResponse (nil-safe).
-
#canonical ⇒ Object
Canonical form (placeholder — delegates to data until shapes are defined).
- #code ⇒ Object
- #content_type ⇒ Object
- #http_not_found? ⇒ Boolean
-
#http_success? ⇒ Boolean
Two layers of status predicates:.
-
#initialize(http_response, data: nil, http_success: nil, error_message: nil) ⇒ ProtocolResponse
constructor
A new instance of ProtocolResponse.
- #retryable? ⇒ Boolean
-
#with(**overrides) ⇒ Object
Derive new response with overrides (same HTTP response, different interpretation).
Constructor Details
#initialize(http_response, data: nil, http_success: nil, error_message: nil) ⇒ ProtocolResponse
Returns a new instance of ProtocolResponse.
10 11 12 13 14 15 16 |
# File 'lib/bsv/network/protocol_response.rb', line 10 def initialize(http_response, data: nil, http_success: nil, error_message: nil) @http_response = http_response @data = data @http_success = http_success.nil? ? http_response.is_a?(Net::HTTPSuccess) : http_success @error_message = freeze end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
8 9 10 |
# File 'lib/bsv/network/protocol_response.rb', line 8 def data @data end |
#error_message ⇒ Object (readonly) Also known as: message
Returns the value of attribute error_message.
8 9 10 |
# File 'lib/bsv/network/protocol_response.rb', line 8 def @error_message end |
Instance Method Details
#body ⇒ Object
Delegated from Net::HTTPResponse (nil-safe)
19 20 21 |
# File 'lib/bsv/network/protocol_response.rb', line 19 def body @http_response&.body end |
#canonical ⇒ Object
Canonical form (placeholder — delegates to data until shapes are defined)
61 62 63 |
# File 'lib/bsv/network/protocol_response.rb', line 61 def canonical data end |
#code ⇒ Object
23 24 25 |
# File 'lib/bsv/network/protocol_response.rb', line 23 def code @http_response&.code end |
#content_type ⇒ Object
27 28 29 |
# File 'lib/bsv/network/protocol_response.rb', line 27 def content_type @http_response&.content_type end |
#http_not_found? ⇒ Boolean
51 52 53 |
# File 'lib/bsv/network/protocol_response.rb', line 51 def http_not_found? @http_response.is_a?(Net::HTTPNotFound) end |
#http_success? ⇒ Boolean
Two layers of status predicates:
HTTP logical status — did the operation succeed? Driven by the http_success: parameter, which defaults to the RFC 9110 success class check (Net::HTTPSuccess, i.e. 2xx) but can be overridden by escape hatches that reinterpret HTTP status (e.g. ARC REJECTED on 2xx sets http_success: false; WoC is_utxo 404 sets http_success: true).
Transport status — what did the HTTP layer actually return? These always reflect the Net::HTTPResponse class hierarchy regardless of the http_success: override. http_not_found? maps directly to Net::HTTPNotFound (404). retryable? is a domain composite: 429 (Net::HTTPTooManyRequests) or 5xx (Net::HTTPServerError) — not an RFC 9110 category itself, but a useful signal for retry logic.
47 48 49 |
# File 'lib/bsv/network/protocol_response.rb', line 47 def http_success? @http_success end |
#retryable? ⇒ Boolean
55 56 57 58 |
# File 'lib/bsv/network/protocol_response.rb', line 55 def retryable? @http_response.is_a?(Net::HTTPTooManyRequests) || @http_response.is_a?(Net::HTTPServerError) end |
#with(**overrides) ⇒ Object
Derive new response with overrides (same HTTP response, different interpretation)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bsv/network/protocol_response.rb', line 69 def with(**overrides) derived = self.class.new( @http_response, data: overrides.fetch(:data, @data), http_success: overrides.fetch(:http_success, @http_success), error_message: overrides.fetch(:error_message, @error_message) ) BSV.logger&.debug do changes = overrides.keys.map { |k| "#{k}=#{overrides[k].inspect[0, 40]}" }.join(' ') "[ProtocolResponse] with(#{changes})" end derived end |