Class: Cacophony::Notifier

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

Instance Method Summary collapse

Constructor Details

#initialize(config_hash_or_filename = nil) ⇒ Notifier

Returns a new instance of Notifier.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/cacophony.rb', line 12

def initialize(config_hash_or_filename = nil)
  if config_hash_or_filename.is_a?(Hash)
    @conf = config_hash_or_filename
  elsif config_hash_or_filename.nil? || config_hash_or_filename.is_a?(String)
    config_hash_or_filename ||= DEFAULT_CONF_PATH
    path = File.expand_path(config_hash_or_filename)
    begin 
      @conf = YAML.load_file(path)
    rescue 
      raise "#{$!} couldn't read Cacophony config file from #{path} "
    end

  else
    raise 'invalid config parameter.'
  end
end

Instance Method Details

#notify(title, message, data = []) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cacophony.rb', line 29

def notify(title, message, data = [])

  threads = []
  @conf.each do |notifier_type, opts|
    begin
      klass = Object.const_get(notifier_type.split('_').map(&:capitalize).join)
    rescue 
        raise "Invalid configuration option(s): #{notifier_type}"
    end

    threads << Thread.new do
      notifier = klass.new(opts || {} )
      puts "Cacophony broadcasting via #{notifier_type.split('_').map(&:capitalize).join(' ')}"
      notifier.notify(message, title, data)
    end
  end

  threads.each{|t| t.join}
end