Class: UptimerobotCmd::CLI
- Inherits:
-
Thor
- Object
- Thor
- UptimerobotCmd::CLI
- Defined in:
- lib/uptimerobot_cmd/cli.rb
Instance Method Summary collapse
- #add(url) ⇒ Object
- #contacts ⇒ Object
- #delete(input) ⇒ Object
- #list ⇒ Object
- #search(input) ⇒ Object
- #version ⇒ Object
Instance Method Details
#add(url) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/uptimerobot_cmd/cli.rb', line 61 def add(url) ENV['UPTIMEROBOT_COLORIZE'] = '1' if [:color] = {} [:monitor_url] = url if url =~ /\A#{URI::regexp}\z/ unless [:monitor_url] puts "Please enter valid url. Example: http://example.com or https://example.com" exit end [:contact_id] = [:contact] || ENV['UPTIMEROBOT_DEFAULT_CONTACT'] || nil unless [:contact_id] puts "Please set contact or set UPTIMEROBOT_DEFAULT_CONTACT environment variable" exit end [:friendly_name] = [:name] if [:name] duplicate_entries = UptimerobotCmd.search_in_monitors(url) if duplicate_entries.count > 0 table_title = 'Found duplicate %d monitor(s)' % duplicate_entries.count puts print_monitors(duplicate_entries, table_title) exit else begin response = UptimerobotCmd.add_new_monitor() if response[0] == 200 and response[1] == "ok" = "#{[:monitor_url]} has been added." else = "Error. Response code: #{response[0]}, status: #{response[1]}" end puts rescue UptimerobotCmd::OptionsError => e puts e end end end |
#contacts ⇒ Object
43 44 45 46 47 48 |
# File 'lib/uptimerobot_cmd/cli.rb', line 43 def contacts ENV['UPTIMEROBOT_COLORIZE'] = '1' if [:color] contacts = UptimerobotCmd.get_alert_contacts table_title = 'Listing %d contact(s)' % contacts.count puts print_contacts(contacts, table_title) end |
#delete(input) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/uptimerobot_cmd/cli.rb', line 98 def delete(input) ENV['UPTIMEROBOT_COLORIZE'] = '1' if [:color] search = UptimerobotCmd.search_in_monitors(input) delete_id = nil if search.count > 1 puts print_monitors(search, 'Found %d monitor(s)' % search.count) ask_delete_id = ask("Please enter ID number:").to_i delete_id = ask_delete_id if ask_delete_id > 0 else ask_user = "You are going to delete %{name} [%{url}]" % { name: search.first['friendlyname'], url: search.first['url'], } result = ask(ask_user, :limited_to => ['y', 'n']) delete_id = search.first['id'] if result == 'y' end if delete_id response = UptimerobotCmd.delete_monitor(monitor_id: delete_id) if response[0] == 200 and response[1] == "ok" = "Site has been deleted." else = "Error. Response code: #{response[0]}, status: #{response[1]}" end puts else puts "Delete canceled..." end end |
#list ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/uptimerobot_cmd/cli.rb', line 25 def list ENV['UPTIMEROBOT_COLORIZE'] = '1' if [:color] sort_field = 'friendlyname' if ['url', 'status'].include?([:sort]) sort_field = [:sort] end monitors = UptimerobotCmd.get_monitors.sort_by{ |el| el[sort_field] } table_title = 'Monitoring %d site(s)' % monitors.count puts print_monitors(monitors, table_title) end |
#search(input) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/uptimerobot_cmd/cli.rb', line 129 def search(input) ENV['UPTIMEROBOT_COLORIZE'] = '1' if [:color] results = UptimerobotCmd.search_in_monitors(input) table_title = 'Found %d monitor(s)' % results.count puts print_monitors(results, table_title) end |
#version ⇒ Object
10 11 12 |
# File 'lib/uptimerobot_cmd/cli.rb', line 10 def version puts UptimerobotCmd::VERSION end |