Class: ActiveMatrix::ConnectionRegistry
- Inherits:
-
Object
- Object
- ActiveMatrix::ConnectionRegistry
- 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
-
#clear_clients! ⇒ Object
Clear all cached clients (useful for testing).
-
#client(name = :primary) ⇒ ActiveMatrix::Client
Get or create a client for a named connection.
-
#connection(name = :primary) ⇒ Hash
Get connection configuration by name.
-
#connection_exists?(name) ⇒ Boolean
Check if a connection exists.
-
#connection_names ⇒ Array<String>
List all available connection names.
-
#initialize ⇒ ConnectionRegistry
constructor
A new instance of ConnectionRegistry.
-
#load!(path) ⇒ Object
Load connections from YAML file.
-
#reload!(path = nil) ⇒ Object
Reload configuration from file.
Constructor Details
#initialize ⇒ ConnectionRegistry
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
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
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
66 67 68 |
# File 'lib/active_matrix/connection_registry.rb', line 66 def connection_exists?(name) @connections.key?(name.to_s) end |
#connection_names ⇒ Array<String>
List all available connection names
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
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
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 |