Class: God::Contact

Inherits:
Object
  • Object
show all
Includes:
Configurable
Defined in:
lib/god/contact.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Configurable

#base_name, complain, #complain, #prepare, #reset, #valid?

Instance Attribute Details

#groupObject

Returns the value of attribute group.



6
7
8
# File 'lib/god/contact.rb', line 6

def group
  @group
end

#infoObject

Returns the value of attribute info.



6
7
8
# File 'lib/god/contact.rb', line 6

def info
  @info
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/god/contact.rb', line 6

def name
  @name
end

Class Method Details

.defaults {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (God::Contact)

    the object that the method was called on



27
28
29
# File 'lib/god/contact.rb', line 27

def self.defaults
  yield self
end

.generate(kind) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/god/contact.rb', line 8

def self.generate(kind)
  sym = kind.to_s.capitalize.gsub(/_(.)/){$1.upcase}.intern
  c = God::Contacts.const_get(sym).new
  
  unless c.kind_of?(Contact)
    abort "Contact '#{c.class.name}' must subclass God::Contact" 
  end
  
  c
rescue NameError
  raise NoSuchContactError.new("No Contact found with the class name God::Contacts::#{sym}")
end

.normalize(spec) ⇒ Object

Normalize the given notify specification into canonical form.

+spec+ is the notify spec as a String, Array of Strings, or Hash

Canonical form looks like: => [‘fred’, ‘john’], :priority => ‘1’, :category => ‘awesome’ Where :contacts will be present and point to an Array of Strings. Both :priority and :category may not be present but if they are, they will each contain a single String.

Returns normalized notify spec Raises ArgumentError on invalid spec (message contains details)



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/god/contact.rb', line 46

def self.normalize(spec)
  case spec
    when String
      {:contacts => Array(spec)}
    when Array
      unless spec.select { |x| !x.instance_of?(String) }.empty?
        raise ArgumentError.new("contains non-String elements")
      end
      {:contacts => spec}
    when Hash
      copy = spec.dup
      
      # check :contacts
      if contacts = copy.delete(:contacts)
        case contacts
          when String
            # valid
          when Array
            unless contacts.select { |x| !x.instance_of?(String) }.empty?
              raise ArgumentError.new("has a :contacts key containing non-String elements")
            end
            # valid
          else
            raise ArgumentError.new("must have a :contacts key pointing to a String or Array of Strings")
        end
      else
        raise ArgumentError.new("must have a :contacts key")
      end
      
      # remove priority and category
      copy.delete(:priority)
      copy.delete(:category)
      
      # check for invalid keys
      unless copy.empty?
        raise ArgumentError.new("contains extra elements: #{copy.inspect}")
      end
      
      # normalize
      spec[:contacts] &&= Array(spec[:contacts])
      spec[:priority] &&= spec[:priority].to_s
      spec[:category] &&= spec[:category].to_s
      
      spec
    else
      raise ArgumentError.new("must be a String (contact name), Array (of contact names), or Hash (contact specification)")
  end
end

.valid?(contact) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/god/contact.rb', line 21

def self.valid?(contact)
  valid = true
  valid &= Configurable.complain("Attribute 'name' must be specified", contact) if contact.name.nil?
  valid
end

Instance Method Details

#arg(name) ⇒ Object



31
32
33
# File 'lib/god/contact.rb', line 31

def arg(name)
  self.instance_variable_get("@#{name}") || self.class.instance_variable_get("@#{name}")
end

#friendly_nameObject

Construct the friendly name of this Contact, looks like:

Contact FooBar



109
110
111
# File 'lib/god/contact.rb', line 109

def friendly_name
  super + " Contact '#{self.name}'"
end

#notify(message, time, priority, category, host) ⇒ Object

Abstract Send the message to the external source

+message+ is the message body returned from the condition
+time+ is the Time at which the notification was made
+priority+ is the arbitrary priority String
+category+ is the arbitrary category String
+host+ is the hostname of the server


102
103
104
# File 'lib/god/contact.rb', line 102

def notify(message, time, priority, category, host)
  raise AbstractMethodNotOverriddenError.new("Contact#notify must be overridden in subclasses")
end