Class: MLB::Stats

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

Overview

Provides methods for fetching player stats from the API

Class Method Summary collapse

Class Method Details

.find(season: nil, group: "hitting", stats: "season", sport: Utils::DEFAULT_SPORT_ID) ⇒ Array<PlayerStat>

Retrieves player stats

Examples:

Get player hitting stats

MLB::Stats.find(season: 2024, group: "hitting")

Get player pitching stats

MLB::Stats.find(season: 2024, group: "pitching", stats: "season")

Parameters:

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

    the season year (defaults to current year)

  • group (String) (defaults to: "hitting")

    the stat group (hitting, pitching, fielding)

  • stats (String) (defaults to: "season")

    the stats type (default: season)

  • sport (Integer, Sport) (defaults to: Utils::DEFAULT_SPORT_ID)

    the sport ID or Sport object

Returns:



34
35
36
37
38
39
# File 'lib/mlb/stats.rb', line 34

def self.find(season: nil, group: "hitting", stats: "season", sport: Utils::DEFAULT_SPORT_ID)
  season ||= Utils.current_season
  params = {season:, group:, stats:, sportIds: Utils.extract_id(sport)}
  response = CLIENT.get("stats?#{Utils.build_query(params)}")
  from_json(response).stats.first&.splits || []
end