Class: Eye::Notify

Inherits:
Object show all
Includes:
Celluloid, Dsl::Validation
Defined in:
lib/eye/notify.rb

Direct Known Subclasses

Custom, Jabber, Mail

Defined Under Namespace

Classes: Custom, Jabber, Mail

Constant Summary collapse

TYPES =
{:mail => 'Mail', :jabber => 'Jabber'}
TIMEOUT =
1.minute

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

included

Constructor Details

#initialize(options = {}, message_h = {}) ⇒ Notify

Returns a new instance of Notify.



53
54
55
56
57
58
# File 'lib/eye/notify.rb', line 53

def initialize(options = {}, message_h = {})
  @message_h = message_h
  @options = options

  debug "created notifier #{options}"
end

Class Method Details

.depends_onObject



98
99
# File 'lib/eye/notify.rb', line 98

def self.depends_on
end

.get_class(type) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/eye/notify.rb', line 10

def self.get_class(type)
  klass = eval("Eye::Notify::#{TYPES[type]}") rescue nil
  raise "unknown notifier :#{type}" unless klass
  if deps = klass.depends_on
    Array(deps).each { |d| require d }
  end
  klass
end

.notify(contact, message_h) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/eye/notify.rb', line 23

def self.notify(contact, message_h)
  contact = contact.to_s
  settings = Eye::Control.settings
  needed_hash = (settings[:contacts] || {})[contact]

  if needed_hash.blank?
    error "contact #{contact} not found; check your configuration"
    return
  end

  create_proc = lambda do |nh|
    type = nh[:type]
    config = (settings[type] || {}).merge(nh[:opts] || {}).merge(:contact => nh[:contact])
    klass = get_class(type)
    notify = klass.new(config, message_h)
    notify.async_notify if notify
  end

  if needed_hash.is_a?(Array)
    needed_hash.each{|nh| create_proc[nh] }
  else
    create_proc[needed_hash]
  end

rescue Exception, Timeout::Error => ex
  log_ex(ex)
end

.register(base) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/eye/notify.rb', line 90

def self.register(base)
  name = base.to_s.gsub('Eye::Notify::', '')
  type = name.underscore.to_sym
  Eye::Notify::TYPES[type] = name
  Eye::Notify.const_set(name, base)
  Eye::Dsl::ConfigOpts.add_notify(type)
end

.validate!(options) ⇒ Object



19
20
21
# File 'lib/eye/notify.rb', line 19

def self.validate!(options)
  get_class(options[:type]).validate(options)
end

Instance Method Details

#async_notifyObject



64
65
66
67
# File 'lib/eye/notify.rb', line 64

def async_notify
  async.notify
  after(TIMEOUT){ terminate }
end

#executeObject

Raises:

  • (NotImplementedError)


76
77
78
# File 'lib/eye/notify.rb', line 76

def execute
  raise NotImplementedError
end

#logger_sub_tagObject



60
61
62
# File 'lib/eye/notify.rb', line 60

def logger_sub_tag
  @options[:contact]
end

#message_bodyObject



86
87
88
# File 'lib/eye/notify.rb', line 86

def message_body
  "#{message_subject} at #{Eye::Utils.human_time2(msg_at)}"
end

#message_subjectObject



82
83
84
# File 'lib/eye/notify.rb', line 82

def message_subject
  "[#{msg_host}] [#{msg_full_name}] #{msg_message}"
end

#notifyObject



69
70
71
72
73
74
# File 'lib/eye/notify.rb', line 69

def notify
  debug "start notify #{@message_h}"
  execute
  debug "end notify #{@message_h}"
  terminate
end