class UptimerobotCmd::CLI

Public Instance Methods

add(url) click to toggle source
# File lib/uptimerobot_cmd/cli.rb, line 61
def add(url)
  ENV['UPTIMEROBOT_COLORIZE'] = '1' if options[:color]

  my_options = {}
  my_options[:monitor_url] = url if url =~ /\A#{URI::regexp}\z/
  unless my_options[:monitor_url]
    puts "Please enter valid url. Example: http://example.com or https://example.com"
    exit
  end
  my_options[:contact_id] = options[:contact] || ENV['UPTIMEROBOT_DEFAULT_CONTACT'] || nil
  unless my_options[:contact_id]
    puts "Please set contact or set UPTIMEROBOT_DEFAULT_CONTACT environment variable"
    exit
  end
  my_options[:friendly_name] = options[:name] if options[: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(my_options)
      if response[0] == 200 and response[1] == "ok"
        message = "#{my_options[:monitor_url]} has been added."
      else
        message = "Error. Response code: #{response[0]}, status: #{response[1]}"
      end
      puts message
    rescue UptimerobotCmd::OptionsError => e
      puts e
    end
  end
end
contacts() click to toggle source
# File lib/uptimerobot_cmd/cli.rb, line 43
def contacts
  ENV['UPTIMEROBOT_COLORIZE'] = '1' if options[:color]
  contacts = UptimerobotCmd.get_alert_contacts
  table_title = 'Listing %d contact(s)' % contacts.count
  puts print_contacts(contacts, table_title)
end
delete(input) click to toggle source
# File lib/uptimerobot_cmd/cli.rb, line 98
def delete(input)
  ENV['UPTIMEROBOT_COLORIZE'] = '1' if options[: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"
      message = "Site has been deleted."
    else
      message = "Error. Response code: #{response[0]}, status: #{response[1]}"
    end
    puts message
  else
    puts "Delete canceled..."
  end
end
list() click to toggle source
# File lib/uptimerobot_cmd/cli.rb, line 25
def list
  ENV['UPTIMEROBOT_COLORIZE'] = '1' if options[:color]
  sort_field = 'friendlyname'
  if ['url', 'status'].include?(options[:sort])
    sort_field = options[: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
print_contacts(contacts, table_title) click to toggle source
print_monitors(monitors, table_title) click to toggle source
version() click to toggle source
# File lib/uptimerobot_cmd/cli.rb, line 10
def version
  puts UptimerobotCmd::VERSION
end