Class: DogWatch::Model::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/dogwatch/model/monitor.rb

Overview

Handles monitor DSL methods and object validation

Constant Summary collapse

TYPE_MAP =
{
  metric_alert: 'metric alert',
  service_check: 'service check',
  event_alert: 'event alert'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ String

Parameters:

  • name (String)


21
22
23
24
# File 'lib/dogwatch/model/monitor.rb', line 21

def initialize(name)
  @attributes = OpenStruct.new
  @name = name
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



17
18
19
# File 'lib/dogwatch/model/monitor.rb', line 17

def attributes
  @attributes
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/dogwatch/model/monitor.rb', line 16

def name
  @name
end

Instance Method Details

#message(message) ⇒ String

Parameters:

  • message (String)

Returns:

  • (String)


41
42
43
# File 'lib/dogwatch/model/monitor.rb', line 41

def message(message)
  @attributes.message = message.to_s
end

#options(&block) ⇒ Hash

Parameters:

  • block (Proc)

Returns:

  • (Hash)


53
54
55
56
57
# File 'lib/dogwatch/model/monitor.rb', line 53

def options(&block)
  opts = DogWatch::Model::Options.new(@monitor_type)
  opts.instance_eval(&block)
  @attributes.options = opts.render
end

#query(query) ⇒ String

Parameters:

  • query (String)

Returns:

  • (String)


35
36
37
# File 'lib/dogwatch/model/monitor.rb', line 35

def query(query)
  @attributes.query = query.to_s
end

#tags(tags) ⇒ Array

Parameters:

  • tags (Array)

Returns:

  • (Array)


47
48
49
# File 'lib/dogwatch/model/monitor.rb', line 47

def tags(tags)
  @attributes.tags = tags.to_a
end

#type(type) ⇒ String

Parameters:

  • type (Symbol)

Returns:

  • (String)


28
29
30
31
# File 'lib/dogwatch/model/monitor.rb', line 28

def type(type)
  @monitor_type = type
  @attributes.type = TYPE_MAP[type]
end

#validateDogWatch::Model::Response



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/dogwatch/model/monitor.rb', line 60

def validate
  return DogWatch::Model::Response.new(invalid_type_response, 'invalid') \
    unless TYPE_MAP.key?(@monitor_type)

  errors = []
  errors.push('Missing monitor type') if missing_type?
  errors.push('Missing monitor query') if missing_query?

  if errors.empty?
    DogWatch::Model::Response.new(['200', { :message => 'valid' }], 'valid')
  else
    DogWatch::Model::Response.new(['400', { 'errors' => errors }], 'invalid')
  end
end