Class: MLB::Standings

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

Overview

Provides methods for fetching standings from the API

Constant Summary collapse

DEFAULT_LEAGUE_IDS =

Default league IDs for American League (103) and National League (104)

[103, 104].freeze

Class Method Summary collapse

Class Method Details

.all(season: nil, league_ids: DEFAULT_LEAGUE_IDS) ⇒ Array<StandingsRecord>

Retrieves standings for the given leagues

Examples:

Get MLB standings

MLB::Standings.all

Get standings for a specific season

MLB::Standings.all(season: 2023)

Parameters:

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

    the season year (defaults to current year)

  • league_ids (Array<Integer>) (defaults to: DEFAULT_LEAGUE_IDS)

    league IDs (default: [103, 104] for AL and NL)

Returns:



26
27
28
29
30
31
# File 'lib/mlb/standings.rb', line 26

def self.all(season: nil, league_ids: DEFAULT_LEAGUE_IDS)
  season ||= Utils.current_season
  params = {leagueId: league_ids.join(","), season:}
  response = CLIENT.get("standings?#{Utils.build_query(params)}")
  from_json(response).records
end

.find(division:, season: nil, league_ids: DEFAULT_LEAGUE_IDS) ⇒ StandingsRecord?

Retrieves standings for a specific division

Examples:

Get AL East standings

MLB::Standings.find(division: 201)

Parameters:

  • division (Division, Integer)

    the division or division ID

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

    the season year (defaults to current year)

  • league_ids (Array<Integer>) (defaults to: DEFAULT_LEAGUE_IDS)

    league IDs (default: [103, 104] for AL and NL)

Returns:



42
43
44
45
# File 'lib/mlb/standings.rb', line 42

def self.find(division:, season: nil, league_ids: DEFAULT_LEAGUE_IDS)
  division_id = Utils.extract_id(division)
  all(season:, league_ids:).find { |record| record.division&.id.eql?(division_id) }
end