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.



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

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
53
# 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



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

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



102
103
# File 'lib/eye/notify.rb', line 102

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



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

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

#executeObject

Raises:

  • (NotImplementedError)


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

def execute
  raise NotImplementedError
end

#logger_sub_tagObject



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

def logger_sub_tag
  @options[:contact]
end

#message_bodyObject



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

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

#message_subjectObject



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

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

#notifyObject



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

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