Class: Leaderbrag::Leader
- Inherits:
-
Object
- Object
- Leaderbrag::Leader
- Defined in:
- lib/leaderbrag/leader.rb
Overview
Mostly wraps MLB standings adding some additional leadership checks
Instance Attribute Summary collapse
-
#standings ⇒ Object
readonly
Returns the value of attribute standings.
Instance Method Summary collapse
- #division_leader?(team) ⇒ Boolean
- #filter(sb_league = false, sb_division = false, s_conference = nil, s_division = nil) ⇒ Object
-
#initialize(date = Date.today) ⇒ Leader
constructor
A new instance of Leader.
- #league_leader?(team) ⇒ Boolean
- #league_members(team) ⇒ Object
- #league_rank(team) ⇒ Object
- #overall_leader?(team) ⇒ Boolean
- #overall_rank(team) ⇒ Object
- #team(team_id) ⇒ Object
Constructor Details
#initialize(date = Date.today) ⇒ Leader
Returns a new instance of Leader.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/leaderbrag/leader.rb', line 9 def initialize(date = Date.today) Xmlstats.cacher = if ENV['XMLSTATS_CACHER'] == 'redis' host = '127.0.0.1' if ENV.key? 'XMLSTATS_REDIS_HOST' host = ENV['XMLSTATS_REDIS_HOST'] end Xmlstats::Cachers::Redis.new(host: host) else Xmlstats::Cachers::Memory.new end begin @standings = Xmlstats.mlb_standing(date) rescue Redis::CannotConnectError warn "WARN: Redis host #{ENV['XMLSTATS_REDIS_HOST']} "\ 'not available. Falling back to memory cacher.' Xmlstats.cacher = Xmlstats::Cachers::Memory.new @standings = Xmlstats.mlb_standing(date) end @standings.sort_by! do |team| team.win_percentage.to_f end @standings.reverse! end |
Instance Attribute Details
#standings ⇒ Object (readonly)
Returns the value of attribute standings.
8 9 10 |
# File 'lib/leaderbrag/leader.rb', line 8 def standings @standings end |
Instance Method Details
#division_leader?(team) ⇒ Boolean
39 40 41 42 43 |
# File 'lib/leaderbrag/leader.rb', line 39 def division_leader?(team) return true if team.rank == 1 false end |
#filter(sb_league = false, sb_division = false, s_conference = nil, s_division = nil) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/leaderbrag/leader.rb', line 71 def filter(sb_league = false, sb_division = false, s_conference = nil, s_division = nil) view = standings.sort_by do |team| sort_items = [] sort_items << team.conference if sb_league sort_items << team.conference if sb_division sort_items << team.division if sb_division sort_items << league_rank(team) if sb_league sort_items << team.rank if sb_division sort_items << overall_rank(team) if !sb_league && !sb_division sort_items end unless s_conference.nil? view.select! do |team| team.conference == s_conference end end unless s_division.nil? view.select! do |team| team.division == s_division end end view end |
#league_leader?(team) ⇒ Boolean
51 52 53 54 55 |
# File 'lib/leaderbrag/leader.rb', line 51 def league_leader?(team) return true if team.team_id == league_members(team)[0].team_id false end |
#league_members(team) ⇒ Object
45 46 47 48 49 |
# File 'lib/leaderbrag/leader.rb', line 45 def league_members(team) @standings.select do |t| team.conference == t.conference end end |
#league_rank(team) ⇒ Object
57 58 59 |
# File 'lib/leaderbrag/leader.rb', line 57 def league_rank(team) league_members(team).find_index(team) + 1 end |
#overall_leader?(team) ⇒ Boolean
61 62 63 64 65 |
# File 'lib/leaderbrag/leader.rb', line 61 def overall_leader?(team) return true if team.team_id == @standings[0].team_id false end |
#overall_rank(team) ⇒ Object
67 68 69 |
# File 'lib/leaderbrag/leader.rb', line 67 def overall_rank(team) @standings.find_index(team) + 1 end |
#team(team_id) ⇒ Object
33 34 35 36 37 |
# File 'lib/leaderbrag/leader.rb', line 33 def team(team_id) @standings.detect do |team| team.team_id == team_id end end |