Class: MatrixSdk::MXID

Inherits:
Object show all
Defined in:
lib/matrix_sdk/mxid.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier) ⇒ MXID

Returns a new instance of MXID.

Parameters:

  • identifier (String)

    The Matrix ID string in the format of ‘&<localpart>:<domain>’ where ‘&’ is the sigil

Raises:

  • (ArgumentError)


8
9
10
11
12
13
14
15
16
17
18
# 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+/]+)$}

  @sigil = identifier[0]
  @localpart, @domain, @port = identifier[1..-1].split(':')
  @port = @port.to_i if @port

  raise ArgumentError, 'Identifier is not a valid MXID' unless valid?
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



5
6
7
# File 'lib/matrix_sdk/mxid.rb', line 5

def domain
  @domain
end

#localpartObject

Returns the value of attribute localpart.



5
6
7
# File 'lib/matrix_sdk/mxid.rb', line 5

def localpart
  @localpart
end

#portObject

Returns the value of attribute port.



5
6
7
# File 'lib/matrix_sdk/mxid.rb', line 5

def port
  @port
end

#sigilObject

Returns the value of attribute sigil.



5
6
7
# File 'lib/matrix_sdk/mxid.rb', line 5

def sigil
  @sigil
end

Instance Method Details

#event?Boolean

Check if the ID is of a event

Returns:

  • (Boolean)

    if the ID is of the event_id type



92
93
94
# File 'lib/matrix_sdk/mxid.rb', line 92

def event?
  type == :event_id
end

#group?Boolean

Check if the ID is of a group

Returns:

  • (Boolean)

    if the ID is of the group_id type



80
81
82
# File 'lib/matrix_sdk/mxid.rb', line 80

def group?
  type == :group_id
end

#homeserverString

Gets the homeserver part of the ID

Examples:

A simple MXID

id = MXID.new('@alice:example.org')
id.homeserver
# => 'example.org'

A fully qualified MXID

id = MXID.new('@user:some.direct.domain:443')
id.homeserver
# => 'some.direct.domain:443'

Returns:

  • (String)


33
34
35
36
# File 'lib/matrix_sdk/mxid.rb', line 33

def homeserver
  port_s = port ? ':' + port.to_s : ''
  domain ? domain + port_s : ''
end

#homeserver_suffixObject

Gets the homserver part of the ID as a suffix (‘:homeserver’)



39
40
41
# File 'lib/matrix_sdk/mxid.rb', line 39

def homeserver_suffix
  ':' + homeserver if domain
end

#room?Boolean

Check if the ID is of a room

Returns:

  • (Boolean)

    if the ID is of the room_id or room_alias types



86
87
88
# File 'lib/matrix_sdk/mxid.rb', line 86

def room?
  type == :room_id || type == :room_alias
end

#room_alias?Boolean

Check if the ID is a room_alias

Returns:

  • (Boolean)

    if the ID is of the room_alias type



104
105
106
# File 'lib/matrix_sdk/mxid.rb', line 104

def room_alias?
  type == :room_alias
end

#room_id?Boolean

Check if the ID is a room_id

Returns:

  • (Boolean)

    if the ID is of the room_id type



98
99
100
# File 'lib/matrix_sdk/mxid.rb', line 98

def room_id?
  type == :room_id
end

#to_sObject



43
44
45
# File 'lib/matrix_sdk/mxid.rb', line 43

def to_s
  "#{sigil}#{localpart}#{homeserver_suffix}"
end

#typeSymbol

Returns the type of the ID

Returns:

  • (Symbol)

    The MXID type, one of (:user_id, :room_id, :event_id, :group_id, or :room_alias)



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/matrix_sdk/mxid.rb', line 50

def type
  case sigil
  when '@'
    :user_id
  when '!'
    :room_id
  when '$'
    :event_id
  when '+'
    :group_id
  when '#'
    :room_alias
  end
end

#user?Boolean

Check if the ID is of a user

Returns:

  • (Boolean)

    if the ID is of the user_id type



74
75
76
# File 'lib/matrix_sdk/mxid.rb', line 74

def user?
  type == :user_id
end

#valid?Boolean

Checks if the ID is valid

Returns:

  • (Boolean)

    If the ID is a valid Matrix ID



68
69
70
# File 'lib/matrix_sdk/mxid.rb', line 68

def valid?
  !type.nil?
end