class AhlScraper::Game

Constants

ATTRIBUTES
IRREGULAR_GAMES

Public Class Methods

new(game_id, opts = {}) click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 49
def initialize(game_id, opts = {})
  @game_id = game_id
  @raw_data = GameDataFetcher.new(game_id).call
  @raw_event_data = GameEventDataFetcher.new(game_id).call
  @season_type = opts[:season_type] || SeasonTypeFetcher.new(@raw_data[:details][:seasonId]).call
end

Public Instance Methods

away_coaches() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 100
def away_coaches
  @away_coaches ||= Array(@raw_data[:visitingTeam][:coaches]).uniq { |coach| coach.values_at(:lastName, :firstName) }
    .map { |coach| Games::Coach.new(coach, { team_id: away_team.id }) }
end
away_goalies() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 179
def away_goalies
  @away_goalies ||= Array(@raw_data[:visitingTeam][:goalies]).map do |g|
    Games::Goalie.new(
      g,
      {
        team_id: away_team.id,
        home_team: false,
        shootout_data: @raw_data.dig(:shootoutDetails, :homeTeamShots),
        penalty_shot_data: @raw_data.dig(:penaltyShots, :homeTeam),
        game_date: @raw_data[:details][:date],
      }
    )
  end
end
away_penalty_shots() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 218
def away_penalty_shots
  @away_penalty_shots ||= Games::PenaltyShotsService.new(@raw_data[:penaltyShots][:visitingTeam]).call
end
away_roster() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 198
def away_roster
  @away_roster ||= [*away_skaters, *away_goalies]
end
away_shootout_attempts() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 244
def away_shootout_attempts
  @away_shootout_attempts ||= raw_away_shootout_attempts&.map&.with_index do |att, i|
    Games::ShootoutAttempt.new(att, { number: i + 1, opposing_team: find_opposing_team(att[:shooterTeam][:id].to_i) })
  end || []
end
away_skaters() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 153
def away_skaters
  @away_skaters ||= Games::CreateSkatersService.new(
    Array(@raw_data[:visitingTeam][:skaters]),
    raw_goals,
    raw_penalties,
    raw_away_shootout_attempts,
    @raw_data[:penaltyShots][:visitingTeam],
    { home_team: false, team_id: away_team.id, team_abbreviation: away_team.abbreviation, game_date: @raw_data[:details][:date] }
  ).call
end
away_team() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 120
def away_team
  @away_team ||= Games::Team.new(
    @raw_data[:visitingTeam],
    {
      home_team: false,
      goals: raw_goals,
      current_state: current_state,
      game_properties: game_properties,
      shots: raw_period_shots,
      goal_totals: raw_period_goals,
      winning_team_id: winning_team_id,
    }
  )
end
current_period() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 74
def current_period
  @current_period ||= set_current_game_period
end
current_period_number() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 78
def current_period_number
  @current_period_number ||= @raw_data[:periods].length
end
current_time() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 70
def current_time
  @current_time ||= set_current_game_time
end
ended_in() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 262
def ended_in
  @ended_in ||=
    if shootout?
      "SO"
    elsif overtime?
      "OT"
    else
      "REG"
    end
end
goals() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 202
def goals
  @goals ||= raw_goals.map.with_index { |g, i| Games::Goal.new(g, { number: i + 1 }) }
end
home_coaches() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 95
def home_coaches
  @home_coaches ||= Array(@raw_data[:homeTeam][:coaches]).uniq { |coach| coach.values_at(:lastName, :firstName) }
    .map { |coach| Games::Coach.new(coach, { team_id: home_team.id }) }
end
home_goalies() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 164
def home_goalies
  @home_goalies ||= Array(@raw_data[:homeTeam][:goalies]).map do |g|
    Games::Goalie.new(
      g,
      {
        team_id: home_team.id,
        home_team: true,
        shootout_data: @raw_data.dig(:shootoutDetails, :visitingTeamShots),
        penalty_shot_data: @raw_data.dig(:penaltyShots, :visitingTeam),
        game_date: @raw_data[:details][:date],
      }
    )
  end
end
home_penalty_shots() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 214
def home_penalty_shots
  @home_penalty_shots ||= Games::PenaltyShotsService.new(@raw_data[:penaltyShots][:homeTeam]).call
end
home_roster() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 194
def home_roster
  @home_roster ||= [*home_skaters, *home_goalies]
end
home_shootout_attempts() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 238
def home_shootout_attempts
  @home_shootout_attempts ||= raw_home_shootout_attempts&.map&.with_index do |att, i|
    Games::ShootoutAttempt.new(att, { number: i + 1, opposing_team: find_opposing_team(att[:shooterTeam][:id].to_i) })
  end || []
end
home_skaters() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 142
def home_skaters
  @home_skaters ||= Games::CreateSkatersService.new(
    @raw_data[:homeTeam][:skaters],
    raw_goals,
    raw_penalties,
    raw_home_shootout_attempts,
    @raw_data[:penaltyShots][:homeTeam],
    { home_team: true, team_id: home_team.id, team_abbreviation: home_team.abbreviation, game_date: @raw_data[:details][:date] }
  ).call
end
home_team() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 105
def home_team
  @home_team ||= Games::Team.new(
    @raw_data[:homeTeam],
    {
      home_team: true,
      goals: raw_goals,
      current_state: current_state,
      game_properties: game_properties,
      shots: raw_period_shots,
      goal_totals: raw_period_goals,
      winning_team_id: winning_team_id,
    }
  )
end
in_progress?() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 258
def in_progress?
  @in_progress ||= status == "in_progress"
end
info() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 66
def info
  @info ||= Games::Info.new(@raw_data[:details], { name: "#{away_team.abbreviation} @ #{home_team.abbreviation}" })
