Class: SleeperApi::Draft
- Inherits:
-
Object
- Object
- SleeperApi::Draft
- Includes:
- Helpers
- Defined in:
- lib/sleeper_api/draft.rb
Constant Summary collapse
- ATTRIBUTES =
Draft attributes from the API.
%w[draft_id created creators draft_order last_message_id last_message_time last_picked league_id settings season season_type metadata slot_to_roster_id sport start_time status type].freeze
Instance Attribute Summary collapse
-
#draft_id ⇒ Object
readonly
Returns the value of attribute draft_id.
Instance Method Summary collapse
-
#initialize(draft_id, client) ⇒ Draft
constructor
A new instance of Draft.
-
#league ⇒ SleeperApi::League
Get the associated league for this draft.
-
#next_pick ⇒ Hash?
Calculate the next pick for a live draft.
-
#picked_by(player_id) ⇒ Hash
Find who picked a specific player.
-
#picks(round: nil, roster_id: nil) ⇒ Array<Hash>
Get all draft picks with optional filtering.
-
#rounds ⇒ Hash{Integer => Array<Hash>}
Get picks grouped by round.
-
#summary ⇒ Hash
Get a high-level draft summary.
-
#team_picks ⇒ Hash{Integer => Array<Hash>}
Get picks grouped by team/roster.
-
#traded_picks(original_owner_id: nil, previous_owner_id: nil, current_owner_id: nil) ⇒ Array<Hash>
Get traded draft picks with optional filtering.
Methods included from Helpers
#deep_symbolize_keys, #player_details
Constructor Details
#initialize(draft_id, client) ⇒ Draft
Returns a new instance of Draft.
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/sleeper_api/draft.rb', line 16 def initialize(draft_id, client) raise ArgumentError, "draft_id must be a non-empty string" if draft_id.to_s.empty? @draft_id = draft_id @client = client @draft_data = nil @picks = nil @traded_picks = nil fetch_draft_data end |
Instance Attribute Details
#draft_id ⇒ Object (readonly)
Returns the value of attribute draft_id.
11 12 13 |
# File 'lib/sleeper_api/draft.rb', line 11 def draft_id @draft_id end |
Instance Method Details
#league ⇒ SleeperApi::League
Get the associated league for this draft.
66 67 68 |
# File 'lib/sleeper_api/draft.rb', line 66 def league @client.league(league_id) end |
#next_pick ⇒ Hash?
Calculate the next pick for a live draft.
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/sleeper_api/draft.rb', line 86 def next_pick return nil if status == "complete" teams_count = settings["teams"] last_pick_no = picks.map { |pick| pick[:pick_no] }.max || 0 { pick_no: last_pick_no + 1, round: ((last_pick_no / (teams_count || 12)) + 1).to_i, draft_slot: ((last_pick_no % (teams_count || 12)) + 1) } end |
#picked_by(player_id) ⇒ Hash
Find who picked a specific player.
74 75 76 77 78 79 80 81 |
# File 'lib/sleeper_api/draft.rb', line 74 def picked_by(player_id) fetch_picks unless @picks pick = @picks.find { |pick| pick[:player_id] == player_id } return nil unless pick league_users = league.users league_users.find { |user| user[:user_id] == pick[:picked_by] } end |
#picks(round: nil, roster_id: nil) ⇒ Array<Hash>
Get all draft picks with optional filtering.
40 41 42 43 44 45 46 |
# File 'lib/sleeper_api/draft.rb', line 40 def picks(round: nil, roster_id: nil) fetch_picks unless @picks picks = @picks picks = picks.select { |pick| pick[:round] == round } if round picks = picks.select { |pick| pick[:roster_id] == roster_id } if roster_id picks end |
#rounds ⇒ Hash{Integer => Array<Hash>}
Get picks grouped by round.
123 124 125 126 |
# File 'lib/sleeper_api/draft.rb', line 123 def rounds fetch_picks unless @picks @picks.group_by { |pick| pick[:round] } end |
#summary ⇒ Hash
Get a high-level draft summary.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/sleeper_api/draft.rb', line 101 def summary fetch_picks unless @picks { draft_id: draft_id, type: type, season: season, total_picks: picks.length, total_rounds: settings["rounds"] || 0, top_picks: picks.select { |pick| pick[:round] == 1 }.map do |pick| { player_id: pick[:player_id], player_name: "#{pick[:metadata][:first_name]} #{pick[:metadata][:last_name]}", position: pick[:metadata][:position], picked_by: pick[:picked_by] } end } end |
#team_picks ⇒ Hash{Integer => Array<Hash>}
Get picks grouped by team/roster.
131 132 133 134 |
# File 'lib/sleeper_api/draft.rb', line 131 def team_picks fetch_picks unless @picks @picks.group_by { |pick| pick[:roster_id] } end |
#traded_picks(original_owner_id: nil, previous_owner_id: nil, current_owner_id: nil) ⇒ Array<Hash>
Get traded draft picks with optional filtering.
54 55 56 57 58 59 60 61 |
# File 'lib/sleeper_api/draft.rb', line 54 def traded_picks(original_owner_id: nil, previous_owner_id: nil, current_owner_id: nil) fetch_traded_picks unless @traded_picks traded_picks = @traded_picks traded_picks = traded_picks.select { |pick| pick[:roster_id] == original_owner_id } if original_owner_id traded_picks = traded_picks.select { |pick| pick[:previous_owner_id] == previous_owner_id } if previous_owner_id traded_picks = traded_picks.select { |pick| pick[:owner_id] == current_owner_id } if current_owner_id traded_picks end |