Class: BSV::Network::ProtocolResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/bsv/network/protocol_response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = error_message
  freeze
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/bsv/network/protocol_response.rb', line 8

def data
  @data
end

#error_messageObject (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
  @error_message
end

Instance Method Details

#bodyObject

Delegated from Net::HTTPResponse (nil-safe)



19
20
21
# File 'lib/bsv/network/protocol_response.rb', line 19

def body
  @http_response&.body
end

#canonicalObject

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

#codeObject



23
24
25
# File 'lib/bsv/network/protocol_response.rb', line 23

def code
  @http_response&.code
end

#content_typeObject



27
28
29
# File 'lib/bsv/network/protocol_response.rb', line 27

def content_type
  @http_response&.content_type
end

#http_not_found?Boolean

Returns:

  • (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.

Returns:

  • (Boolean)


47
48
49
# File 'lib/bsv/network/protocol_response.rb', line 47

def http_success?
  @http_success
end

#retryable?Boolean

Returns:

  • (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