Class: Kobako::Transport::Response

Inherits:
Data
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, payload:) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status: (Integer)
  • payload: (Object)


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

#payloadObject (readonly)

Returns the value of attribute payload.

Returns:

  • (Object)


8
9
10
# File 'sig/kobako/transport/response.rbs', line 8

def payload
  @payload
end

#statusInteger (readonly)

Returns the value of attribute status.

Returns:

  • (Integer)


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.

Parameters:

  • bytes (String)

Returns:



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

Parameters:

Returns:



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

.newResponse

Parameters:

  • status: (Integer)
  • payload: (Object)

Returns:



16
# File 'sig/kobako/transport/response.rbs', line 16

def self.new: (status: Integer, payload: untyped) -> Response

.ok(value) ⇒ Response

Parameters:

  • value (Object)

Returns:



29
30
31
# File 'lib/kobako/transport/response.rb', line 29

def self.ok(value)
  new(status: STATUS_OK, payload: value)
end

Instance Method Details

#==Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


28
# File 'sig/kobako/transport/response.rbs', line 28

def ==: (untyped other) -> bool

#encodeString

Encode this Response to msgpack bytes as the 2-element [status, payload] array.

Returns:

  • (String)


57
58
59
# File 'lib/kobako/transport/response.rb', line 57

def encode
  Codec::Encoder.encode([status, payload])
end

#error?Boolean

Returns:

  • (Boolean)


53
# File 'lib/kobako/transport/response.rb', line 53

def error? = status == STATUS_ERROR

#hashInteger

Returns:

  • (Integer)


30
# File 'sig/kobako/transport/response.rbs', line 30

def hash: () -> Integer

#ok?Boolean

Returns:

  • (Boolean)


52
# File 'lib/kobako/transport/response.rb', line 52

def ok?    = status == STATUS_OK

#withResponse

Parameters:

  • status: (Integer)
  • payload: (Object)

Returns:



20
# File 'sig/kobako/transport/response.rbs', line 20

def with: (?status: Integer, ?payload: untyped) -> Response