Class: Kobako::RPC::Response

Inherits:
Data
  • Object
show all
Defined in:
lib/kobako/rpc/envelope.rb

Overview

Value object for a single host-side RPC Response (SPEC.md Wire Codec → 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).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, payload:) ⇒ Response

Returns a new instance of Response.



88
89
90
91
92
93
94
95
96
97
# File 'lib/kobako/rpc/envelope.rb', line 88

def initialize(status:, payload:)
  unless [STATUS_OK, STATUS_ERROR].include?(status)
    raise ArgumentError, "Response status must be 0 or 1, got #{status.inspect}"
  end
  if status == STATUS_ERROR && !payload.is_a?(Kobako::RPC::Fault)
    raise ArgumentError, "Response status=1 payload must be Kobako::RPC::Fault"
  end

  super
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload

Returns:

  • (Object)

    the current value of payload



74
75
76
# File 'lib/kobako/rpc/envelope.rb', line 74

def payload
  @payload
end

#statusObject (readonly)

Returns the value of attribute status

Returns:

  • (Object)

    the current value of status



74
75
76
# File 'lib/kobako/rpc/envelope.rb', line 74

def status
  @status
end

Class Method Details

.error(fault) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/kobako/rpc/envelope.rb', line 80

def self.error(fault)
  unless fault.is_a?(Kobako::RPC::Fault)
    raise ArgumentError, "Response.error requires Kobako::RPC::Fault, got #{fault.class}"
  end

  new(status: STATUS_ERROR, payload: fault)
end

.ok(value) ⇒ Object

steep:ignore:start



76
77
78
# File 'lib/kobako/rpc/envelope.rb', line 76

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

Instance Method Details

#error?Boolean

Returns:

  • (Boolean)


100
101
# File 'lib/kobako/rpc/envelope.rb', line 100

def error? = status == STATUS_ERROR
# steep:ignore:end

#ok?Boolean

Returns:

  • (Boolean)


99
# File 'lib/kobako/rpc/envelope.rb', line 99

def ok?    = status == STATUS_OK