Class: URI::MATRIX

Inherits:
Generic show all
Defined in:
lib/matrix_sdk/util/uri.rb

Overview

A matrix: URI according to MSC2312

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MATRIX

Returns a new instance of MATRIX.

Raises:

  • (InvalidComponentError)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/matrix_sdk/util/uri.rb', line 22

def initialize(*args)
  super(*args)

  @action = nil
  @authority = nil
  @mxid = nil
  @mxid2 = nil
  @via = nil

  raise InvalidComponentError, 'missing opaque part for matrix URL' if !@opaque && !@path

  if @path
    @authority = @host
    @authority += ":#{@port}" if @port
  else
    @path, @query = @opaque.split('?')
    @query, @fragment = @query.split('#') if @query&.include? '#'
    @path, @fragment = @path.split('#') if @path&.include? '#'
    @path = "/#{path}"
    @opaque = nil
  end

  components = @path.delete_prefix('/').split('/', -1)
  raise InvalidComponentError, 'component count must be 2 or 4' if components.size != 2 && components.size != 4

  sigil = case components.shift
          when 'u', 'user'
            '@'
          when 'r', 'room'
            '#'
          when 'roomid'
            '!'
          else
            raise InvalidComponentError, 'invalid component in path'
          end

  component = components.shift
  raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?

  @mxid = MatrixSdk::MXID.new("#{sigil}#{component}")

  if components.size == 2
    sigil2 = case components.shift
             when 'e', 'event'
               '$'
             else
               raise InvalidComponentError, 'invalid component in path'
             end
    component = components.shift
    raise InvalidComponentError, "component can't be empty" if component.nil? || component.empty?

    @mxid2 = MatrixSdk::MXID.new("#{sigil2}#{component}")
  end

  return unless @query

  @action = @query.match(/action=([^&]+)/)&.captures&.first&.to_sym
  @via = @query.scan(/via=([^&]+)/)&.flatten&.compact
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



20
21
22
# File 'lib/matrix_sdk/util/uri.rb', line 20

def action
  @action
end

#authorityObject (readonly)

Returns the value of attribute authority.



20
21
22
# File 'lib/matrix_sdk/util/uri.rb', line 20

def authority
  @authority
end

#mxidObject (readonly)

Returns the value of attribute mxid.



20
21
22
# File 'lib/matrix_sdk/util/uri.rb', line 20

def mxid
  @mxid
end

#mxid2Object (readonly)

Returns the value of attribute mxid2.



20
21
22
# File 'lib/matrix_sdk/util/uri.rb', line 20

def mxid2
  @mxid2
end

#viaObject (readonly)

Returns the value of attribute via.



20
21
22
# File 'lib/matrix_sdk/util/uri.rb', line 20

def via
  @via
end

Instance Method Details

#mxid2?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/matrix_sdk/util/uri.rb', line 82

def mxid2?
  !@mxid2.nil?
end