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)


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
81
82
83
84
85
86
# File 'lib/matrix_sdk/util/uri.rb', line 28

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.



26
27
28
# File 'lib/matrix_sdk/util/uri.rb', line 26

def action
  @action
end

#authorityObject (readonly)

Returns the value of attribute authority.



26
27
28
# File 'lib/matrix_sdk/util/uri.rb', line 26

def authority
  @authority
end

#mxidObject (readonly)

Returns the value of attribute mxid.



26
27
28
# File 'lib/matrix_sdk/util/uri.rb', line 26

def mxid
  @mxid
end

#mxid2Object (readonly)

Returns the value of attribute mxid2.



26
27
28
# File 'lib/matrix_sdk/util/uri.rb', line 26

def mxid2
  @mxid2
end

#viaObject (readonly)

Returns the value of attribute via.



26
27
28
# File 'lib/matrix_sdk/util/uri.rb', line 26

def via
  @via
end

Instance Method Details

#mxid2?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/matrix_sdk/util/uri.rb', line 88

def mxid2?
  !@mxid2.nil?
end