Method: MatrixSdk::Api.new_for_domain

Defined in:
lib/matrix_sdk/api.rb

.new_for_domain(domain, target: :client, keep_wellknown: false, ssl: true, **params) ⇒ API

Create an API connection to a domain entry

This will follow the server discovery spec for client-server and federation

Examples:

Opening a Matrix API connection to a homeserver

hs = MatrixSdk::API.new_for_domain 'example.com'
hs.connection_address
# => 'matrix.example.com'
hs.connection_port
# => 443

Parameters:

  • domain (String)

    The domain to set up the API connection for, can contain a ‘:’ to denote a port

  • target (:client, :identity, :server) (defaults to: :client)

    The target for the domain lookup

  • keep_wellknown (Boolean) (defaults to: false)

    Should the .well-known response be kept for further handling

  • params (Hash)

    Additional options to pass to .new

Returns:

  • (API)

    The API connection



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/matrix_sdk/api.rb', line 91

def self.new_for_domain(domain, target: :client, keep_wellknown: false, ssl: true, **params)
  domain, port = domain.split(':')
  uri = URI("http#{ssl ? 's' : ''}://#{domain}")
  well_known = nil
  target_uri = nil

  if !port.nil? && !port.empty?
    target_uri = URI("https://#{domain}:#{port}")
  elsif target == :server
    # Attempt SRV record discovery
    target_uri = begin
                   require 'resolv'
                   resolver = Resolv::DNS.new
                   resolver.getresource("_matrix._tcp.#{domain}")
                 rescue StandardError
                   nil
                 end

    if target_uri.nil?
      well_known = begin
                     data = Net::HTTP.get("https://#{domain}/.well-known/matrix/server")
                     JSON.parse(data)
                   rescue StandardError
                     nil
                   end

      target_uri = well_known['m.server'] if well_known&.key?('m.server')
    else
      target_uri = URI("https://#{target_uri.target}:#{target_uri.port}")
    end
  elsif i[client identity].include? target
    # Attempt .well-known discovery
    well_known = begin
                   data = Net::HTTP.get("https://#{domain}/.well-known/matrix/client")
                   JSON.parse(data)
                 rescue StandardError
                   nil
                 end

    if well_known
      key = 'm.homeserver'
      key = 'm.identity_server' if target == :identity

      if well_known.key?(key) && well_known[key].key?('base_url')
        uri = URI(well_known[key]['base_url'])
        target_uri = uri
      end
    end
  end

  # Fall back to direct domain connection
  target_uri ||= URI("https://#{domain}:8448")

  params[:well_known] = well_known if keep_wellknown

  new(uri,
      params.merge(
        address: target_uri.host,
        port: target_uri.port
      ))
end