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
# File 'lib/dogwatch/model/client.rb', line 16

def initialize(config)
  @config = config
  @client = Dogapi::Client.new(@config.api_key, @config.app_key)
  @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) ⇒ Object

Parameters:



23
24
25
26
27
28
29
# File 'lib/dogwatch/model/client.rb', line 23

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:



45
46
47
48
49
50
51
# File 'lib/dogwatch/model/client.rb', line 45

def new_monitor(monitor)
  options = options(monitor)
  response = @client.monitor(monitor.attributes.type,
                             monitor.attributes.query,
                             options)
  @response = DogWatch::Model::Response.new(response)
end

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

Parameters:

Returns:



33
34
35
36
37
38
39
40
41
# File 'lib/dogwatch/model/client.rb', line 33

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])
  @response = DogWatch::Model::Response.new(response, updated)
end