Class: MLB::Leaders

Inherits:
Shale::Mapper
  • Object
show all
Defined in:
lib/mlb/leaders.rb

Overview

Provides methods for fetching league leaders from the API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#league_leadersArray<LeaderCategory>

Returns the league leaders

Examples:

leaders.league_leaders #=> [#<MLB::LeaderCategory>, ...]

Returns:



46
# File 'lib/mlb/leaders.rb', line 46

attribute :league_leaders, LeaderCategory, collection: true

Class Method Details

.find(category:, season: nil, limit: 10) ⇒ Array<Leader>

Retrieves league leaders for a category

Examples:

Get home run leaders

MLB::Leaders.find(category: "homeRuns", season: 2024)

Parameters:

  • category (String)

    the stat category

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

    the season year (defaults to current year)

  • limit (Integer) (defaults to: 10)

    the number of leaders to return (defaults to 10)

Returns:

  • (Array<Leader>)

    the leaders



61
62
63
64
65
66
# File 'lib/mlb/leaders.rb', line 61

def self.find(category:, season: nil, limit: 10)
  season ||= Utils.current_season
  params = {leaderCategories: category, season:, limit:}
  response = CLIENT.get("stats/leaders?#{Utils.build_query(params)}")
  from_json(response).league_leaders.first&.leaders || []
end