Class: ActionMessenger::Messenger

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

Overview

Represents a single instant messenger.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_hash = {}) ⇒ Messenger

Initialises the messenger. At the moment this class doesn't do anything with the config except store it into an attribute.


11
12
13
14
# File 'lib/action_messenger/messenger.rb', line 11

def initialize(config_hash = {})
  @config = config_hash
  @message_handlers = []
end

Instance Attribute Details

#configObject (readonly)

The configuration used to load this messenger.


7
8
9
# File 'lib/action_messenger/messenger.rb', line 7

def config
  @config
end

Class Method Details

.resolve(messenger) ⇒ Object

Resolves any object into a Messenger. If the object itself is a Messenger, the object itself is returned. Otherwise, it is converted to a string and that string is used to look it up in the registry.


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

def self.resolve(messenger)
  if messenger.is_a?(Messenger)
    messenger
  else
    MessengerRegistry.find_by_name(messenger.to_s)
  end
end

Instance Method Details

#add_message_handler(&block) ⇒ Object

Adds a message handler which will be called for any incoming messages.


17
18
19
# File 'lib/action_messenger/messenger.rb', line 17

def add_message_handler(&block)
  @message_handlers << block
end

#message_received(message) ⇒ Object

Called by subclasses when a message is received.


22
23
24
25
26
# File 'lib/action_messenger/messenger.rb', line 22

def message_received(message)
  @message_handlers.each do |block|
    block.call(message)
  end
end

#shutdownObject

Shuts down this messenger.

This implementation does nothing and is here for the convenience of subclasses that don't need special shutdown code.


43
44
# File 'lib/action_messenger/messenger.rb', line 43

def shutdown
end