Class: Copperegg::Alerts

Inherits:
Object
  • Object
show all
Defined in:
lib/copperegg/alerts.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAlerts

Returns a new instance of Alerts.



7
8
9
10
# File 'lib/copperegg/alerts.rb', line 7

def initialize
  @client = Copperegg::Client.instance
  @schedules = @client.get('alerts/schedules.json')
end

Instance Attribute Details

#schedulesObject (readonly)

Returns the value of attribute schedules.



5
6
7
# File 'lib/copperegg/alerts.rb', line 5

def schedules
  @schedules
end

Instance Method Details

#create_schedule(name, *args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/copperegg/alerts.rb', line 34

def create_schedule(name, *args)
  defaults = {
      'name' => name,
      'state' => 'enabled',
      'duration' => 10,
      'start_time' => Time.now.gmtime.strftime('%Y-%m-%dt%H:%M:%Sz'),
  }
  args.each { |arg| defaults.deep_merge!(arg) }
  if result = @client.post?('alerts/schedules.json', defaults)
    @schedules << result.parsed_response
  end
end

#delete_schedules(name) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/copperegg/alerts.rb', line 47

def delete_schedules(name)
  selected_schedules = @schedules.select { |h| h['name'] == name }
  if selected_schedules
    @schedules -= selected_schedules
    selected_schedules.each do |s|
      if @client.delete?("alerts/schedules/#{s['id']}.json") == nil
        @schedules << s
      end
    end
  end
end

#modify_schedule(name, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/copperegg/alerts.rb', line 17

def modify_schedule(name, *args)
  body = {}
  args.each { |arg| body.deep_merge!(arg) }
  selected_schedules = @schedules.select { |h| h['name'] == name }
  if selected_schedules
    @schedules -= selected_schedules
    selected_schedules.each do |s|
      result = @client.put?("alerts/schedules/#{s['id']}.json", body)
      if result == nil
        @schedules << s
      else
        @schedules << result
      end
    end
  end
end

#set_schedule(name, tags = {}) ⇒ Object



12
13
14
15
# File 'lib/copperegg/alerts.rb', line 12

def set_schedule(name, tags = {})
  delete_schedules(name)
  create_schedule(name, tags)
end