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.



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

def group
  @group
end

#infoObject

Returns the value of attribute info.



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

def info
  @info
end

#nameObject

Returns the value of attribute name.



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

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



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

def self.defaults
  yield self
end

.generate(kind) ⇒ Object



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

def self.generate(kind)
  sym = kind.to_s.capitalize.gsub(/_(.)/) { Regexp.last_match(1).upcase }.intern
  c = God::Contacts.const_get(sym).new

  abort "Contact '#{c.class.name}' must subclass God::Contact" unless c.is_a?(Contact)

  c
rescue NameError
  raise NoSuchContactError, "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)



45
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
# File 'lib/god/contact.rb', line 45

def self.normalize(spec)
  case spec
  when String
    { contacts: Array(spec) }
  when Array
    raise ArgumentError, 'contains non-String elements' unless spec.all? { |x| x.instance_of?(String) }

    { contacts: spec }
  when Hash
    copy = spec.dup

    # check :contacts
    raise ArgumentError, 'must have a :contacts key' unless (contacts = copy.delete(:contacts))

    case contacts
    when String
    # valid
    when Array
      raise ArgumentError, 'has a :contacts key containing non-String elements' unless contacts.all? { |x| x.instance_of?(String) }
    # valid
    else
      raise ArgumentError, 'must have a :contacts key pointing to a String or Array of Strings'
    end

    # remove priority and category
    copy.delete(:priority)
    copy.delete(:category)

    # check for invalid keys
    raise ArgumentError, "contains extra elements: #{copy.inspect}" unless copy.empty?

    # normalize
    spec[:contacts] &&= Array(spec[:contacts])
    spec[:priority] &&= spec[:priority].to_s
    spec[:category] &&= spec[:category].to_s

    spec
  else
    raise ArgumentError, 'must be a String (contact name), Array (of contact names), or Hash (contact specification)'
  end
end

.valid?(contact) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#friendly_nameObject

Construct the friendly name of this Contact, looks like:

Contact FooBar



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

def friendly_name
  super + " Contact '#{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


94
95
96
# File 'lib/god/contact.rb', line 94

def notify(message, time, priority, category, host) # rubocop:disable Lint/UnusedMethodArgument
  raise AbstractMethodNotOverriddenError, 'Contact#notify must be overridden in subclasses'
end