Sha256: 56c734f2327dbc4b16392ef88d3ff8a51d10cf73abc891811cdfcafa3139624e

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module UppityRobot
  module CLI
    module Commands
      module Monitors
        # UppityRobot::CLI::Commands::Monitors::Delete delete a monitor
        class Delete < Dry::CLI::Command
          desc "Delete a monitor"

          argument :field, required: true, values: %w[id name], desc: "The field used to target the monitor"
          argument :value, required: true, type: Integer, desc: "The field value"

          example [
            "id 1",
            "name aspace"
          ]

          def call(field:, value:, **)
            check_field(field)
            monitor = find_monitor(field, value)
            response = UppityRobot::Client.new(:deleteMonitor, {id: monitor["id"]}).execute
            puts response.to_json
          end

          def check_field(field)
            return if %w[id name].include? field

            abort({stat: "fail",
                   error: "Field not recognized, must be one of: [id, name]"}.to_json)
          end

          def find_monitor(field, value)
            params = {}
            case field
            when "id"
              params[:monitors] = value
            when "name"
              params[:search] = value
            end
            UppityRobot::Client.new(:getMonitors, params).fetch
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
uppityrobot-0.4.1 lib/uppityrobot/cli/commands/monitors/delete.rb