Class: UptimerobotCmd::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/uptimerobot_cmd/cli.rb

Instance Method Summary collapse

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 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

#contactsObject



43
44
45
46
47
48
# 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) ⇒ 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 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

#listObject



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 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

#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 options[:color]
  results = UptimerobotCmd.search_in_monitors(input)
  table_title = 'Found %d monitor(s)' % results.count
  puts print_monitors(results, table_title)
end

#versionObject



10
11
12
# File 'lib/uptimerobot_cmd/cli.rb', line 10

def version
  puts UptimerobotCmd::VERSION
end