Class: ActiveMatrix::ConnectionRegistry

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/active_matrix/connection_registry.rb

Overview

Registry for named Matrix connections loaded from config/active_matrix.yml

Connections are defined in YAML with ERB support for secrets:

primary:
  homeserver_url: <%= ENV['MATRIX_HOMESERVER_URL'] %>
  access_token: <%= ENV['MATRIX_ACCESS_TOKEN'] %>

Usage:

ActiveMatrix.connection(:primary)  # Returns connection config hash
ActiveMatrix.client(:primary)      # Returns authenticated Client

Defined Under Namespace

Classes: ConnectionNotFound

Instance Method Summary collapse

Constructor Details

#initializeConnectionRegistry

Returns a new instance of ConnectionRegistry.



25
26
27
28
29
# File 'lib/active_matrix/connection_registry.rb', line 25

def initialize
  @connections = {}
  @clients = {}
  @mutex = Mutex.new
end

Instance Method Details

#clear_clients!Object

Clear all cached clients (useful for testing)



77
78
79
80
81
82
83
84
85
86
# File 'lib/active_matrix/connection_registry.rb', line 77

def clear_clients!
  @mutex.synchronize do
    @clients.each_value do |client|
      client.logout if client.logged_in?
    rescue StandardError
      # Ignore cleanup errors
    end
    @clients.clear
  end
end

#client(name = :primary) ⇒ ActiveMatrix::Client

Get or create a client for a named connection

Parameters:

  • name (Symbol, String) (defaults to: :primary)

    connection name (default: :primary)

Returns:



55
56
57
58
59
60
61
# File 'lib/active_matrix/connection_registry.rb', line 55

def client(name = :primary)
  name = name.to_s

  @mutex.synchronize do
    @clients[name] ||= build_client(name)
  end
end

#connection(name = :primary) ⇒ Hash

Get connection configuration by name

Parameters:

  • name (Symbol, String) (defaults to: :primary)

    connection name (default: :primary)

Returns:

  • (Hash)

    connection configuration

Raises:



44
45
46
47
48
49
50
# File 'lib/active_matrix/connection_registry.rb', line 44

def connection(name = :primary)
  name = name.to_s
  config = @connections[name]
  raise ConnectionNotFound, "Connection '#{name}' not found in config/active_matrix.yml" unless config

  config.symbolize_keys
end

#connection_exists?(name) ⇒ Boolean

Check if a connection exists

Parameters:

  • name (Symbol, String)

    connection name

Returns:

  • (Boolean)


66
67
68
# File 'lib/active_matrix/connection_registry.rb', line 66

def connection_exists?(name)
  @connections.key?(name.to_s)
end

#connection_namesArray<String>

List all available connection names

Returns:

  • (Array<String>)


72
73
74
# File 'lib/active_matrix/connection_registry.rb', line 72

def connection_names
  @connections.keys - ['default']
end

#load!(path) ⇒ Object

Load connections from YAML file

Parameters:

  • path (String, Pathname)

    path to YAML config file



33
34
35
36
37
38
# File 'lib/active_matrix/connection_registry.rb', line 33

def load!(path)
  @mutex.synchronize do
    @connections = load_yaml(path)
    @clients.clear # Clear cached clients when config reloads
  end
end

#reload!(path = nil) ⇒ Object

Reload configuration from file

Parameters:

  • path (String, Pathname) (defaults to: nil)

    path to YAML config file



90
91
92
93
94
# File 'lib/active_matrix/connection_registry.rb', line 90

def reload!(path = nil)
  path ||= default_config_path
  clear_clients!
  load!(path)
end