Class: Eye::Notify

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

Direct Known Subclasses

Custom, Jabber, Mail, Slack

Defined Under Namespace

Classes: Custom, Jabber, Mail, Slack

Constant Summary collapse

TYPES =
{ mail: 'Mail', jabber: 'Jabber', slack: 'Slack' }
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.


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

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

  debug { "created notifier #{options}" }
end

Class Method Details

.get_class(type) ⇒ Object


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

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


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
52
# File 'lib/eye/notify.rb', line 27

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 Object => ex
  log_ex(ex)
end

.register(base) ⇒ Object


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

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


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

def self.requires; end

.validate!(options) ⇒ Object


23
24
25
# File 'lib/eye/notify.rb', line 23

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

Instance Method Details

#async_notifyObject


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

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

#executeObject

Raises:

  • (NotImplementedError)

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

def execute
  raise NotImplementedError
end

#logger_sub_tagObject


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

def logger_sub_tag
  @options[:contact]
end

#message_bodyObject


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

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

#message_subjectObject


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

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

#notifyObject


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

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