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.
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/async/matrix/application_service/event.rb', line 73 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.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def content @content end |
#event_id ⇒ Object (readonly)
Returns the value of attribute event_id.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def event_id @event_id end |
#origin_server_ts ⇒ Object (readonly)
Returns the value of attribute origin_server_ts.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def origin_server_ts @origin_server_ts end |
#raw ⇒ Object (readonly)
Returns the value of attribute raw.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def raw @raw end |
#room_id ⇒ Object (readonly)
Returns the value of attribute room_id.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def room_id @room_id end |
#sender ⇒ Object (readonly)
Returns the value of attribute sender.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def sender @sender end |
#state_key ⇒ Object (readonly)
Returns the value of attribute state_key.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def state_key @state_key end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def type @type end |
#unsigned ⇒ Object (readonly)
Returns the value of attribute unsigned.
70 71 72 |
# File 'lib/async/matrix/application_service/event.rb', line 70 def unsigned @unsigned end |
Instance Method Details
#content_properties ⇒ Array<String>
Content property names defined by the schema for this event type.
109 |
# File 'lib/async/matrix/application_service/event.rb', line 109 def content_properties = Schema.content_properties(@type) |
#schema ⇒ Object
The JSONSchemer::Schema for this event’s type, or nil if unknown.
86 |
# File 'lib/async/matrix/application_service/event.rb', line 86 def schema = Schema[@type] |
#state_event? ⇒ Boolean
Is this a state event? (has a state_key)
112 |
# File 'lib/async/matrix/application_service/event.rb', line 112 def state_event? = !@state_key.nil? |
#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.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/async/matrix/application_service/event.rb', line 95 def valid! errors = Schema.validate(@raw) unless errors.empty? raise Schema::ValidationError.new( errors, event_type: @type, event_id: @event_id ) end true end |