Class: SC2Cli::Subcommands::History

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

Constant Summary collapse

@@console =
Shared::Console.instance
@@prefix =
"/sc2/legacy/profile"
@@suffix =
"/matches"

Instance Method Summary collapse

Constructor Details

#initialize(configuration:, options:) ⇒ History

Returns a new instance of History.



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sc2cli/subcommands/history.rb', line 42

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

  id     = nil
  name   = 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("-i", "--id ID", Integer, "Player profile ID.") do |value|
      id = value
    end

    opts.on("-n", "--name NAME", String, "Player name. Must exist in names file.") do |value|
      name = 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!

  if name.kind_of?(String) then
    name     = Shared::Names.new(configuration: @configuration, name: name)
    id       = name.id
    region ||= name.region
  end

  region ||= @configuration.region

  @@console.fatal("Player profile ID or name must be specified!") unless id.kind_of?(Integer)

  @id     = id
  @region = region
end

Instance Method Details

#runObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sc2cli/subcommands/history.rb', line 86

def run
  @@console.info("Finding match history:")
  @@console.info(" - ID    : #{@id.to_s}")
  @@console.info(" - Region: #{@region.description}")

  path = "#{@@prefix}/#{@region.id.to_s}/#{@region.realm.to_s}/#{@id.to_s}#{@@suffix}"

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

  result = api.get(path: path)

  matches = HistoryShared::HistoryMatches.new(json: result)

  @@console.info(matches.to_s)
end