Class: AhlScraper::Game

Inherits:
Resource show all
Includes:
Enumerable
Defined in:
lib/ahl_scraper/resources/game.rb

Overview

rubocop:disable Metrics/ClassLength

Constant Summary collapse

IRREGULAR_GAMES =
{
  "1022174" => { status: "result_void", start_time_in_seconds: 0, end_time_in_seconds: 1200 },
}.freeze
ATTRIBUTES =
%i[
  game_id
  season_type
  info
  current_time
  current_period
  current_period_number
  season_id
  played
  in_progress
  referees
  home_coaches
  away_coaches
  home_team
  away_team
  winning_team
  three_stars
  home_skaters
  away_skaters
  home_goalies
  away_goalies
  home_roster
  away_roster
  goals
  penalties
  penalty_shots
  periods
  overtimes
  overtime
  home_shootout_attempts
  away_shootout_attempts
  home_penalty_shots
  away_penalty_shots
  shootout
].freeze

Instance Method Summary collapse

Methods inherited from Resource

#[], #each, #inspect, #keys, #to_json

Constructor Details

#initialize(game_id, opts = {}) ⇒ Game

Returns a new instance of Game.



49
50
51
52
53
54
55
# File 'lib/ahl_scraper/resources/game.rb', line 49

def initialize(game_id, opts = {})
  raw_data = GameDataFetcher.new(game_id).call
  super(raw_data, opts)
  @game_id = game_id
  @raw_event_data = GameEventDataFetcher.new(game_id).call
  @season_type = opts[:season_type] || SeasonTypeFetcher.new(@raw_data[:details][:seasonId]).call
end

Instance Method Details

#away_coachesObject



101
102
103
104
# File 'lib/ahl_scraper/resources/game.rb', line 101

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_goaliesObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/ahl_scraper/resources/game.rb', line 180

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_shotsObject



219
220
221
# File 'lib/ahl_scraper/resources/game.rb', line 219

def away_penalty_shots
  @away_penalty_shots ||= Games::PenaltyShotsService.new(@raw_data[:penaltyShots][:visitingTeam]).call
end

#away_rosterObject



199
200
201
# File 'lib/ahl_scraper/resources/game.rb', line 199

def away_roster
  @away_roster ||= [*away_skaters, *away_goalies]
end

#away_shootout_attemptsObject



245
246
247
248
249
# File 'lib/ahl_scraper/resources/game.rb', line 245

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_skatersObject



154
155
156
157
158
159
160
161
162
163
# File 'lib/ahl_scraper/resources/game.rb', line 154

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_teamObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/ahl_scraper/resources/game.rb', line 121

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_periodObject



75
76
77
# File 'lib/ahl_scraper/resources/game.rb', line 75

def current_period
  @current_period ||= set_current_game_period
end

#current_period_numberObject



79
80
81
# File 'lib/ahl_scraper/resources/game.rb', line 79

def current_period_number
  @current_period_number ||= @raw_data[:periods].length
end

#current_timeObject



71
72
73
# File 'lib/ahl_scraper/resources/game.rb', line 71

def current_time
  @current_time ||= set_current_game_time
end

#ended_inObject



263
264
265
266
267
268
269
270
271
272
# File 'lib/ahl_scraper/resources/game.rb', line 263

def ended_in
  @ended_in ||=
    if shootout?
      "SO"
    elsif overtime?
      "OT"
    else
      "REG"
    end
end

#goalsObject



203
204
205
# File 'lib/ahl_scraper/resources/game.rb', line 203

def goals
  @goals ||= raw_goals.map.with_index { |g, i| Games::Goal.new(g, { number: i + 1 }) }
end

#home_coachesObject



96
97
98
99
# File 'lib/ahl_scraper/resources/game.rb', line 96

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_goaliesObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ahl_scraper/resources/game.rb', line 165

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_shotsObject



215
216
217
# File 'lib/ahl_scraper/resources/game.rb', line 215

