Class: WatchList::DSL::Context::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/watch_list/dsl/context/monitor.rb,
lib/watch_list/dsl/context/monitor/http.rb,
lib/watch_list/dsl/context/monitor/ping.rb,
lib/watch_list/dsl/context/monitor/port.rb,
lib/watch_list/dsl/context/monitor/keyword.rb

Defined Under Namespace

Classes: HTTP, Keyword, Ping, Port, Type

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Monitor

Returns a new instance of Monitor.



2
3
4
5
6
7
8
9
10
11
12
# File 'lib/watch_list/dsl/context/monitor.rb', line 2

def initialize(name, &block)
  @name = name

  @result = {
    :FriendlyName  => name,
    :Paused        => false,
    :AlertContacts => [],
  }

  instance_eval(&block)
end

Instance Method Details

#alert_contact(type, value) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/watch_list/dsl/context/monitor.rb', line 42

def alert_contact(type, value)
  if type.kind_of?(Integer)
    alert_contact_type = type
  else
    alert_contact_type = WatchList::AlertContact::Type[type.to_s]
  end

  unless WatchList::AlertContact::Type.values.include?(alert_contact_type)
    raise %!Monitor `#{@name}`: "alert_contact" type is invalid: #{type.inspect}!
  end

  raise %!Monitor `#{@name}`: "alert_contact" value is invalid: #{value.inspect}! if value.nil?

  hash = {
    :Type  => alert_contact_type,
    :Value => WatchList::Secure.decrypt_if_possible(value).to_s,
  }

  if @result[:AlertContacts].include?(hash)
    raise %!Monitor `#{@name}`: "alert_contact"(#{type}, #{value}) is already defined!
  end

  @result[:AlertContacts] << hash
end

#interval(value) ⇒ Object



29
30
31
32
# File 'lib/watch_list/dsl/context/monitor.rb', line 29

def interval(value)
  raise %!Monitor `#{@name}`: "interval" is invalid: #{value.inspect}! unless value.kind_of?(Integer)
  @result[:Interval] = value
end

#paused(value) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/watch_list/dsl/context/monitor.rb', line 34

def paused(value)
  unless [TrueClass, FalseClass].any? {|i| value.kind_of?(i) }
    raise %!Monitor `#{@name}`: "paused" is invalid: #{value.inspect}!
  end

  @result[:Paused] = value
end

#resultObject



14
15
16
17
18
19
20
21
22
# File 'lib/watch_list/dsl/context/monitor.rb', line 14

def result
  raise %!Monitor `#{@name}`: "target" is required! unless @result[:URL]

  [:Interval, :Type].each do |key|
    raise %!Monitor `#{@name}`: "#{key.to_s.downcase}" is required! unless @result[key]
  end

  @result
end

#target(value) ⇒ Object



24
25
26
27
# File 'lib/watch_list/dsl/context/monitor.rb', line 24

def target(value)
  raise %!Monitor `#{@name}`: "target" is invalid: #{value.inspect}! if value.nil?
  @result[:URL] = WatchList::Secure.decrypt_if_possible(value).to_s
end

#type(value, &block) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/watch_list/dsl/context/monitor.rb', line 67

def type(value, &block)
  if value.kind_of?(Integer)
    monitor_type = value
  else
    monitor_type = WatchList::Monitor::Type[value.to_s]
  end

  unless WatchList::Monitor::Type.values.include?(monitor_type)
    raise %!Monitor `#{@name}`: "type" is invalid: #{value.inspect}!
  end

  @result[:Type] = monitor_type

  type_class = WatchList::DSL::Context::Monitor::Type[value.to_s]
  raise "Monitor `#{@name}`: #{value.inspect} is unimplemented type" unless type_class

  @result.update(type_class.new(@name, &block).result)
end