Class: DogWatch::Model::Client

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

Overview

Client interface for the DataDog API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Client

Returns a new instance of Client.

Parameters:



16
17
18
19
20
21
# 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

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/dogwatch/model/client.rb', line 11

def client
  @client
end

#config=(value) ⇒ Object

Sets the attribute config

Parameters:

  • value

    the value to set the attribute config to.



12
13
14
# File 'lib/dogwatch/model/client.rb', line 12

def config=(value)
  @config = value
end

#responseObject (readonly)

Returns the value of attribute response.



13
14
15
# File 'lib/dogwatch/model/client.rb', line 13

def response
  @response
end

Instance Method Details

#execute(monitor) ⇒ DogWatch::Model::Response

Parameters:

Returns:



25
26
27
28
29
30
31
# 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) ⇒ DogWatch::Model::Response

Parameters:

Returns:



47
48
49
50
51
52
53
# 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) ⇒ DogWatch::Model::Response

Parameters:

Returns:



35
36
37
38
39
40
41
42
43
# 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) ⇒ Dogwatch::Model::Response

Parameters:

  • monitor (Dogwatch::Model::Monitor)

Returns:

  • (Dogwatch::Model::Response)


57
58
59
60
61
62
63
# 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