Class: Kobako::Transport::Response
- Inherits:
-
Data
- Object
- Data
- Kobako::Transport::Response
- Defined in:
- lib/kobako/transport/response.rb,
sig/kobako/transport/response.rbs
Overview
Value object for a single host-side Transport Response (docs/wire-codec.md Envelope Encoding → Response).
2-element msgpack array: [status, value-or-fault]. status is 0
(success) or 1 (fault). For success the second element is the return
value; for fault it is a Fault (ext 0x02 envelope).
Built on the class X < Data.define(...) subclass form so the
class body is fully Steep-visible; see lib/kobako/outcome/panic.rb
for the rationale.
Instance Attribute Summary collapse
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#status ⇒ Integer
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.decode(bytes) ⇒ Response
Decode
bytesinto a Response. - .error(fault) ⇒ Response
- .new ⇒ Response
- .ok(value) ⇒ Response
Instance Method Summary collapse
- #== ⇒ Boolean
-
#encode ⇒ String
Encode this Response to msgpack bytes as the 2-element
[status, payload]array. - #error? ⇒ Boolean
- #hash ⇒ Integer
-
#initialize(status:, payload:) ⇒ Response
constructor
A new instance of Response.
- #ok? ⇒ Boolean
- #with ⇒ Response
Constructor Details
#initialize(status:, payload:) ⇒ Response
Returns a new instance of Response.
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/kobako/transport/response.rb', line 41 def initialize(status:, payload:) unless [STATUS_OK, STATUS_ERROR].include?(status) raise ArgumentError, "Response status must be 0 (ok) or 1 (error), got #{status.inspect}" end if status == STATUS_ERROR && !payload.is_a?(Kobako::Fault) raise ArgumentError, "Response with error status must carry a Kobako::Fault payload" end super end |
Instance Attribute Details
#payload ⇒ Object (readonly)
Returns the value of attribute payload.
8 9 10 |
# File 'sig/kobako/transport/response.rbs', line 8 def payload @payload end |
#status ⇒ Integer (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'sig/kobako/transport/response.rbs', line 7 def status @status end |
Class Method Details
.decode(bytes) ⇒ Response
Decode bytes into a Kobako::Transport::Response. Raises Codec::InvalidType when the
envelope is not the expected 2-element msgpack array, or when the
Value Object's construction invariants reject the decoded fields.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kobako/transport/response.rb', line 64 def self.decode(bytes) Codec::Decoder.decode(bytes) do |arr| unless arr.is_a?(Array) && arr.length == 2 raise Codec::InvalidType, "Response envelope is malformed (expected a 2-element array)" end status, payload = arr new(status: status, payload: payload) end end |
.error(fault) ⇒ Response
33 34 35 36 37 38 39 |
# File 'lib/kobako/transport/response.rb', line 33 def self.error(fault) unless fault.is_a?(Kobako::Fault) raise ArgumentError, "Response.error requires Kobako::Fault, got #{fault.class}" end new(status: STATUS_ERROR, payload: fault) end |
.new ⇒ Response
16 |
# File 'sig/kobako/transport/response.rbs', line 16
def self.new: (status: Integer, payload: untyped) -> Response
|
Instance Method Details
#== ⇒ Boolean
28 |
# File 'sig/kobako/transport/response.rbs', line 28
def ==: (untyped other) -> bool
|
#encode ⇒ String
Encode this Response to msgpack bytes as the 2-element
[status, payload] array.
57 58 59 |
# File 'lib/kobako/transport/response.rb', line 57 def encode Codec::Encoder.encode([status, payload]) end |
#error? ⇒ Boolean
53 |
# File 'lib/kobako/transport/response.rb', line 53 def error? = status == STATUS_ERROR |
#hash ⇒ Integer
30 |
# File 'sig/kobako/transport/response.rbs', line 30
def hash: () -> Integer
|
#with ⇒ Response
20 |
# File 'sig/kobako/transport/response.rbs', line 20
def with: (?status: Integer, ?payload: untyped) -> Response
|