Class: MLB::PlayerGameStats

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

Overview

Provides methods for fetching player game stats from the API

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#statsArray<PlayerGameStatGroup>

Returns the stat groups

Examples:

stats.stats #=> [#<MLB::PlayerGameStatGroup>, ...]

Returns:



299
# File 'lib/mlb/player_game_stats.rb', line 299

attribute :stats, PlayerGameStatGroup, collection: true

Class Method Details

.find(player:, game:) ⇒ PlayerGameStats

Retrieves a player’s stats for a specific game

Examples:

Get player stats for a game

MLB::PlayerGameStats.find(player: 660271, game: 745571)

Get stats using Player and ScheduledGame objects

MLB::PlayerGameStats.find(player: player, game: game)

Parameters:

  • player (Integer, Player)

    the player ID or player object

  • game (Integer, ScheduledGame)

    the game ID or game object

Returns:



315
316
317
318
319
320
# File 'lib/mlb/player_game_stats.rb', line 315

def self.find(player:, game:)
  player_id = Utils.extract_id(player)
  game_pk = game.respond_to?(:game_pk) ? game.game_pk : game
  response = CLIENT.get("people/#{player_id}/stats/game/#{game_pk}")
  from_json(response)
end

Instance Method Details

#battingPlayerGameBattingStats?

Returns the batting stats from the response

Examples:

Get batting stats

stats.batting #=> #<MLB::PlayerGameBattingStats>

Returns:



328
329
330
331
332
333
334
# File 'lib/mlb/player_game_stats.rb', line 328

def batting
  batting_group = stats.find { |s| s.group.eql?("hitting") }
  split = batting_group&.splits&.first
  return nil unless split

  PlayerGameBattingStats.from_json(split.stat.to_json)
end

#pitchingPlayerGamePitchingStats?

Returns the pitching stats from the response

Examples:

Get pitching stats

stats.pitching #=> #<MLB::PlayerGamePitchingStats>

Returns:



342
343
344
345
346
347
348
# File 'lib/mlb/player_game_stats.rb', line 342

def pitching
  pitching_group = stats.find { |s| s.group.eql?("pitching") }
  split = pitching_group&.splits&.first
  return nil unless split

  PlayerGamePitchingStats.from_json(split.stat.to_json)
end