Module: NewBridge::Envelope
- Defined in:
- lib/new_bridge/envelope.rb
Overview
MsgPack-encoded control-plane envelope (see docs/New_Bridge.md).
Defined Under Namespace
Classes: Error, InvalidEnvelope
Constant Summary collapse
- REQUIRED_KEYS =
%w[call_id type].freeze
- OPTIONAL_KEYS =
%w[parent_id payload status session_id instance_id].freeze
Class Method Summary collapse
Class Method Details
.decode(bytes) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/new_bridge/envelope.rb', line 29 def decode(bytes) raise InvalidEnvelope, 'empty envelope' if bytes.nil? || bytes.empty? h = MessagePack.unpack(bytes) unless h.is_a?(Hash) raise Error, 'invalid MsgPack envelope: expected a map (Hash)' end h = stringify_keys(h) missing = REQUIRED_KEYS - h.keys raise InvalidEnvelope, "missing keys: #{missing.join(', ')}" unless missing.empty? h rescue MessagePack::MalformedFormatError, MessagePack::UnpackError => e raise Error, "invalid MsgPack envelope: #{e.}" end |
.encode(hash) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/new_bridge/envelope.rb', line 16 def encode(hash) h = stringify_keys(hash) missing = REQUIRED_KEYS - h.keys raise InvalidEnvelope, "missing keys: #{missing.join(', ')}" unless missing.empty? extra = h.keys - (REQUIRED_KEYS + OPTIONAL_KEYS) raise InvalidEnvelope, "unknown keys: #{extra.join(', ')}" unless extra.empty? MessagePack.pack(h) rescue MessagePack::MalformedFormatError, MessagePack::UnpackError => e raise Error, "MsgPack pack failed: #{e.}" end |