Class: Ask::SessionProtocol::Events::Event

Inherits:
Data
  • Object
show all
Defined in:
lib/ask/session_protocol/events.rb

Overview

An event on the wire. Immutable; validate on construction.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, seq:, payload: {}) ⇒ Event

Returns a new instance of Event.

Parameters:

  • canonical event type (see Events::TYPES)

  • monotonic per-session sequence number (> 0)

  • (defaults to: {})

    string-keyed event body

Raises:



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ask/session_protocol/events.rb', line 210

def initialize(type:, seq:, payload: {})
  unless Events.known?(type)
    raise ArgumentError, "unknown session event type: #{type.inspect}"
  end
  unless seq.is_a?(Integer) && seq.positive?
    raise ArgumentError, "event seq must be a positive integer, got: #{seq.inspect}"
  end
  raise ArgumentError, "event payload must be a Hash, got: #{payload.class}" unless payload.is_a?(Hash)

  super(type: type, seq: seq, payload: payload.freeze)
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload

Returns:

  • the current value of payload



206
207
208
# File 'lib/ask/session_protocol/events.rb', line 206

def payload
  @payload
end

#seqObject (readonly)

Returns the value of attribute seq

Returns:

  • the current value of seq



206
207
208
# File 'lib/ask/session_protocol/events.rb', line 206

def seq
  @seq
end

#typeObject (readonly)

Returns the value of attribute type

Returns:

  • the current value of type



206
207
208
# File 'lib/ask/session_protocol/events.rb', line 206

def type
  @type
end

Instance Method Details

#to_hObject

Wire shape: { "type" => ..., "seq" => ..., "payload" => ... }



223
224
225
# File 'lib/ask/session_protocol/events.rb', line 223

def to_h
  { "type" => type, "seq" => seq, "payload" => payload }
end