class DogWatch::Model::Client

Client interface for the DataDog API

Attributes

client[RW]
config[RW]
response[R]

Public Class Methods

new(config) click to toggle source

@param [DogWatch::Model::Config] config

# File lib/dogwatch/model/client.rb, line 16
def initialize(config)
  @config = config
  @client = Dogapi::Client.new(@config.api_key, @config.app_key,
                               nil, nil, true, @config.timeout)
  @all_monitors = all_monitors
end

Public Instance Methods

execute(monitor) click to toggle source

@param [DogWatch::Model::Monitor] monitor @return [DogWatch::Model::Response]

# File lib/dogwatch/model/client.rb, line 25
def execute(monitor)
  if monitor_exists?(monitor.name)
    update_monitor(monitor)
  else
    new_monitor(monitor)
  end
end
new_monitor(monitor) click to toggle source

@param [DogWatch::Model::Monitor] monitor @return [DogWatch::Model::Response]

# File lib/dogwatch/model/client.rb, line 47
def new_monitor(monitor)
  options = options(monitor)
  response = @client.monitor(monitor.attributes.type,
                             monitor.attributes.query,
                             options)
  DogWatch::Model::Response.new(response, options[:name])
end
update_monitor(monitor) click to toggle source

@param [DogWatch::Model::Monitor] monitor @return [DogWatch::Model::Response]

# File lib/dogwatch/model/client.rb, line 35
def update_monitor(monitor)
  options = options(monitor)
  existing_monitor = get_monitor(monitor.name)
  response = @client.update_monitor(existing_monitor['id'],
                                    monitor.attributes.query,
                                    options)
  updated = %w(200 202).include?(response[0])
  DogWatch::Model::Response.new(response, options[:name], updated)
end
validate(monitor) click to toggle source

@param [Dogwatch::Model::Monitor] monitor @return [Dogwatch::Model::Response]

# File lib/dogwatch/model/client.rb, line 57
def validate(monitor)
  validation = monitor.validate
  return validation if validation.status == :error

  # If no validation errors return a valid Response
  DogWatch::Model::Response.new(['200', {}], 'valid')
end

Private Instance Methods

all_monitors() click to toggle source

@return [Array]

# File lib/dogwatch/model/client.rb, line 91
def all_monitors
  response = @client.get_all_monitors if @all_monitors.nil?
  @all_monitors = response[1] if response[0] == '200'
  @all_monitors
end
get_monitor(name) click to toggle source

@param [String] name @return [Hash]

# File lib/dogwatch/model/client.rb, line 86
def get_monitor(name)
  @all_monitors.find { |m| m['name'] == name }
end
monitor_exists?(name) click to toggle source

@param [String] name @return [TrueClass|FalseClass]

# File lib/dogwatch/model/client.rb, line 80
def monitor_exists?(name)
  @all_monitors.count { |m| m['name'] == name } > 0
end
options(monitor) click to toggle source

@param [DogWatch::Model::Monitor] monitor @return [Hash]

# File lib/dogwatch/model/client.rb, line 69
def options(monitor)
  {
    name: monitor.name,
    message: monitor.attributes.message,
    tags: monitor.attributes.tags,
    options: monitor.attributes.options.nil? ? {} : monitor.attributes.options
  }
end