Class: NagiosHerald::Message

Inherits:
Object
  • Object
show all
Includes:
Logging, Util
Defined in:
lib/nagios-herald/messages/irc.rb,
lib/nagios-herald/messages/base.rb,
lib/nagios-herald/messages/email.rb,
lib/nagios-herald/messages/pager.rb

Direct Known Subclasses

Email, IRC, Pager

Defined Under Namespace

Classes: Email, IRC, Pager

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#get_nagios_var, get_script_path, load_helper, underscore_to_camel_case, #unescape_text

Methods included from Logging

#configure_logger_for, #logger, #logger_for

Constructor Details

#initialize(recipients, options) ⇒ Message

Returns a new instance of Message.



16
17
18
19
20
# File 'lib/nagios-herald/messages/base.rb', line 16

def initialize(recipients, options)
  @no_send     = options[:no_send]
  # TODO: instead of passing this in via the subclass, let's set it via message.recipients
  @recipients = recipients
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



13
14
15
# File 'lib/nagios-herald/messages/base.rb', line 13

def content
  @content
end

#recipientsObject

Returns the value of attribute recipients.



14
15
16
# File 'lib/nagios-herald/messages/base.rb', line 14

def recipients
  @recipients
end

Class Method Details

.inherited(subclass) ⇒ Object

Public: When subclassed message types are instantiated, add them to the @@message_types hash. The key is the downcased and snake_cased name of the class file (i.e. email); the value is the actual class (i.e. Email) so that we can easily instantiate message types when we know the message type name. Learned this pattern thanks to the folks at Chef and @jonlives. See github.com/opscode/chef/blob/11-stable/lib/chef/knife.rb#L79#L83

Returns the message_types hash.



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/nagios-herald/messages/base.rb', line 41

def self.inherited(subclass)
  subclass_base_name = subclass.name.split('::').last
  if subclass_base_name == subclass_base_name.upcase
    # we've got an all upper case class name (probably an acronym like IRC); just downcase the whole thing
    subclass_base_name.downcase!
    message_types[subclass_base_name] = subclass
  else
    subclass_base_name.gsub!(/[A-Z]/) { |s| "_" + s } # replace uppercase with underscore and lowercase
    subclass_base_name.downcase!
    subclass_base_name.sub!(/^_/, "")   # strip the leading underscore
    message_types[subclass_base_name] = subclass
  end
end

.message_typesObject



29
30
31
# File 'lib/nagios-herald/messages/base.rb', line 29

def self.message_types
  @@message_types ||= {}
end

Instance Method Details

#sendObject

Public: Defines what is required to send a message. The message type knows best how this is done. Override the #send method in your message subclass.

Raises:

  • (Exception)


25
26
27
# File 'lib/nagios-herald/messages/base.rb', line 25

def send
  raise Exception, "#{self.to_s}: You must override #send"
end