Class: MLB::Players

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

Overview

Provides methods for fetching MLB players from the API

Class Method Summary collapse

Class Method Details

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

Retrieves all players for a given season and sport

Examples:

MLB::Players.all #=> [#<MLB::Player>, ...]

Parameters:

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

    the season year (defaults to current year)

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

    the sport or sport ID

Returns:

  • (Array<Player>)

    the list of players



21
22
23
24
25
26
# File 'lib/mlb/players.rb', line 21

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

.find(player) ⇒ Player?

Finds a single player by ID or Player object

Examples:

MLB::Players.find(660271) #=> #<MLB::Player>

Parameters:

  • player (Player, Integer)

    the player or player ID

Returns:

  • (Player, nil)

    the found player or nil



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

def self.find(player)
  params = {personIds: Utils.extract_id(player)}
  response = CLIENT.get("people?#{Utils.build_query(params)}")
  from_json(response).players.first
end

.find_all(*players) ⇒ Array<Player>

Finds multiple players by IDs or Player objects

Examples:

MLB::Players.find_all(660271, 545361) #=> [#<MLB::Player>, ...]

Parameters:

  • players (Array<Player, Integer>)

    the players or player IDs

Returns:

  • (Array<Player>)

    the list of found players



48
49
50
51
52
53
54
# File 'lib/mlb/players.rb', line 48

def self.find_all(*players)
  return all if players.empty?

  player_ids = players.map { |p| Utils.extract_id(p) }.join(",")
  response = CLIENT.get("people?#{Utils.build_query(personIds: player_ids)}")
  from_json(response).players
end