Class: Async::Matrix::ApplicationService::Event
- Inherits:
-
Object
- Object
- Async::Matrix::ApplicationService::Event
- Defined in:
- lib/async/matrix/application_service/event.rb
Overview
Represents a Matrix event received via the Application Service API.
Provides typed accessors for all envelope fields plus schema-driven validation using the official Matrix spec YAML schemas.
event = Event.new(raw_hash)
event.type # => "m.room.message"
event.sender # => "@alice:example.org"
event.content.body # => "hello"
event.valid? # => true
event.valid! # => true (or raises Schema::ValidationError)
Instance Attribute Summary collapse
-
#content ⇒ Object
readonly
Returns the value of attribute content.
-
#event_id ⇒ Object
readonly
Returns the value of attribute event_id.
-
#origin_server_ts ⇒ Object
readonly
Returns the value of attribute origin_server_ts.
-
#raw ⇒ Object
readonly
Returns the value of attribute raw.
-
#room_id ⇒ Object
readonly
Returns the value of attribute room_id.
-
#sender ⇒ Object
readonly
Returns the value of attribute sender.
-
#state_key ⇒ Object
readonly
Returns the value of attribute state_key.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#unsigned ⇒ Object
readonly
Returns the value of attribute unsigned.
Instance Method Summary collapse
-
#content_properties ⇒ Array<String>
Content property names defined by the schema for this event type.
-
#initialize(data) ⇒ Event
constructor
A new instance of Event.
-
#schema ⇒ Object
The JSONSchemer::Schema for this event’s type, or nil if unknown.
-
#state_event? ⇒ Boolean
Is this a state event? (has a state_key).
-
#valid! ⇒ Object
Validate this event against its schema.
-
#valid? ⇒ Boolean
Validate this event against its schema.
Constructor Details
#initialize(data) ⇒ Event
Returns a new instance of Event.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/async/matrix/application_service/event.rb', line 76 def initialize(data) @raw = data @type = data["type"] @sender = data["sender"] @room_id = data["room_id"] @state_key = data["state_key"] @event_id = data["event_id"] @origin_server_ts = data["origin_server_ts"] @unsigned = data["unsigned"] @content = Content.new(data["content"] || {}) end |
Instance Attribute Details
#content ⇒ Object (readonly)
Returns the value of attribute content.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def content @content end |
#event_id ⇒ Object (readonly)
Returns the value of attribute event_id.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def event_id @event_id end |
#origin_server_ts ⇒ Object (readonly)
Returns the value of attribute origin_server_ts.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def origin_server_ts @origin_server_ts end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def raw @raw end |
#room_id ⇒ Object (readonly)
Returns the value of attribute room_id.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def room_id @room_id end |
#sender ⇒ Object (readonly)
Returns the value of attribute sender.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def sender @sender end |
#state_key ⇒ Object (readonly)
Returns the value of attribute state_key.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def state_key @state_key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def type @type end |
#unsigned ⇒ Object (readonly)
Returns the value of attribute unsigned.
73 74 75 |
# File 'lib/async/matrix/application_service/event.rb', line 73 def unsigned @unsigned end |
Instance Method Details
#content_properties ⇒ Array<String>
Content property names defined by the schema for this event type.
116 117 118 |
# File 'lib/async/matrix/application_service/event.rb', line 116 def content_properties Schema.content_properties(@type) end |
#schema ⇒ Object
The JSONSchemer::Schema for this event’s type, or nil if unknown.
89 90 91 |
# File 'lib/async/matrix/application_service/event.rb', line 89 def schema Schema[@type] end |
#state_event? ⇒ Boolean
Is this a state event? (has a state_key)
121 122 123 |
# File 'lib/async/matrix/application_service/event.rb', line 121 def state_event? !@state_key.nil? end |
#valid! ⇒ Object
Validate this event against its schema. Raises Schema::ValidationError with detailed errors on failure. Returns true if valid or if no schema exists.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/async/matrix/application_service/event.rb', line 102 def valid! errors = Schema.validate(@raw) unless errors.empty? raise Schema::ValidationError.new( errors, event_type: @type, event_id: @event_id ) end true end |