Class: SleeperApi::Draft

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

Instance Method Summary collapse

Methods included from Helpers

#deep_symbolize_keys, #player_details

Constructor Details

#initialize(draft_id, client) ⇒ Draft

Returns a new instance of Draft.

Parameters:

  • draft_id (String)

    Draft identifier

  • client (SleeperApi::Client)

    HTTP client instance

Raises:

  • (ArgumentError)

    If draft_id is empty



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

#leagueSleeperApi::League

Get the associated league for this draft.

Returns:



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

def league
  @client.league(league_id)
end

#next_pickHash?

Calculate the next pick for a live draft.

Returns:

  • (Hash, nil)

    Next pick details or nil if draft is complete



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.

Parameters:

  • player_id (String, Integer)

    Player ID

Returns:

  • (Hash)

    User data for the picker, or nil if player not drafted



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.

Parameters:

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

    Filter by round number

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

    Filter by team/roster

Returns:

  • (Array<Hash>)

    Formatted pick objects



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

#roundsHash{Integer => Array<Hash>}

Get picks grouped by round.

Returns:

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

    Round number to picks mapping



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

#summaryHash

Get a high-level draft summary.

Returns:

  • (Hash)

    Summary with top picks and stats



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_picksHash{Integer => Array<Hash>}

Get picks grouped by team/roster.

Returns:

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

    Roster ID to picks mapping



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.

Parameters:

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

    Filter by original team

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

    Filter by previous owner

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

    Filter by current owner

Returns:

  • (Array<Hash>)

    Formatted traded pick objects



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