class MLBGameday::Game

This class is just too long. It might be able to be split up, but it's not likely to happen any time soon. For now, we'll disable the cop. rubocop:disable Metrics/ClassLength

Attributes

away_team[R]
files[R]
gid[R]
home_team[R]

Public Class Methods

new(api, gid, files = {}) click to toggle source
# File lib/mlb_gameday/game.rb, line 10
def initialize(api, gid, files = {})
  @api = api
  @gid = gid
  @files = files

  @home_team = @api.team home_team_identifier
  @away_team = @api.team away_team_identifier
end

Public Instance Methods

attendance() click to toggle source
# File lib/mlb_gameday/game.rb, line 255
def attendance
  @files[:rawboxscore]&.xpath('//boxscore/@attendance')&.text || '0'
end
away_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 197
def away_pitcher
  # Spring training games can end in ties, in which case there's
  # really no pitching data. This should really return a null object.
  case status
  when 'In Progress'
    # The xpath changes based on which half of the inning it is
    if @files[:linescore].xpath('//game/@top_inning').text == 'Y'
      current_pitcher
    else
      opposing_pitcher
    end
  when 'Preview', 'Warmup', 'Pre-Game'
    @api.pitcher away_starting_pitcher
  when 'Final', 'Game Over'
    home, away = score

    home > away ? losing_pitcher : winning_pitcher
  end
end
away_radio() click to toggle source
# File lib/mlb_gameday/game.rb, line 235
def away_radio
  return nil unless files[:gamecenter]

  files[:gamecenter].xpath('//game/broadcast/away/radio').text
end
away_record() click to toggle source
# File lib/mlb_gameday/game.rb, line 112
def away_record
  return [0, 0] unless @files[:linescore]

  [
    @files[:linescore].xpath('//game/@away_win'),
    @files[:linescore].xpath('//game/@away_loss')
  ].map(&:text).map(&:to_i)
end
away_start_time(ampm: true) click to toggle source
# File lib/mlb_gameday/game.rb, line 46
def away_start_time(ampm: true)
  if ampm
    [
      @files[:linescore].xpath('//game/@away_time').text,
      @files[:linescore].xpath('//game/@away_ampm').text,
      @files[:linescore].xpath('//game/@away_time_zone').text
    ].join ' '
  else
    [
      @files[:linescore].xpath('//game/@away_time').text,
      @files[:linescore].xpath('//game/@away_time_zone').text
    ].join ' '
  end
end
away_starting_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 156
def away_starting_pitcher
  return '' unless @files[:linescore]

  @files[:linescore].xpath('//game/away_probable_pitcher/@id').text
end
away_tv() click to toggle source
# File lib/mlb_gameday/game.rb, line 223
def away_tv
  return nil unless files[:gamecenter]

  files[:gamecenter].xpath('//game/broadcast/away/tv').text
end
current_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 121
def current_pitcher
  return nil unless in_progress?

  @api.pitcher @files[:linescore].xpath('//game/current_pitcher/@id').text,
               year: date.year
end
date() click to toggle source
# File lib/mlb_gameday/game.rb, line 247
def date
  return Date.today unless @files[:linescore] # SUPER KLUDGE

  @date ||= Chronic.parse(
    @files[:linescore].xpath('//game/@original_date').text
  )
end
elapsed_time() click to toggle source
# File lib/mlb_gameday/game.rb, line 259
def elapsed_time
  @files[:rawboxscore]&.xpath('//boxscore/@elapsed_time')&.text || ''
end
fat_lady_has_sung?()
Alias for: over?
free?() click to toggle source
# File lib/mlb_gameday/game.rb, line 241
def free?
  return false unless @files[:linescore]

  @files[:linescore].xpath('//game/game_media/media/@free').text == 'ALL'
end
home_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 177
def home_pitcher
  # Spring training games can end in ties, in which case there's
  # really no pitching data. This should really return a null object.
  case status
  when 'In Progress'
    # The xpath changes based on which half of the inning it is
    if @files[:linescore].xpath('//game/@top_inning').text == 'Y'
      opposing_pitcher
    else
      current_pitcher
    end
  when 'Preview', 'Warmup', 'Pre-Game'
    @api.pitcher home_starting_pitcher
  when 'Final'
    home, away = score

    home > away ? winning_pitcher : losing_pitcher
  end
end
home_radio() click to toggle source
# File lib/mlb_gameday/game.rb, line 229
def home_radio
  return nil unless files[:gamecenter]

  files[:gamecenter].xpath('//game/broadcast/home/radio').text
end
home_record() click to toggle source
# File lib/mlb_gameday/game.rb, line 103
def home_record
  return [0, 0] unless @files[:linescore]

  [
    @files[:linescore].xpath('//game/@home_win'),
    @files[:linescore].xpath('//game/@home_loss')
  ].map(&:text).map(&:to_i)
