Class: Autocronitor::Helper
- Inherits:
-
Object
- Object
- Autocronitor::Helper
- Defined in:
- lib/autocronitor/helper.rb
Instance Method Summary collapse
-
#initialize(api_key) ⇒ Helper
constructor
A new instance of Helper.
- #monitor_create(monitor_name, rules, template_names) ⇒ Object
- #monitor_suggest(cron_expression) ⇒ Object
Constructor Details
#initialize(api_key) ⇒ Helper
Returns a new instance of Helper.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/autocronitor/helper.rb', line 6 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
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/autocronitor/helper.rb', line 23 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
45 46 47 48 49 50 51 52 |
# File 'lib/autocronitor/helper.rb', line 45 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 |