def home_penalty_shots
  @home_penalty_shots ||= Games::PenaltyShotsService.new(@raw_data[:penaltyShots][:homeTeam]).call
end

#home_rosterObject



195
196
197
# File 'lib/ahl_scraper/resources/game.rb', line 195

def home_roster
  @home_roster ||= [*home_skaters, *home_goalies]
end

#home_shootout_attemptsObject



239
240
241
242
243
# File 'lib/ahl_scraper/resources/game.rb', line 239

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_skatersObject



143
144
145
146
147
148
149
150
151
152
# File 'lib/ahl_scraper/resources/game.rb', line 143

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_teamObject



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ahl_scraper/resources/game.rb', line 106

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?Boolean

Returns:

  • (Boolean)


259
260
261
# File 'lib/ahl_scraper/resources/game.rb', line 259

def in_progress?
  @in_progress ||= status == "in_progress"
end

#infoObject



67
68
69
# File 'lib/ahl_scraper/resources/game.rb', line 67

def info
  @info ||= Games::Info.new(@raw_data[:details], { name: "#{away_team.abbreviation} @ #{home_team.abbreviation}" })
end

#overtime?Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/ahl_scraper/resources/game.rb', line 231

def overtime?
  @overtime ||= overtimes.length.positive?
end

#overtimesObject



227
228
229
# File 'lib/ahl_scraper/resources/game.rb', line 227

def overtimes
  @overtimes ||= Array(@raw_data[:periods][3..]).map { |o| Games::Overtime.new(o, { regular_season: season_type == :regular }) }
end

#penaltiesObject



207
208
209
# File 'lib/ahl_scraper/resources/game.rb', line 207

def penalties
  @penalties ||= raw_penalties.map.with_index { |pn, i| Games::Penalty.new(pn, { number: i + 1 }) }
end

#penalty_shotsObject



211
212
213
# File 'lib/ahl_scraper/resources/game.rb', line 211

def penalty_shots
  @penalty_shots ||= (home_penalty_shots || []) + (away_penalty_shots || [])
end

#periodsObject



223
224
225
# File 'lib/ahl_scraper/resources/game.rb', line 223

def periods
  @periods ||= Array(@raw_data[:periods][0..2]).map { |per| Games::Period.new(per) }
end

#played?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/ahl_scraper/resources/game.rb', line 255

def played?
  @played ||= status == "finished"
end

#refereesObject



87
88
89
90
# File 'lib/ahl_scraper/resources/game.rb', line 87

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_idObject



83
84
85
# File 'lib/ahl_scraper/resources/game.rb', line 83

def season_id
  @season_id ||= @raw_data[:details][:seasonId].to_i
end

#shootout?Boolean

Returns:

  • (Boolean)


251
252
253
# File 'lib/ahl_scraper/resources/game.rb', line 251

def shootout?
  @shootout ||= @raw_data[:hasShootout] == true
end

#shootout_attemptsObject



235
236
237
# File 'lib/ahl_scraper/resources/game.rb', line 235

def shootout_attempts
  @shootout_attempts ||= [*home_shootout_attempts, *away_shootout_attempts]
end

#statusObject



63
64
65
# File 'lib/ahl_scraper/resources/game.rb', line 63

def status
  @status ||= set_game_status
end

#three_starsObject



92
93
94
# File 'lib/ahl_scraper/resources/game.rb', line 92

def three_stars
  @three_stars ||= Array(@raw_data[:mostValuablePlayers]).map.with_index { |t, i| Games::Star.new(t, { number: i + 1 }) }
end

#valuesObject



57
58
59
60
61
# File 'lib/ahl_scraper/resources/game.rb', line 57

def values
  @values ||= ATTRIBUTES.index_with do |m|
    send(m)
  end.transform_keys(&:to_sym)
end

#winning_teamObject



136
137
138
139
140
141
# File 'lib/ahl_scraper/resources/game.rb', line 136

def winning_team
  @winning_team ||=
    if status == "finished"
      winning_team_id == home_team.id ? home_team : away_team
    end
end