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 * 60

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.



55
56
57
58
59
60
# File 'lib/eye/notify.rb', line 55

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

  debug "created notifier #{options}"
end

Class Method Details

.get_class(type) ⇒ Object



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

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

.notify(contact, message_h) ⇒ Object



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
50
51
# File 'lib/eye/notify.rb', line 25

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_link(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



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

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

.requiresObject



100
101
# File 'lib/eye/notify.rb', line 100

def self.requires
end

.validate!(options) ⇒ Object



21
22
23
# File 'lib/eye/notify.rb', line 21

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

Instance Method Details

#async_notifyObject



66
67
68
69
# File 'lib/eye/notify.rb', line 66

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

#executeObject

Raises:

  • (NotImplementedError)


78
79
80
# File 'lib/eye/notify.rb', line 78

def execute
  raise NotImplementedError
end

#logger_sub_tagObject



62
63
64
# File 'lib/eye/notify.rb', line 62

def logger_sub_tag
  @options[:contact]
end

#message_bodyObject



88
89
90
# File 'lib/eye/notify.rb', line 88

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

#message_subjectObject



84
85
86
# File 'lib/eye/notify.rb', line 84

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

#notifyObject



71
72
73
74
75
76
# File 'lib/eye/notify.rb', line 71

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