Class: Async::Matrix::ApplicationService::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/async/matrix/application_service/event.rb

Overview

Wraps the content object of a Matrix event, providing typed access to schema-defined properties via method_missing.

Common fields like ‘msgtype`, `body`, and `membership` have dedicated accessors for convenience. Any other field defined in the event’s schema (or present in the raw hash) is accessible as a method call:

content.msgtype      # => "m.text"
content.body         # => "hello"
content.membership   # => "join"
content.avatar_url   # => "mxc://example.org/abc"
content["custom"]    # => direct hash access for non-standard fields

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Content

Returns a new instance of Content.



28
29
30
31
32
33
# File 'lib/async/matrix/application_service/event.rb', line 28

def initialize(data)
	@data       = data
	@msgtype    = data["msgtype"]
	@body       = data["body"]
	@membership = data["membership"]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Dynamic access to any content field present in the raw data.



43
44
45
46
47
48
49
50
# File 'lib/async/matrix/application_service/event.rb', line 43

def method_missing(name, *args)
	key = name.to_s
	if @data.key?(key)
		@data[key]
	else
		nil
	end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



26
27
28
# File 'lib/async/matrix/application_service/event.rb', line 26

def body
  @body
end

#membershipObject (readonly)

Returns the value of attribute membership.



26
27
28
# File 'lib/async/matrix/application_service/event.rb', line 26

def membership
  @membership
end

#msgtypeObject (readonly)

Returns the value of attribute msgtype.



26
27
28
# File 'lib/async/matrix/application_service/event.rb', line 26

def msgtype
  @msgtype
end

Instance Method Details

#[](key) ⇒ Object

Direct hash access for any content field.



40
# File 'lib/async/matrix/application_service/event.rb', line 40

def [](key) = @data[key.to_s]

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/async/matrix/application_service/event.rb', line 52

def respond_to_missing?(name, include_private = false)
	@data.key?(name.to_s) || super
end

#to_hObject



35
# File 'lib/async/matrix/application_service/event.rb', line 35

def to_h = @data

#to_sObject



36
# File 'lib/async/matrix/application_service/event.rb', line 36

def to_s = @body.to_s

#to_strObject



37
# File 'lib/async/matrix/application_service/event.rb', line 37

def to_str = to_s