Class: Autocronitor::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/autocronitor/helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Helper

Returns a new instance of Helper.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/autocronitor/helper.rb', line 8

def initialize(api_key)
  @monitor_template = {
      name: '',
      notifications: {
        templates: [],
  },
      rules: [
  ],
      note: 'A human-friendly description of this monitor'
  }

  @api_url = 'https://cronitor.io/v1'
  @monitor_url = 'https://cronitor.link'
  @monitor_details = {}
  @api_key = api_key
end

Instance Method Details

#monitor_create(monitor_name, rules, template_names) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/autocronitor/helper.rb', line 25

def monitor_create(monitor_name,rules,template_names)
  @monitor_template[:name] = monitor_name
  @monitor_template[:rules] = rules
  @monitor_template[:notifications][:templates] = rules.kind_of?(Array) ? template_names : [template_names]

  response = Unirest.post(
      "#{@api_url}/monitors",
      auth: { user: @api_key },
      parameters: @monitor_template.to_json
  )

  monitor_id = response.body["code"]
  if response.code == 201
    puts "Monitor '#{monitor_name}' created with ID #{monitor_id}"
  else
    puts "Error code: #{response.code} returned, exiting"
    exit 1
  end
  @monitor_details[monitor_name] = monitor_id
  return "#{@monitor_url}/#{monitor_id}"
end

#monitor_suggest(cron_expression) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/autocronitor/helper.rb', line 47

def monitor_suggest(cron_expression)
  response = Unirest.get(
      "#{@api_url}/rules/suggest",
      auth: { user: @api_key },
      parameters:{ 'cron-expression' => cron_expression }
  )
  return response.body
end