Class: Async::Matrix::ApplicationService::Event

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#contentObject (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_idObject (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_tsObject (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

#rawObject (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_idObject (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

#senderObject (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_keyObject (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

#typeObject (readonly)

Returns the value of attribute type.



73
74
75
# File 'lib/async/matrix/application_service/event.rb', line 73

def type
  @type
end

#unsignedObject (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_propertiesArray<String>

Content property names defined by the schema for this event type.

Returns:

  • (Array<String>)


116
117
118
# File 'lib/async/matrix/application_service/event.rb', line 116

def content_properties
	Schema.content_properties(@type)
end

#schemaObject

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)

Returns:

  • (Boolean)


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

#valid?Boolean

Validate this event against its schema. Returns true if valid or if no schema exists (lenient).

Returns:

  • (Boolean)


95
96
97
# File 'lib/async/matrix/application_service/event.rb', line 95

def valid?
	Schema.valid?(@raw)
end