Class: Arcp::Envelope
- Inherits:
-
Data
- Object
- Data
- Arcp::Envelope
- Defined in:
- lib/arcp/envelope.rb
Overview
ARCP wire envelope per spec §5.1. Eight fields: arcp, id, type, session_id, trace_id, job_id, event_seq, payload.
‘trace_id` and `job_id` may be nil for session-level envelopes. `event_seq` is nil for non-event traffic; events carry a monotonic Integer.
Constant Summary collapse
- HEX32 =
/\A[0-9a-f]{32}\z/
Instance Attribute Summary collapse
-
#arcp ⇒ Object
readonly
Returns the value of attribute arcp.
-
#event_seq ⇒ Object
readonly
Returns the value of attribute event_seq.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#job_id ⇒ Object
readonly
Returns the value of attribute job_id.
-
#payload ⇒ Object
readonly
Returns the value of attribute payload.
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#trace_id ⇒ Object
readonly
Returns the value of attribute trace_id.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
- .build(type:, session_id:, payload:, trace_id: nil, job_id: nil, event_seq: nil, id: nil) ⇒ Object
- .deep_freeze(value) ⇒ Object
- .from_h(hash) ⇒ Object
- .from_json(bytes) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#arcp ⇒ Object (readonly)
Returns the value of attribute arcp
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def arcp @arcp end |
#event_seq ⇒ Object (readonly)
Returns the value of attribute event_seq
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def event_seq @event_seq end |
#id ⇒ Object (readonly)
Returns the value of attribute id
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def id @id end |
#job_id ⇒ Object (readonly)
Returns the value of attribute job_id
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def job_id @job_id end |
#payload ⇒ Object (readonly)
Returns the value of attribute payload
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def payload @payload end |
#session_id ⇒ Object (readonly)
Returns the value of attribute session_id
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def session_id @session_id end |
#trace_id ⇒ Object (readonly)
Returns the value of attribute trace_id
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def trace_id @trace_id end |
#type ⇒ Object (readonly)
Returns the value of attribute type
15 16 17 |
# File 'lib/arcp/envelope.rb', line 15 def type @type end |
Class Method Details
.build(type:, session_id:, payload:, trace_id: nil, job_id: nil, event_seq: nil, id: nil) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/arcp/envelope.rb', line 18 def self.build(type:, session_id:, payload:, trace_id: nil, job_id: nil, event_seq: nil, id: nil) raise Arcp::Errors::InvalidRequest, 'trace_id must be 32 hex chars' if trace_id && trace_id !~ HEX32 new( arcp: Arcp::PROTOCOL_VERSION, id: id || Arcp::Ids.envelope_id, type: type, session_id: session_id, trace_id: trace_id, job_id: job_id, event_seq: event_seq, payload: payload || {} ) end |
.deep_freeze(value) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/arcp/envelope.rb', line 79 def self.deep_freeze(value) case value when Hash value.each_value { |v| deep_freeze(v) } when Array value.each { |v| deep_freeze(v) } end value.freeze end |
.from_h(hash) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/arcp/envelope.rb', line 33 def self.from_h(hash) raise Arcp::Errors::InvalidRequest, 'envelope must be a Hash' unless hash.is_a?(Hash) h = hash.transform_keys(&:to_s) arcp = h['arcp'] unless arcp == Arcp::PROTOCOL_VERSION raise Arcp::Errors::InvalidRequest, "unsupported arcp version: #{arcp.inspect}" end type = h['type'] raise Arcp::Errors::InvalidRequest, 'envelope type must be a String' unless type.is_a?(String) session_id = h['session_id'] unless session_id.is_a?(String) raise Arcp::Errors::InvalidRequest, 'envelope session_id must be a String' end event_seq = h['event_seq'] unless event_seq.nil? || event_seq.is_a?(Integer) raise Arcp::Errors::InvalidRequest, 'event_seq must be an Integer' end trace_id = h['trace_id'] raise Arcp::Errors::InvalidRequest, 'trace_id must be 32 hex chars' if trace_id && trace_id !~ HEX32 payload = h['payload'] raise Arcp::Errors::InvalidRequest, 'payload must be a Hash' unless payload.is_a?(Hash) || payload.nil? new( arcp: arcp, id: h.fetch('id'), type: type, session_id: session_id, trace_id: trace_id, job_id: h['job_id'], event_seq: event_seq, payload: deep_freeze(payload || {}) ) end |
.from_json(bytes) ⇒ Object
75 76 77 |
# File 'lib/arcp/envelope.rb', line 75 def self.from_json(bytes) from_h(Arcp::Serializer.load(bytes)) end |
Instance Method Details
#known? ⇒ Boolean
108 |
# File 'lib/arcp/envelope.rb', line 108 def known? = Arcp::MessageTypes.known?(type) |
#stringify(value) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/arcp/envelope.rb', line 100 def stringify(value) case value when Hash then value.transform_keys(&:to_s).transform_values { |v| stringify(v) } when Array then value.map { |v| stringify(v) } else value end end |
#to_h ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/arcp/envelope.rb', line 89 def to_h h = { 'arcp' => arcp, 'id' => id, 'type' => type, 'session_id' => session_id, 'payload' => stringify(payload) } h['trace_id'] = trace_id if trace_id h['job_id'] = job_id if job_id h['event_seq'] = event_seq if event_seq h end |
#to_json(*_args) ⇒ Object
98 |
# File 'lib/arcp/envelope.rb', line 98 def to_json(*_args) = Arcp::Serializer.dump(to_h) |