Class: MLB::Teams

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

Overview

Collection of teams from the MLB Stats API

Class Method Summary collapse

Class Method Details

.all(season: nil, sport: Utils::DEFAULT_SPORT_ID) ⇒ Array<Team>

Retrieves all teams

Examples:

MLB::Teams.all

Parameters:

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

    the season year (defaults to current year)

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

    the sport ID or Sport object

Returns:

  • (Array<Team>)

    list of all teams



17
18
19
20
21
22
# File 'lib/mlb/teams.rb', line 17

def self.all(season: nil, sport: Utils::DEFAULT_SPORT_ID)
  season ||= Utils.current_season
  params = {sportId: Utils.extract_id(sport), season:}
  response = CLIENT.get("teams?#{Utils.build_query(params)}")
  from_json(response).teams
end

.find(team, season: nil, sport: Utils::DEFAULT_SPORT_ID) ⇒ Team?

Finds a team by ID

Examples:

MLB::Teams.find(147)

Parameters:

  • team (Integer, Team)

    the team ID or Team object

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

    the season year (defaults to current year)

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

    the sport ID or Sport object

Returns:

  • (Team, nil)

    the team if found



33
34
35
36
37
38
# File 'lib/mlb/teams.rb', line 33

def self.find(team, season: nil, sport: Utils::DEFAULT_SPORT_ID)
  season ||= Utils.current_season
  params = {sportId: Utils.extract_id(sport), season:}
  response = CLIENT.get("teams/#{Utils.extract_id(team)}?#{Utils.build_query(params)}")
  from_json(response).teams.first
end