Class: SC2Cli::Subcommands::LadderShared::LadderSummary

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

Instance Method Summary collapse

Constructor Details

#initialize(json:, api:, player:) ⇒ LadderSummary

Returns a new instance of LadderSummary.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sc2cli/subcommands/ladder/laddersummary.rb', line 23

def initialize(json:, api:, player:)
  @ladders = Array.new

  if json.key?("allLadderMemberships") then
    ladders = json["allLadderMemberships"]
    @@console.fatal("Returned ladder summary list of all ladders is not an array!") unless ladders.kind_of?(Array)

    ladders.each do |ladder|
      @@console.fatal("Returned ladder summary list of all ladders contains a ladder without an ID!") unless ladder.key?("ladderId")

      id = ladder["ladderId"]

      id = id.to_i if id.kind_of?(String)

      ladder = LadderDetails.new(id: id, api: api, player: player)
      add(ladder: ladder)
    end
  end
end

Instance Method Details

#add(ladder:) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/sc2cli/subcommands/ladder/laddersummary.rb', line 45

def add(ladder:)
  id = ladder.id

  unless @ladders.any? {|ladder| ladder.id == id}
    @ladders << ladder if ladder.kind_of?(LadderDetails)
    @ladders.sort_by!{ |ladder| ladder.membership.type }
  end
end

#to_sObject



56
57
58
59
60
61
62
63
64
# File 'lib/sc2cli/subcommands/ladder/laddersummary.rb', line 56

def to_s
  result = String.new

  @ladders.each do |ladder|
    result += ladder.to_s
  end

  return result
end