Sha256: 260882ff88c4f9a759ab57b915c074413936bff9c88d4f02fa69a6212b520ab0

Contents?: true

Size: 1.53 KB

Versions: 3

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

module UppityRobot
  module CLI
    module Commands
      module Monitors
        # UppityRobot::CLI::Commands::Monitors::Create creates an HTTP monitor
        class Create < Dry::CLI::Command
          desc "Create a new HTTP monitor"

          argument :name, required: true, desc: "The name for the monitor"
          argument :url, required: true, desc: "The http/s url to monitor"
          argument :contacts, required: true, desc: "The list of contact ids"

          example [
            "google https://www.google.com 1-2-3"
          ]

          def call(name:, url:, contacts:, **)
            sub_type = check_subtype(URI.parse(url))

            data = {
              friendly_name: name,
              url: url,
              type: UptimeRobot::Monitor::Type::HTTP,
              sub_type: sub_type,
              alert_contacts: contacts
            }

            response = UppityRobot::Client.new(:newMonitor, data).execute
            puts response.to_json
          rescue URI::InvalidURIError => e
            puts JSON.generate({ stat: "fail", error: "URI parser #{e.message}" })
          end

          def check_subtype(uri)
            if uri.instance_of?(URI::HTTP)
              UptimeRobot::Monitor::SubType::HTTP
            elsif uri.instance_of?(URI::HTTPS)
              UptimeRobot::Monitor::SubType::HTTPS
            else
              abort({ stat: "fail",
                      error: "Monitor URL must be HTTP/S" }.to_json)
            end
          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/create.rb
uppityrobot-0.3.1 lib/uppityrobot/cli/commands/monitors/create.rb
uppityrobot-0.2.0 lib/uppityrobot/cli/commands/monitors/create.rb