Class: SleeperApi::User
- Inherits:
-
Object
- Object
- SleeperApi::User
- Includes:
- Helpers
- Defined in:
- lib/sleeper_api/user.rb
Constant Summary collapse
- ATTRIBUTES =
User attributes from the API.
%w[username user_id display_name avatar email cookies created currencies data_updated deleted is_bot metadata notifications pending phone real_name solicitable summoner_name summoner_region token verification].freeze
Instance Attribute Summary collapse
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
Instance Method Summary collapse
-
#avatar_url ⇒ String?
Generate avatar URL from ID.
-
#drafts(season = Time.now.year) ⇒ Array<Hash>
Get all drafts for a specific season with enhanced metadata.
-
#initialize(identifier, client) ⇒ User
constructor
A new instance of User.
-
#leagues(season = Time.now.year) ⇒ Array<Hash>
Get all leagues for a specific season.
-
#rosters(season = Time.now.year) ⇒ Array<Hash>
Get all rosters for this user across all leagues in a season.
-
#summary(season = Time.now.year) ⇒ Hash
Get a summary of user's league participation.
Methods included from Helpers
#deep_symbolize_keys, #player_details
Constructor Details
#initialize(identifier, client) ⇒ User
Returns a new instance of User.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/sleeper_api/user.rb', line 18 def initialize(identifier, client) raise ArgumentError, "identifier must be a non-empty string" if identifier.to_s.empty? @identifier = identifier @client = client @user_data = nil @leagues = nil @drafts = nil fetch_user_data @user_id = @user_data["user_id"] || raise(SleeperApi::Error, "Invalid user data: user_id not found") end |
Instance Attribute Details
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
12 13 14 |
# File 'lib/sleeper_api/user.rb', line 12 def identifier @identifier end |
Instance Method Details
#avatar_url ⇒ String?
Generate avatar URL from ID.
41 42 43 |
# File 'lib/sleeper_api/user.rb', line 41 def avatar_url avatar ? "https://sleepercdn.com/avatars/#{avatar}" : nil end |
#drafts(season = Time.now.year) ⇒ Array<Hash>
Get all drafts for a specific season with enhanced metadata.
77 78 79 80 81 82 |
# File 'lib/sleeper_api/user.rb', line 77 def drafts(season = Time.now.year) raise ArgumentError, "season must be a valid year" unless season.is_a?(Integer) fetch_drafts(season) unless @drafts format_drafts end |
#leagues(season = Time.now.year) ⇒ Array<Hash>
Get all leagues for a specific season.
50 51 52 53 54 55 |
# File 'lib/sleeper_api/user.rb', line 50 def leagues(season = Time.now.year) raise ArgumentError, "season must be a valid year" unless season.is_a?(Integer) fetch_leagues(season) @leagues end |
#rosters(season = Time.now.year) ⇒ Array<Hash>
Get all rosters for this user across all leagues in a season.
62 63 64 65 66 67 68 69 70 |
# File 'lib/sleeper_api/user.rb', line 62 def rosters(season = Time.now.year) raise ArgumentError, "season must be a valid year" unless season.is_a?(Integer) fetch_leagues(season) unless @leagues (@leagues || []).flat_map do |league| league_instance = League.new(league[:league_id], @client, no_data: true) league_instance.rosters(user_id: @user_id) end end |
#summary(season = Time.now.year) ⇒ Hash
Get a summary of user's league participation.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sleeper_api/user.rb', line 88 def summary(season = Time.now.year) leagues_data = leagues(season) rosters_data = rosters(season) { season: season, total_leagues: leagues_data.length, winning_record: rosters_data.select { |r| r[:wins] > r[:losses] }, total_wins: rosters_data.sum { |r| r[:wins] }, total_losses: rosters_data.sum { |r| r[:losses] }, total_ties: rosters_data.sum { |r| r[:ties] }, avg_record: if rosters_data.any? { wins: rosters_data.sum { |r| r[:wins].to_f } / rosters_data.length, losses: rosters_data.sum { |r| r[:losses].to_f } / rosters_data.length, ties: rosters_data.sum { |r| r[:ties].to_f } / rosters_data.length } end, best_team: rosters_data.max_by { |r| r[:wins].to_i - r[:losses].to_i }, worst_team: rosters_data.min_by { |r| r[:wins].to_i - r[:losses].to_i } } end |