Sha256: 008073843a07b10559b27316ebbbb25d4bbf7f9aaaee2f5126d47509bb054911

Contents?: true

Size: 1.32 KB

Versions: 3

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

3 entries across 3 versions & 1 rubygems

Version Path
uppityrobot-0.4.0 lib/uppityrobot/cli/commands/monitors/delete.rb
uppityrobot-0.3.1 lib/uppityrobot/cli/commands/monitors/delete.rb
uppityrobot-0.2.0 lib/uppityrobot/cli/commands/monitors/delete.rb