Class: Async::Matrix::ApplicationService::Content
- Inherits:
-
Object
- Object
- Async::Matrix::ApplicationService::Content
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#membership ⇒ Object
readonly
Returns the value of attribute membership.
-
#msgtype ⇒ Object
readonly
Returns the value of attribute msgtype.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Direct hash access for any content field.
-
#initialize(data) ⇒ Content
constructor
A new instance of Content.
-
#method_missing(name, *args) ⇒ Object
Dynamic access to any content field present in the raw data.
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
-
#to_h ⇒ Object
Returns the raw content hash.
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.
46 47 48 49 50 51 52 53 |
# File 'lib/async/matrix/application_service/event.rb', line 46 def method_missing(name, *args) key = name.to_s if @data.key?(key) @data[key] else nil end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
26 27 28 |
# File 'lib/async/matrix/application_service/event.rb', line 26 def body @body end |
#membership ⇒ Object (readonly)
Returns the value of attribute membership.
26 27 28 |
# File 'lib/async/matrix/application_service/event.rb', line 26 def membership @membership end |
#msgtype ⇒ Object (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.
36 37 38 |
# File 'lib/async/matrix/application_service/event.rb', line 36 def [](key) @data[key.to_s] end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
55 56 57 |
# File 'lib/async/matrix/application_service/event.rb', line 55 def respond_to_missing?(name, include_private = false) @data.key?(name.to_s) || super end |
#to_h ⇒ Object
Returns the raw content hash.
41 42 43 |
# File 'lib/async/matrix/application_service/event.rb', line 41 def to_h @data end |