end
home_start_time(ampm: true) click to toggle source
# File lib/mlb_gameday/game.rb, line 31
def home_start_time(ampm: true)
  if ampm
    [
      @files[:linescore].xpath('//game/@home_time').text,
      @files[:linescore].xpath('//game/@home_ampm').text,
      @files[:linescore].xpath('//game/@home_time_zone').text
    ].join ' '
  else
    [
      @files[:linescore].xpath('//game/@home_time').text,
      @files[:linescore].xpath('//game/@home_time_zone').text
    ].join ' '
  end
end
home_starting_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 162
def home_starting_pitcher
  return '' unless @files[:linescore]

  @files[:linescore].xpath('//game/home_probable_pitcher/@id').text
end
home_tv() click to toggle source
# File lib/mlb_gameday/game.rb, line 217
def home_tv
  return nil unless files[:gamecenter]

  files[:gamecenter].xpath('//game/broadcast/home/tv').text
end
in_progress?() click to toggle source
# File lib/mlb_gameday/game.rb, line 91
def in_progress?
  status == 'In Progress'
end
inning() click to toggle source
3, Top/Middle/Bottom/End
# File lib/mlb_gameday/game.rb, line 69
def inning
  return [0, '?'] unless @files[:linescore]&.xpath('//game/@inning')

  [
    @files[:linescore].xpath('//game/@inning').text.to_i,
    @files[:linescore].xpath('//game/@inning_state').text
  ]
end
inspect() click to toggle source

So we don't get huge printouts

# File lib/mlb_gameday/game.rb, line 284
def inspect
  %(#<MLBGameday::Game @gid="#{@gid}">)
end
losing_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 142
def losing_pitcher
  return nil unless over?

  @api.pitcher @files[:linescore].xpath('//game/losing_pitcher/@id').text,
               year: date.year
end
opposing_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 128
def opposing_pitcher
  return nil unless in_progress?

  @api.pitcher @files[:linescore].xpath('//game/opposing_pitcher/@id').text,
               year: date.year
end
over?() click to toggle source
# File lib/mlb_gameday/game.rb, line 86
def over?
  ['Final', 'Game Over', 'Completed Early'].include? status
end
Also aliased as: fat_lady_has_sung?
postponed?() click to toggle source
# File lib/mlb_gameday/game.rb, line 99
def postponed?
  status == 'Postponed'
end
runners() click to toggle source
# File lib/mlb_gameday/game.rb, line 78
def runners
  first = nil
  second = nil
  third = nil

  [first, second, third]
end
save_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 149
def save_pitcher
  return nil unless over?

  @api.pitcher @files[:linescore].xpath('//game/save_pitcher/@id').text,
               year: date.year
end
score() click to toggle source
# File lib/mlb_gameday/game.rb, line 168
def score
  return [0, 0] unless in_progress? || over?

  [
    @files[:linescore].xpath('//game/@home_team_runs').text,
    @files[:linescore].xpath('//game/@away_team_runs').text
  ].map(&:to_i)
end
started?() click to toggle source
# File lib/mlb_gameday/game.rb, line 95
def started?
  over? || in_progress?
end
status() click to toggle source

Preview, Pre-Game, In Progress, Final

# File lib/mlb_gameday/game.rb, line 62
def status
  return 'Preview' unless @files[:linescore]

  @status ||= @files[:linescore].xpath('//game/@status').text
end
teams() click to toggle source
# File lib/mlb_gameday/game.rb, line 19
def teams
  [@home_team, @away_team]
end
umpires() click to toggle source
# File lib/mlb_gameday/game.rb, line 271
def umpires
  return [] unless @files[:rawboxscore]

  umps = {}

  @files[:rawboxscore].xpath('//boxscore/umpires/umpire').each do |umpire|
    umps[umpire.xpath('@position').text] = umpire.xpath('@name').text
  end

  umps
end
venue() click to toggle source
# File lib/mlb_gameday/game.rb, line 23
def venue
  if @files[:linescore]
    return @files[:linescore].xpath('//game/@venue').text
  end

  files[:gamecenter].xpath('//game/venueShort').text
end
weather() click to toggle source
# File lib/mlb_gameday/game.rb, line 263
def weather
  @files[:rawboxscore]&.xpath('//boxscore/@weather')&.text || ''
end
wind() click to toggle source
# File lib/mlb_gameday/game.rb, line 267
def wind
  @files[:rawboxscore]&.xpath('//boxscore/@wind')&.text || ''
end
winning_pitcher() click to toggle source
# File lib/mlb_gameday/game.rb, line 135
def winning_pitcher
  return nil unless over?

  @api.pitcher @files[:linescore].xpath('//game/winning_pitcher/@id').text,
               year: date.year
end

Protected Instance Methods

away_team_identifier() click to toggle source
# File lib/mlb_gameday/game.rb, line 298
def away_team_identifier
  if files[:linescore]
    return files[:linescore].xpath('//game/@away_name_abbrev').text
  end

  files[:gamecenter].xpath('//game/@id').text[11, 6]
end
home_team_identifier() click to toggle source
# File lib/mlb_gameday/game.rb, line 290
def home_team_identifier
  if files[:linescore]
    return files[:linescore].xpath('//game/@home_name_abbrev').text
  end

  files[:gamecenter].xpath('//game/@id').text[18, 6]
end