Class: Eye::Notify

Inherits:
Object
  • Object
show all
Extended by:
Dsl::Validation
Includes:
Celluloid, Logger::Helpers
Defined in:
lib/eye/notify.rb

Direct Known Subclasses

Jabber, Mail

Defined Under Namespace

Classes: Jabber, Mail

Constant Summary collapse

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

Instance Attribute Summary

Attributes included from Dsl::Validation

#defaults, #should_bes, #validates, #variants

Attributes included from Logger::Helpers

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Dsl::Validation

inherited, param, validate

Constructor Details

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

Returns a new instance of Notify.



44
45
46
47
48
49
50
# File 'lib/eye/notify.rb', line 44

def initialize(options = {}, message_h = {})
  @logger = Eye::Logger.new("#{self.class.name.downcase} - #{options[:contact]}")
  debug "created notifier #{options}"

  @message_h = message_h
  @options = options
end

Class Method Details

.get_class(type) ⇒ Object



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

def self.get_class(type)
  klass = eval("Eye::Notify::#{TYPES[type]}") rescue nil
  raise "Unknown notify #{type}" unless klass
  klass
end

.notify(contact, message_h) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/eye/notify.rb', line 21

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

  return if needed_hash.blank?

  create_proc = lambda do |nh|
    type = nh[:type]
    config = (self_config[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
end

.validate!(options) ⇒ Object



17
18
19
# File 'lib/eye/notify.rb', line 17

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

Instance Method Details

#async_notifyObject



52
53
54
55
# File 'lib/eye/notify.rb', line 52

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

#executeObject



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

def execute
  raise "realize me"
end

#message_bodyObject



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

def message_body
  "#{message_subject} at #{msg_at.to_s(:short)}"
end

#message_subjectObject



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

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

#notifyObject



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

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