Class: MatrixSdk::MXID
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
-
#localpart ⇒ Object
Returns the value of attribute localpart.
-
#port ⇒ Object
Returns the value of attribute port.
-
#sigil ⇒ Object
Returns the value of attribute sigil.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Check if two MXIDs are equal.
-
#event? ⇒ Boolean
Check if the ID is of a event.
-
#group? ⇒ Boolean
Check if the ID is of a group.
-
#homeserver ⇒ String
Gets the homeserver part of the ID.
-
#homeserver_suffix ⇒ Object
Gets the homserver part of the ID as a suffix (‘:homeserver’).
-
#initialize(identifier) ⇒ MXID
constructor
A new instance of MXID.
-
#room? ⇒ Boolean
Check if the ID is of a room.
-
#room_alias? ⇒ Boolean
Check if the ID is a room_alias.
-
#room_id? ⇒ Boolean
Check if the ID is a room_id.
- #to_s ⇒ Object (also: #to_str)
-
#to_uri(event_id: nil, action: nil, via: nil) ⇒ Object
Converts the MXID to a matrix: URI according to MSC2312.
-
#type ⇒ Symbol
Returns the type of the ID.
-
#user? ⇒ Boolean
Check if the ID is of a user.
-
#valid? ⇒ Boolean
Checks if the ID is valid.
Constructor Details
#initialize(identifier) ⇒ MXID
Returns a new instance of MXID.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/matrix_sdk/mxid.rb', line 8 def initialize(identifier) raise ArgumentError, 'Identifier must be a String' unless identifier.is_a? String raise ArgumentError, 'Identifier is too long' if identifier.size > 255 raise ArgumentError, 'Identifier lacks required data' unless identifier =~ %r{^([@!$+#][^:]+:[^:]+(?::\d+)?)|(\$[A-Za-z0-9+/]+)$} # TODO: Community-as-a-Room / Profile-as-a-Room, in case they're going for room aliases @sigil = identifier[0] @localpart, @domain, @port = identifier[1..].split(':') @port = @port.to_i if @port raise ArgumentError, 'Identifier is not a valid MXID' unless valid? end |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
5 6 7 |
# File 'lib/matrix_sdk/mxid.rb', line 5 def domain @domain end |
#localpart ⇒ Object
Returns the value of attribute localpart.
5 6 7 |
# File 'lib/matrix_sdk/mxid.rb', line 5 def localpart @localpart end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/matrix_sdk/mxid.rb', line 5 def port @port end |
#sigil ⇒ Object
Returns the value of attribute sigil.
5 6 7 |
# File 'lib/matrix_sdk/mxid.rb', line 5 def sigil @sigil end |
Instance Method Details
#==(other) ⇒ Boolean
Check if two MXIDs are equal
142 143 144 |
# File 'lib/matrix_sdk/mxid.rb', line 142 def ==(other) to_s == other.to_s end |
#event? ⇒ Boolean
Check if the ID is of a event
90 91 92 |
# File 'lib/matrix_sdk/mxid.rb', line 90 def event? type == :event_id end |
#group? ⇒ Boolean
Check if the ID is of a group
78 79 80 |
# File 'lib/matrix_sdk/mxid.rb', line 78 def group? type == :group_id end |
#homeserver ⇒ String
Gets the homeserver part of the ID
34 35 36 37 |
# File 'lib/matrix_sdk/mxid.rb', line 34 def homeserver port_s = port ? ":#{port}" : '' domain ? domain + port_s : '' end |
#homeserver_suffix ⇒ Object
Gets the homserver part of the ID as a suffix (‘:homeserver’)
40 41 42 |
# File 'lib/matrix_sdk/mxid.rb', line 40 def homeserver_suffix ":#{homeserver}" if domain end |
#room? ⇒ Boolean
Check if the ID is of a room
84 85 86 |
# File 'lib/matrix_sdk/mxid.rb', line 84 def room? type == :room_id || type == :room_alias end |
#room_alias? ⇒ Boolean
Check if the ID is a room_alias
102 103 104 |
# File 'lib/matrix_sdk/mxid.rb', line 102 def room_alias? type == :room_alias end |
#room_id? ⇒ Boolean
Check if the ID is a room_id
96 97 98 |
# File 'lib/matrix_sdk/mxid.rb', line 96 def room_id? type == :room_id end |
#to_s ⇒ Object Also known as: to_str
44 45 46 |
# File 'lib/matrix_sdk/mxid.rb', line 44 def to_s "#{sigil}#{localpart}#{homeserver_suffix}" end |
#to_uri(event_id: nil, action: nil, via: nil) ⇒ Object
Converts the MXID to a matrix: URI according to MSC2312
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/matrix_sdk/mxid.rb', line 111 def to_uri(event_id: nil, action: nil, via: nil) uri = '' case sigil when '@' raise ArgumentError, "can't provide via for user URIs" if via raise ArgumentError, "can't provide event_id for user URIs" if event_id uri += 'u' when '#' uri += 'r' when '!' uri += 'roomid' else raise ArgumentError, "this MXID can't be converted to a URI" end uri = "matrix:#{uri}/#{localpart}#{homeserver_suffix}" uri += "/e/#{event_id.to_s.delete_prefix('$')}" if event_id query = [] query << "action=#{action}" if action [via].flatten.compact.each { |v| query << "via=#{v}" } uri += "?#{query.join('&')}" unless query.empty? URI(uri) end |
#type ⇒ Symbol
Returns the type of the ID
53 54 55 56 57 58 59 60 61 |
# File 'lib/matrix_sdk/mxid.rb', line 53 def type { '@' => :user_id, '!' => :room_id, '$' => :event_id, '+' => :group_id, '#' => :room_alias }[sigil] end |
#user? ⇒ Boolean
Check if the ID is of a user
72 73 74 |
# File 'lib/matrix_sdk/mxid.rb', line 72 def user? type == :user_id end |
#valid? ⇒ Boolean
Checks if the ID is valid
66 67 68 |
# File 'lib/matrix_sdk/mxid.rb', line 66 def valid? !type.nil? end |