Class: UppityRobot::CLI::Commands::Monitors::List

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/uppityrobot/cli/commands/monitors/list.rb

Overview

UppityRobot::CLI::Commands::Monitors::List lists monitors

Instance Method Summary collapse

Instance Method Details

#call(csv: nil, filter: "{}", search: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/uppityrobot/cli/commands/monitors/list.rb', line 21

def call(csv: nil, filter: "{}", search: nil, **)
  filter = JSON.parse(filter)
  filtered = {stat: "ok", total: 0, monitors: []}
  params = search ? {search: search} : {}
  total = 0

  UppityRobot::Client.new(:getMonitors, params).filter(filter).each do |m|
    filtered[:monitors] << m
    total += 1
  end

  filtered[:total] = total
  write_csv(csv, filtered[:monitors])
  puts filtered.to_json
rescue JSON::ParserError => e
  puts JSON.generate({stat: "fail", error: "JSON parser #{e.message}"})
end

#write_csv(file, data) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/uppityrobot/cli/commands/monitors/list.rb', line 39

def write_csv(file, data)
  return unless file

  CSV.open(File.expand_path(file), "wb", encoding: "UTF-8") do |csv|
    csv << data.first.keys
    data.each { |hash| csv << hash.values }
  end
end