Class: SC2Cli::Subcommands::Raw

Inherits:
Object
  • Object
show all
Defined in:
lib/sc2cli/subcommands/raw.rb

Constant Summary collapse

@@console =
Shared::Console.instance

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, options:) ⇒ Raw

Returns a new instance of Raw.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/sc2cli/subcommands/raw.rb', line 30

def initialize(configuration:, options:)
  @configuration = configuration

  path   = nil
  region = nil

  OptionParser.new do |opts|
    opts.banner = "Usage: #{$0} #{self.class.name.split("::").last.downcase} [options]"

    opts.on("-h", "--help", "Prints this help") do
      @@console.info(opts)
      exit
    end

    opts.on("-p", "--path PATH", String, "The API path to query.") do |value|
      path = value
    end

    opts.on("-r", "--region REGION", String, "Region name, such as 'eu' or 'us'. Use configuration region by default.") do |value|
      region = Shared::Region.new(name: value)
    end
  end.parse!

  region ||= @configuration.region

  @@console.fatal("Raw path must be specified!") unless path.kind_of?(String)
  @@console.fatal("Raw path must not be blank!") if path.empty?
  @@console.fatal("Raw path must begin with '/'!") unless path.chars.first == "/"

  @path   = path
  @region = region
end

Instance Method Details

#runObject



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/sc2cli/subcommands/raw.rb', line 65

def run
  @@console.info("Running raw API request:")
  @@console.info(" - Path  : #{@path}")
  @@console.info(" - Region: #{@region.description}")

  api = Shared::Api.new(configuration: @configuration, region: @region)

  result = api.get(path: @path)

  @@console.info(JSON.pretty_generate(result))
end