| Class | ActionMessenger::MessengerRegistry |
| In: |
lib/action_messenger/messenger_registry.rb
|
| Parent: | Object |
A repository of preconfigured messengers loaded from YAML configuration.
| config_file | [RW] | The config file. Can be set before anything else is called to override the location which configuration is loaded from. Setting it after the configuration is already loaded will have no effect. |
| messengers | [R] | All currently-active messengers. |
| mock_messengers | [RW] | Set this to always return mocks. Set by default when running in Rails testing mode. |
| shared_instance | [RW] | Purely for the sake of unit testing. |
Constructs a new registry.
# File lib/action_messenger/messenger_registry.rb, line 43
43: def initialize
44: @config_file = if defined?(RAILS_ROOT)
45: RAILS_ROOT + "/config/messenger.yml"
46: else
47: "messenger.yml"
48: end
49:
50: @config = nil
51: @messengers = {}
52: @mock_messengers = (defined?(RAILS_ENV) and RAILS_ENV == 'test')
53: end
Adds extra configuration in hash form.
# File lib/action_messenger/messenger_registry.rb, line 67
67: def add_config(config_hashes)
68: if @config.nil?
69: @config = {}
70: end
71: @config.merge!(config_hashes)
72: end
Creates a messenger from its configuration hash.
# File lib/action_messenger/messenger_registry.rb, line 88
88: def create_from_config(config_hash)
89: if mock_messengers
90: Messengers::MockMessenger.new
91: else
92: case config_hash['type']
93: when 'mock' then Messengers::MockMessenger.new
94: when 'xmpp4r' then Messengers::Xmpp4rMessenger.new(config_hash)
95: else raise NoSuchMessengerTypeError, "Unknown messenger type: #{config_hash['type']}"
96: end
97: end
98: end
Finds a messenger by its name. Creates the messenger on-demand if the messenger hasn‘t been initialised yet.
# File lib/action_messenger/messenger_registry.rb, line 102
102: def find_by_name(name)
103: name = name.to_s
104: messenger = messengers[name]
105: if messenger.nil?
106: config_hash = config[name]
107: if config_hash.nil?
108: raise NoSuchMessengerError, "No configuration for name #{name}"
109: end
110: messengers[name] = messenger = create_from_config(config_hash)
111: end
112: messenger
113: end
Loads (potentially additional) config from the given filename.
# File lib/action_messenger/messenger_registry.rb, line 75
75: def load_config(config_file = @config_file)
76: unless File.exists?(config_file)
77: raise NoConfigFileError, "Config file doesn't exist: #{config_file}"
78: end
79: add_config(YAML.load_file(config_file))
80: end