Class: SleeperApi::League
- Inherits:
-
Object
- Object
- SleeperApi::League
- Includes:
- Helpers
- Defined in:
- lib/sleeper_api/league.rb
Constant Summary collapse
- ATTRIBUTES =
League attributes from the API.
%w[name league_id total_rosters status sport settings season_type season scoring_settings roster_positions previous_league_id draft_id bracket_id bracket_overrides_id loser_bracket_id loser_bracket_overrides_id group_id avatar company_id shard last_message_id last_author_avatar last_author_display_name last_author_id last_author_is_bot last_message_attachment last_message_text_map last_message_time last_pinned_message_id last_read_id metadata].freeze
Instance Attribute Summary collapse
-
#league_id ⇒ Object
readonly
Returns the value of attribute league_id.
-
#weeks ⇒ Object
readonly
Returns the value of attribute weeks.
Instance Method Summary collapse
-
#avatar_url ⇒ String?
Generate avatar URL from ID.
-
#initialize(league_id, client, no_data: false) ⇒ League
constructor
A new instance of League.
-
#league_rosters ⇒ Array<Hash>
Get all league users (raw data).
-
#league_users ⇒ Hash{Integer => Array<Hash>}
Get all matchups across all weeks.
-
#matchups ⇒ Hash{Integer => Array<Hash>}
Get all matchups across all weeks.
-
#matchups_by_week(week: nil) ⇒ Array<Hash>
Get formatted matchups for a specific week.
-
#playoff_bracket ⇒ Array<Hash>
Get formatted playoff bracket with team names.
-
#reigning_champ ⇒ Hash?
Get the reigning champion's roster.
-
#rosters(roster_id: nil, user_id: nil) ⇒ Array<Hash>
Get formatted rosters with team names, records, and player lists.
-
#toilet_bowl ⇒ Array<Hash>
Get formatted toilet bowl (losers bracket) with team names.
-
#transactions(week: nil) ⇒ Array<Hash>
Get formatted transactions for a specific week.
-
#users ⇒ Array<Hash>
Get formatted league users with team names and commissioner status.
Methods included from Helpers
#deep_symbolize_keys, #player_details
Constructor Details
#initialize(league_id, client, no_data: false) ⇒ League
Returns a new instance of League.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/sleeper_api/league.rb', line 20 def initialize(league_id, client, no_data: false) raise ArgumentError, "league_id must be a non-empty string" if league_id.to_s.empty? @league_id = league_id @client = client @weeks = 1..17 @league_data = nil @league_rosters = nil @league_users = nil @matchups = nil @transactions = nil @playoff_bracket = nil @toilet_bowl = nil fetch_league_data unless no_data end |
Instance Attribute Details
#league_id ⇒ Object (readonly)
Returns the value of attribute league_id.
14 15 16 |
# File 'lib/sleeper_api/league.rb', line 14 def league_id @league_id end |
#weeks ⇒ Object (readonly)
Returns the value of attribute weeks.
14 15 16 |
# File 'lib/sleeper_api/league.rb', line 14 def weeks @weeks end |
Instance Method Details
#avatar_url ⇒ String?
Generate avatar URL from ID.
47 48 49 |
# File 'lib/sleeper_api/league.rb', line 47 def avatar_url avatar ? "https://sleepercdn.com/avatars/#{avatar}" : nil end |
#league_rosters ⇒ Array<Hash>
Get all league users (raw data).
65 66 67 68 |
# File 'lib/sleeper_api/league.rb', line 65 def league_rosters fetch_rosters unless @league_rosters @league_rosters end |
#league_users ⇒ Hash{Integer => Array<Hash>}
Get all matchups across all weeks.
73 74 75 76 |
# File 'lib/sleeper_api/league.rb', line 73 def league_users fetch_users unless @league_users @league_users end |
#matchups ⇒ Hash{Integer => Array<Hash>}
Get all matchups across all weeks.
81 82 83 84 |
# File 'lib/sleeper_api/league.rb', line 81 def matchups fetch_matchups unless @matchups&.keys&.sort == @weeks.to_a.sort @matchups end |
#matchups_by_week(week: nil) ⇒ Array<Hash>
Get formatted matchups for a specific week.
119 120 121 122 123 124 |
# File 'lib/sleeper_api/league.rb', line 119 def matchups_by_week(week: nil) raise ArgumentError, "Week must be between 1 and 17" unless @weeks.include?(week) fetch_matchups([week]) unless @matchups&.key?(week) format_matchups(week) end |
#playoff_bracket ⇒ Array<Hash>
Get formatted playoff bracket with team names.
162 163 164 165 |
# File 'lib/sleeper_api/league.rb', line 162 def playoff_bracket fetch_playoff_bracket unless @playoff_bracket @playoff_bracket end |
#reigning_champ ⇒ Hash?
Get the reigning champion's roster.
54 55 56 57 58 59 60 |
# File 'lib/sleeper_api/league.rb', line 54 def reigning_champ roster_id = @league_data&.dig("metadata", "latest_league_winner_roster_id") return nil unless roster_id roster_id = roster_id.to_i rosters(roster_id: roster_id) end |
#rosters(roster_id: nil, user_id: nil) ⇒ Array<Hash>
Get formatted rosters with team names, records, and player lists.
99 100 101 102 103 104 |
# File 'lib/sleeper_api/league.rb', line 99 def rosters(roster_id: nil, user_id: nil) fetch_rosters unless @league_rosters fetch_users unless @league_users format_rosters(roster_id: roster_id, user_id: user_id) end |
#toilet_bowl ⇒ Array<Hash>
Get formatted toilet bowl (losers bracket) with team names.
170 171 172 173 |
# File 'lib/sleeper_api/league.rb', line 170 def toilet_bowl fetch_toilet_bowl unless @toilet_bowl @toilet_bowl end |
#transactions(week: nil) ⇒ Array<Hash>
Get formatted transactions for a specific week.
152 153 154 155 156 157 |
# File 'lib/sleeper_api/league.rb', line 152 def transactions(week: nil) raise ArgumentError, "Week must be between 1 and 17" unless @weeks.include?(week) fetch_transactions([week]) unless @transactions&.key?(week) format_transactions(week) end |
#users ⇒ Array<Hash>
Get formatted league users with team names and commissioner status.
136 137 138 139 |
# File 'lib/sleeper_api/league.rb', line 136 def users fetch_users unless @league_users format_users end |