end
overtime?() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 230
def overtime?
  @overtime ||= overtimes.length.positive?
end
overtimes() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 226
def overtimes
  @overtimes ||= Array(@raw_data[:periods][3..-1]).map { |o| Games::Overtime.new(o, { regular_season: season_type == :regular }) }
end
penalties() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 206
def penalties
  @penalties ||= raw_penalties.map.with_index { |pn, i| Games::Penalty.new(pn, { number: i + 1 }) }
end
penalty_shots() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 210
def penalty_shots
  @penalty_shots ||= (home_penalty_shots || []) + (away_penalty_shots || [])
end
periods() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 222
def periods
  @periods ||= Array(@raw_data[:periods][0..2]).map { |per| Games::Period.new(per) }
end
played?() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 254
def played?
  @played ||= status == "finished"
end
referees() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 86
def referees
  @referees ||= Array((@raw_data[:referees] + @raw_data[:linesmen])).uniq { |referee| referee.values_at(:lastName, :firstName, :jerseyNumber) }
    .map { |r| Games::Referee.new(r) }
end
season_id() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 82
def season_id
  @season_id ||= @raw_data[:details][:seasonId].to_i
end
shootout?() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 250
def shootout?
  @shootout ||= @raw_data[:hasShootout] == true
end
shootout_attempts() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 234
def shootout_attempts
  @shootout_attempts ||= [*home_shootout_attempts, *away_shootout_attempts]
end
status() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 62
def status
  @status ||= set_game_status
end
three_stars() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 91
def three_stars
  @three_stars ||= Array(@raw_data[:mostValuablePlayers]).map.with_index { |t, i| Games::Star.new(t, { number: i + 1 }) }
end
values() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 56
def values
  @values ||= ATTRIBUTES.index_with do |m|
    send(m)
  end.transform_keys(&:to_sym)
end
winning_team() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 135
def winning_team
  @winning_team ||=
    if status == "finished"
      winning_team_id == home_team.id ? home_team : away_team
    end
end

Private Instance Methods

current_state() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 321
def current_state
  @current_state ||= {
    status: status,
    period: current_period,
    period_number: current_period_number,
    time: current_time,
    shootout: shootout?,
    overtime: overtime?,
  }
end
find_opposing_team(team_id) click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 303
def find_opposing_team(team_id)
  team_id == home_team.id ? away_team : home_team
end
game_properties() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 332
def game_properties
  @game_properties ||= {
    playoffs: @season_type == :playoffs,
    periods: periods.length,
    overtime_periods: overtimes.length,
    game_start_time_in_seconds: IRREGULAR_GAMES.dig(game_id.to_s, :start_time_in_seconds),
    game_end_time_in_seconds: IRREGULAR_GAMES.dig(game_id.to_s, :end_time_in_seconds),
  }
end
raw_away_shootout_attempts() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 299
def raw_away_shootout_attempts
  @raw_away_shootout_attempts ||= @raw_data.dig(:shootoutDetails, :visitingTeamShots)
end
raw_goals() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 275
def raw_goals
  @raw_goals ||= Array(@raw_data[:periods]).map { |a| a[:goals] }.flatten
end
raw_home_shootout_attempts() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 295
def raw_home_shootout_attempts
  @raw_home_shootout_attempts ||= @raw_data.dig(:shootoutDetails, :homeTeamShots)
end
raw_penalties() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 279
def raw_penalties
  @raw_penalties ||= Array(@raw_data[:periods]).map { |a| a[:penalties] }.flatten
end
raw_period_goals() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 289
def raw_period_goals
  @raw_period_goals ||= Array(@raw_data[:periods]).map do |period|
    { home: period[:stats][:homeGoals].to_i, away: period[:stats][:visitingGoals].to_i }
  end
end
raw_period_shots() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 283
def raw_period_shots
  @raw_period_shots ||= Array(@raw_data[:periods]).map do |period|
    { home: period[:stats][:homeShots].to_i, away: period[:stats][:visitingShots].to_i }
  end
end
set_current_game_period() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 364
def set_current_game_period
  return if /Final/.match?(@raw_data[:details][:status])

  period = @raw_data[:details][:status].match(/(1st|2nd|3rd|OT|[1-9]?[0-9]OT|SO)/).to_s
  return if period.empty?

  period
end
set_current_game_time() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 355
def set_current_game_time
  return if /Final/.match?(@raw_data[:details][:status])

  game_time = @raw_data[:details][:status].match(/\d{1,2}:\d{2}/).to_s
  return if game_time.empty?

  game_time
end
set_game_status() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 342
def set_game_status
  irregular_game_status = IRREGULAR_GAMES.dig(game_id.to_s, :status)
  return irregular_game_status if irregular_game_status

  # return "forfeited" if game is a forfeit, need to figure this out if it happens

  return "finished" if @raw_data[:details][:final] == "1"

  return "in_progress" if @raw_data[:details][:started] == "1"

  "not_started"
end
set_winning_team_id() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 311
def set_winning_team_id
  if @raw_data[:homeTeam][:stats][:goals] > @raw_data[:visitingTeam][:stats][:goals]
    @raw_data[:homeTeam][:info][:id]
  elsif @raw_data[:homeTeam][:stats][:goals] < @raw_data[:visitingTeam][:stats][:goals]
    @raw_data[:visitingTeam][:info][:id]
  else
    @raw_data[:shootoutDetails][:winningTeam][:id]
  end
end
winning_team_id() click to toggle source
# File lib/ahl_scraper/resources/game.rb, line 307
def winning_team_id
  @winning_team_id ||= status == "finished" ? set_winning_team_id : nil
end