Class: SleeperApi::League

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • league_id (String)

    League identifier

  • client (SleeperApi::Client)

    HTTP client instance

  • no_data (Boolean) (defaults to: false)

    Skip initial data fetch (default: false)

Raises:

  • (ArgumentError)

    If league_id is empty



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_idObject (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

#weeksObject (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_urlString?

Generate avatar URL from ID.

Returns:

  • (String, nil)

    Full avatar URL or nil if no avatar



47
48
49
# File 'lib/sleeper_api/league.rb', line 47

def avatar_url
  avatar ? "https://sleepercdn.com/avatars/#{avatar}" : nil
end

#league_rostersArray<Hash>

Get all league users (raw data).

Returns:

  • (Array<Hash>)

    Raw user data from API



65
66
67
68
# File 'lib/sleeper_api/league.rb', line 65

def league_rosters
  fetch_rosters unless @league_rosters
  @league_rosters
end

#league_usersHash{Integer => Array<Hash>}

Get all matchups across all weeks.

Returns:

  • (Hash{Integer => Array<Hash>})

    Week number to matchup data



73
74
75
76
# File 'lib/sleeper_api/league.rb', line 73

def league_users
  fetch_users unless @league_users
  @league_users
end

#matchupsHash{Integer => Array<Hash>}

Get all matchups across all weeks.

Returns:

  • (Hash{Integer => Array<Hash>})

    Week number to matchup data



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.

Examples:

matchups = league.matchups_by_week(week: 5)
matchups.each do |matchup|
  matchup[:rosters].each do |team|
    puts "#{team[:points]} points from #{team[:starters].length} starters"
  end
end

Parameters:

  • week (Integer) (defaults to: nil)

    Week number (1-17)

Returns:

  • (Array<Hash>)

    Matchup data with scoring breakdown

Raises:

  • (ArgumentError)

    If week is invalid



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_bracketArray<Hash>

Get formatted playoff bracket with team names.

Returns:

  • (Array<Hash>)

    Bracket matchups with winner/loser 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_champHash?

Get the reigning champion's roster.

Returns:

  • (Hash, nil)

    Formatted roster data or nil if no champion data



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.

Examples:

Get all rosters

rosters = league.rosters
roster = rosters.first
puts "#{roster[:owner_display_name]}: #{roster[:wins]}-#{roster[:losses]}"

Get my roster

my_roster = league.rosters(user_id: my_user_id)

Parameters:

  • roster_id (Integer, nil) (defaults to: nil)

    Filter by specific roster

  • user_id (String, nil) (defaults to: nil)

    Filter by user owner

Returns:

  • (Array<Hash>)

    Formatted roster objects



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_bowlArray<Hash>

Get formatted toilet bowl (losers bracket) with team names.

Returns:

  • (Array<Hash>)

    Bracket matchups with winner/loser 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.

Examples:

transactions = league.transactions(week: 5)
transactions.each do |tx|
  puts "#{tx[:type]}: #{tx[:adds]&.length || 0} adds, #{tx[:drops]&.length || 0} drops"
end

Parameters:

  • week (Integer) (defaults to: nil)

    Week number (1-17)

Returns:

  • (Array<Hash>)

    Transaction data with adds/drops and draft picks

Raises:

  • (ArgumentError)

    If week is invalid



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

#usersArray<Hash>

Get formatted league users with team names and commissioner status.

Examples:

users = league.users
users.each do |user|
  role = user[:commissioner] ? "(Commissioner)" : ""
  puts "#{user[:display_name]} #{role}"
end

Returns:

  • (Array<Hash>)

    Formatted user objects



136
137
138
139
# File 'lib/sleeper_api/league.rb', line 136

def users
  fetch_users unless @league_users
  format_users
end