Class: ActionMessenger::MessengerRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/action_messenger/messenger_registry.rb

Overview

A repository of preconfigured messengers loaded from YAML configuration.

Defined Under Namespace

Classes: NoConfigFileError, NoSuchMessengerError, NoSuchMessengerTypeError

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMessengerRegistry

Constructs a new registry.


43
44
45
46
47
48
49
50
51
52
53
# File 'lib/action_messenger/messenger_registry.rb', line 43

def initialize
  @config_file = if defined?(RAILS_ROOT)
                   RAILS_ROOT + "/config/messenger.yml"
                 else
                   "messenger.yml"
                 end
                 
  @config = nil
  @messengers = {}
  @mock_messengers = (defined?(RAILS_ENV) and RAILS_ENV == 'test')
end

Class Attribute Details

.shared_instanceObject

Purely for the sake of unit testing.


39
40
41
# File 'lib/action_messenger/messenger_registry.rb', line 39

def shared_instance
  @shared_instance
end

Instance Attribute Details

#config_fileObject

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.


23
24
25
# File 'lib/action_messenger/messenger_registry.rb', line 23

def config_file
  @config_file
end

#messengersObject (readonly)

All currently-active messengers.


26
27
28
# File 'lib/action_messenger/messenger_registry.rb', line 26

def messengers
  @messengers
end

#mock_messengersObject

Set this to always return mocks. Set by default when running in Rails testing mode.


18
19
20
# File 'lib/action_messenger/messenger_registry.rb', line 18

def mock_messengers
  @mock_messengers
end

Class Method Details

.method_missing(method, *params, &block) ⇒ Object

:nodoc:


31
32
33
34
35
36
# File 'lib/action_messenger/messenger_registry.rb', line 31

def method_missing(method, *params, &block) #:nodoc:
  if @shared_instance.nil?
    @shared_instance = MessengerRegistry.new
  end
  @shared_instance.send(method, *params, &block)
end

Instance Method Details

#add_config(config_hashes) ⇒ Object

Adds extra configuration in hash form.


67
68
69
70
71
72
# File 'lib/action_messenger/messenger_registry.rb', line 67

def add_config(config_hashes)
  if @config.nil?
    @config = {}
  end
  @config.merge!(config_hashes)
end

#configObject

Gets the messenger config.

If the config hasn't been initialised already, then the config is loaded from a file.


59
60
61
62
63
64
# File 'lib/action_messenger/messenger_registry.rb', line 59

def config
  if @config.nil?
    load_config
  end
  @config
end

#create_from_config(config_hash) ⇒ Object

Creates a messenger from its configuration hash.


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/action_messenger/messenger_registry.rb', line 89

def create_from_config(config_hash)
  if mock_messengers
    Messengers::MockMessenger.new
  else
    case config_hash['type']
      when 'mock' then Messengers::MockMessenger.new
      when 'xmpp4r' then Messengers::Xmpp4rMessenger.new(config_hash)
      else raise NoSuchMessengerTypeError, "Unknown messenger type: #{config_hash['type']}"
    end
  end
end

#find_by_name(name) ⇒ Object

Finds a messenger by its name. Creates the messenger on-demand if the messenger hasn't been initialised yet.


103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/action_messenger/messenger_registry.rb', line 103

def find_by_name(name)
  name = name.to_s
  messenger = messengers[name]
  if messenger.nil?
    config_hash = config[name]
    if config_hash.nil?
      raise NoSuchMessengerError, "No configuration for name #{name}"
    end
    messengers[name] = messenger = create_from_config(config_hash)
  end
  messenger
end

#load_config(config_file = @config_file) ⇒ Object

Loads (potentially additional) config from the given filename.


75
76
77
78
79
80
81
# File 'lib/action_messenger/messenger_registry.rb', line 75

def load_config(config_file = @config_file)
  unless File.exists?(config_file)
    raise NoConfigFileError, "Config file doesn't exist: #{config_file}"
  end

  add_config(YAML.load(ERB.new(IO.read(config_file)).result))
end

#register(name, messenger) ⇒ Object

Registers a messenger by name. This will probably only be used from unit tests.


84
85
86
# File 'lib/action_messenger/messenger_registry.rb', line 84

def register(name, messenger)
  messengers[name.to_s] = messenger
end