Class: Leaderbrag::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/leaderbrag/cli.rb

Overview

A CLI for Leaderbrag

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = [], local_options = {}, config = {}) ⇒ CLI

Returns a new instance of CLI.



18
19
20
21
22
23
24
25
26
27
# File 'lib/leaderbrag/cli.rb', line 18

def initialize(args = [], local_options = {}, config = {})
  super
  if ENV['XMLSTATS_CONTACT_INFO'].nil?
    warn 'Please set XMLSTATS_CONTACT_INFO environment variable.'
    exit 60
  elsif ENV['XMLSTATS_API_KEY'].nil?
    warn 'Please set XMLSTATS_API_KEY environment variable'
    exit 70
  end
end

Class Method Details

.exit_on_failureObject



13
14
15
# File 'lib/leaderbrag/cli.rb', line 13

def self.exit_on_failure
  true
end

Instance Method Details

#boardObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/leaderbrag/cli.rb', line 45

def board
  populate
  puts 'Team ID               Win%   W   L  Rank  League Rank  Div Rank   GB   Streak'
  puts '-'.ljust(77, '-')
  teams = @leader.filter(options[:sort_by_league],
                         options[:sort_by_division],
                         options[:only_league],
                         options[:only_division])
  teams.each do |team|
    puts team.team_id.ljust(22) + team.win_percentage.ljust(6) +
         team.won.to_s.ljust(4) +
         team.lost.to_s.ljust(@leader.overall_rank(team) < 10 ? 5 : 4) +
         @leader.overall_rank(team).to_s.ljust(@leader.overall_rank(team) < 10 ? 7 : 8) +
         team.conference.ljust(@leader.league_rank(team) < 10 ? 7 : 6) +
         @leader.league_rank(team).to_s.ljust(@leader.league_rank(team) < 10 ? 5 : 6) +
         team.division.ljust(5) +
         team.rank.to_s.ljust(team.games_back < 10 ? 5 : 4) +
         team.games_back.to_s.ljust(team.games_back < 10 ? 7 : 8) +
         team.streak.to_s
  end
end

#findObject



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/leaderbrag/cli.rb', line 80

def find
  if !options[:division].nil? && options[:league].nil?
    warn 'League must be specified.'
    exit 80
  end
  populate
  s_league = (options[:league].nil? ? nil : options[:league])
  s_division = (options[:division].nil? ? nil : options[:division])
  teams = @leader.filter(!options[:league].nil?, !options[:division].nil?,
                         s_league, s_division)
  myteam = teams[0]
  brag(myteam, false, options[:stats])
end

#is?(team_id) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/leaderbrag/cli.rb', line 108

def is?(team_id)
  populate
  myteam = @leader.team(team_id)
  if myteam.nil?
    warn "No such team with ID '#{team_id}'"
    exit 50
  end
  brag(myteam, options[:quiet], options[:stats])
  if @leader.overall_leader?(myteam) ||
     (options[:league] && @leader.league_leader?(myteam)) ||
     (options[:division] && @leader.division_leader?(myteam))
    exit 0
  else
    exit myteam.rank
  end
end