Class: Snoopit::NotificationManager

Inherits:
Object
  • Object
show all
Defined in:
lib/snoopit/notification_manager.rb

Overview

Instantiates all specified Notifiers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ NotificationManager

Returns a new instance of NotificationManager.



8
9
10
11
12
# File 'lib/snoopit/notification_manager.rb', line 8

def initialize(config=nil)
  @active = {}
  @config = config
  load_default_notifiers unless @config.nil?
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



6
7
8
# File 'lib/snoopit/notification_manager.rb', line 6

def active
  @active
end

Instance Method Details

#get_notifier(name) ⇒ Object



40
41
42
# File 'lib/snoopit/notification_manager.rb', line 40

def get_notifier(name)
  @active[name]
end

#load_notifier_config(config) ⇒ Object

Load default notifiers and configured notifiers



15
16
17
18
19
20
# File 'lib/snoopit/notification_manager.rb', line 15

def load_notifier_config(config)
  @config = config
  load_default_notifiers
  load = @config['load']
  load_files(load) unless load.nil?
end

#notify(snoopies) ⇒ Object

Invoke all notifications that this list of snoopies is subscribed to via the snooper.json

Parameters:



46
47
48
49
50
51
52
# File 'lib/snoopit/notification_manager.rb', line 46

def notify(snoopies)
  snoopies.each do |snoopy|
    snoopy.sniffers.each do |sniffer|
      sniffer_notify(sniffer)
    end
  end
end

#register(notifier) ⇒ Object

Register the specified Notifier

Parameters:



24
25
26
27
28
# File 'lib/snoopit/notification_manager.rb', line 24

def register(notifier)
  raise NameError.new "Notifier missing valid name: #{notifier.inspect}" if notifier.name.nil?
  Snoopit.logger.debug "Registering notifier #{notifier.name}"
  @active[notifier.name] = notifier
end

#sniffer_notify(sniffer) ⇒ Object

Snoopies use sniffers that may or may not be associated with a notifier

Parameters:



56
57
58
59
60
61
62
63
64
65
# File 'lib/snoopit/notification_manager.rb', line 56

def sniffer_notify(sniffer)
  messages = get_sniffed_messages sniffer
  sniffer.notifiers.each do |key, value|
    n = @active[key]
    next if n.nil?
    messages.each do |message|
      n.notify(message, value)
    end
  end
end

#unregister(notifier) ⇒ Object

Unregister the specified Notifier

Parameters:



32
33
34
# File 'lib/snoopit/notification_manager.rb', line 32

def unregister(notifier)
  self.unregister_by_name notifier.name
end

#unregister_by_name(notifier_name) ⇒ Object



36
37
38
# File 'lib/snoopit/notification_manager.rb', line 36

def unregister_by_name(notifier_name)
  @active.delete notifier_name
end