Sha256: 47b068a6a93cf90aeff01a96f8423aea2efa80eb34d8a25882a307b492d38fdb

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

module UppityRobot
  module CLI
    module Commands
      # UppityRobot::CLI::Commands::Exec executes an API task
      class Exec < Dry::CLI::Command
        desc "Execute an API task"

        argument :task, required: true, values: UptimeRobot::Client::METHODS, desc: "API task to be executed"

        option   :data, type: :string, desc: "JSON data file"
        option   :params, type: :string, default: "{}", desc: "JSON params"

        example [
          "getMonitors --data $json_data_file",
          'getMonitors --params \'{"monitors": "123-456-789"}\''
        ]

        def call(task:, data: nil, params: "{}", **)
          task   = task.to_sym
          params = data.nil? ? JSON.parse(params) : parse_file(data)
          puts UppityRobot::Client.new(task, params).execute.to_json
        rescue JSON::ParserError => e
          puts JSON.generate({ stat: "fail", error: "JSON parser #{e.message}" })
        end

        def parse_file(file)
          JSON.parse(File.read(File.expand_path(file)))
        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/exec.rb
uppityrobot-0.3.1 lib/uppityrobot/cli/commands/exec.rb
uppityrobot-0.2.0 lib/uppityrobot/cli/commands/exec.rb