class DogWatch::Model::Monitor

Handles monitor DSL methods and object validation

Constants

TYPE_MAP

Attributes

attributes[R]
name[R]

Public Class Methods

new(name) click to toggle source

@param [String] name @return [String]

# File lib/dogwatch/model/monitor.rb, line 21
def initialize(name)
  @attributes = OpenStruct.new
  @name = name
end

Public Instance Methods

message(message) click to toggle source

@param [String] message @return [String]

# File lib/dogwatch/model/monitor.rb, line 41
def message(message)
  @attributes.message = message.to_s
end
options(&block) click to toggle source

@param [Proc] block @return [Hash]

# 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) click to toggle source

@param [String] query @return [String]

# File lib/dogwatch/model/monitor.rb, line 35
def query(query)
  @attributes.query = query.to_s
end
tags(tags) click to toggle source

@param [Array] tags @return [Array]

# File lib/dogwatch/model/monitor.rb, line 47
def tags(tags)
  @attributes.tags = tags.to_a
end
type(type) click to toggle source

@param [Symbol] type @return [String]

# File lib/dogwatch/model/monitor.rb, line 28
def type(type)
  @monitor_type = type
  @attributes.type = TYPE_MAP[type]
end
validate() click to toggle source

@return [DogWatch::Model::Response]

# 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

Private Instance Methods

invalid_type_response() click to toggle source
# File lib/dogwatch/model/monitor.rb, line 89
def invalid_type_response
  [
    '400',
    { 'errors' => [
      "Monitor type '#{@monitor_type}' is not valid. " \
      "Valid monitor types are: #{valid_types}"
    ] }
  ]
end
missing_query?() click to toggle source
# File lib/dogwatch/model/monitor.rb, line 85
def missing_query?
  @attributes.query.nil? || @attributes.query.empty?
end
missing_type?() click to toggle source
# File lib/dogwatch/model/monitor.rb, line 81
def missing_type?
  @attributes.type.nil? || @attributes.type.empty?
end
valid_types() click to toggle source
# File lib/dogwatch/model/monitor.rb, line 77
def valid_types
  TYPE_MAP.keys.map { |k| ":#{k}" }.join(', ')
end