Class: Sportradar::Api::Football::Game
- Inherits:
-
Data
- Object
- Data
- Sportradar::Api::Football::Game
show all
- Defined in:
- lib/sportradar/api/football/game.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#assign_away(team) ⇒ Object
-
#assign_home(team) ⇒ Object
-
#away ⇒ Object
-
#away_alias ⇒ Object
-
#box ⇒ Object
-
#cancelled? ⇒ Boolean
-
#changed?(key) ⇒ Boolean
-
#check_newness(key, new_object) ⇒ Object
-
#clock_display ⇒ Object
-
#closed? ⇒ Boolean
-
#completed? ⇒ Boolean
-
#current_possession_team_id ⇒ Object
-
#delayed? ⇒ Boolean
-
#drives ⇒ Object
-
#drives_with_events ⇒ Object
-
#events ⇒ Object
-
#finished? ⇒ Boolean
-
#future? ⇒ Boolean
-
#generate_title ⇒ Object
-
#get_box ⇒ Object
-
#get_pbp ⇒ Object
-
#get_statistics ⇒ Object
-
#half_quarters ⇒ Object
-
#halftime? ⇒ Boolean
-
#home ⇒ Object
def update_from_team(id, data) end.
-
#home_alias ⇒ Object
-
#ingest_box(data) ⇒ Object
-
#ingest_pbp(data) ⇒ Object
-
#ingest_statistics(data) ⇒ Object
-
#initialize(data, **opts) ⇒ Game
constructor
-
#leading_team ⇒ Object
-
#leading_team_id ⇒ Object
-
#next_possession_team_id ⇒ Object
-
#not_updated?(key, object) ⇒ Boolean
-
#parse_score(data) ⇒ Object
-
#path_box ⇒ Object
-
#path_extended_box ⇒ Object
-
#path_pbp ⇒ Object
-
#path_roster ⇒ Object
-
#path_statistics ⇒ Object
-
#path_summary ⇒ Object
-
#pbp ⇒ Object
-
#period ⇒ Object
-
#period_key ⇒ Object
-
#plays ⇒ Object
-
#postponed? ⇒ Boolean
-
#quarter_class ⇒ Object
-
#quarter_display ⇒ Object
-
#quarters ⇒ Object
def summary @summary ||= get_summary end.
-
#queue_pbp ⇒ Object
-
#queue_statistics ⇒ Object
-
#realtime_state ⇒ Object
-
#remember(key, object) ⇒ Object
-
#scoring ⇒ Object
-
#sim! ⇒ Object
-
#started? ⇒ Boolean
-
#stats(team_id) ⇒ Object
-
#summary_stat(team_id, stat_name) ⇒ Object
-
#team(team_id) ⇒ Object
-
#team_class ⇒ Object
-
#team_ids ⇒ Object
-
#tied? ⇒ Boolean
-
#timeouts ⇒ Object
-
#unnecessary? ⇒ Boolean
-
#update(data, source: nil, **opts) ⇒ Object
-
#update_drives(data, **opts) ⇒ Object
-
#update_player_stats(player, stats) ⇒ Object
-
#update_score(score) ⇒ Object
-
#update_stats(team, stats) ⇒ Object
-
#update_teams(data) ⇒ Object
Methods inherited from Data
#all_attributes, #attributes, #create_data, #parse_into_array, #parse_into_array_with_options, #parse_out_hashes, #structure_links, #update_data
Constructor Details
#initialize(data, **opts) ⇒ Game
Returns a new instance of Game.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/sportradar/api/football/game.rb', line 9
def initialize(data, **opts)
@response = data
@api = opts[:api]
@scoring_raw = Scoring.new(data, game: self)
@home = team_class.new({}, api: api, game: self)
@away = team_class.new({}, api: api, game: self)
@updates = {}
@changes = {}
@teams_hash = {}
@team_stats = {}
@player_stats = {}
@quarters_hash = {}
@drives_hash = {}
@score = {}
update(data, **opts)
end
|
Instance Attribute Details
#api ⇒ Object
Returns the value of attribute api.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def api
@api
end
|
#attendance ⇒ Object
Returns the value of attribute attendance.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def attendance
@attendance
end
|
#away_id ⇒ Object
Returns the value of attribute away_id.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def away_id
@away_id
end
|
#broadcast ⇒ Object
Returns the value of attribute broadcast.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def broadcast
@broadcast
end
|
#changes ⇒ Object
Returns the value of attribute changes.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def changes
@changes
end
|
#clock ⇒ Object
Returns the value of attribute clock.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def clock
@clock
end
|
#coverage ⇒ Object
Returns the value of attribute coverage.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def coverage
@coverage
end
|
#duration ⇒ Object
Returns the value of attribute duration.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def duration
@duration
end
|
#home_id ⇒ Object
Returns the value of attribute home_id.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def home_id
@home_id
end
|
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def id
@id
end
|
#lineup ⇒ Object
Returns the value of attribute lineup.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def lineup
@lineup
end
|
#player_stats ⇒ Object
Returns the value of attribute player_stats.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def player_stats
@player_stats
end
|
#quarter ⇒ Object
Returns the value of attribute quarter.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def quarter
@quarter
end
|
#response ⇒ Object
Returns the value of attribute response.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def response
@response
end
|
#scheduled ⇒ Object
Returns the value of attribute scheduled.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def scheduled
@scheduled
end
|
#score ⇒ Object
Returns the value of attribute score.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def score
@score
end
|
#status ⇒ Object
Returns the value of attribute status.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def status
@status
end
|
#team_stats ⇒ Object
Returns the value of attribute team_stats.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def team_stats
@team_stats
end
|
#title ⇒ Object
Returns the value of attribute title.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def title
@title
end
|
#type ⇒ Object
Returns the value of attribute type.
7
8
9
|
# File 'lib/sportradar/api/football/game.rb', line 7
def type
@type
end
|
#venue ⇒ Object
Returns the value of attribute venue.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def venue
@venue
end
|
#week ⇒ Object
Returns the value of attribute week.
5
6
7
|
# File 'lib/sportradar/api/football/game.rb', line 5
def week
@week
end
|
#week_number ⇒ Object
Returns the value of attribute week_number.
7
8
9
|
# File 'lib/sportradar/api/football/game.rb', line 7
def week_number
@week_number
end
|
#year ⇒ Object
Returns the value of attribute year.
7
8
9
|
# File 'lib/sportradar/api/football/game.rb', line 7
def year
@year
end
|
Instance Method Details
#assign_away(team) ⇒ Object
196
197
198
199
|
# File 'lib/sportradar/api/football/game.rb', line 196
def assign_away(team)
@away_id = team.id
@teams_hash[team.id] = team
end
|
#assign_home(team) ⇒ Object
191
192
193
194
|
# File 'lib/sportradar/api/football/game.rb', line 191
def assign_home(team)
@home_id = team.id
@teams_hash[team.id] = team
end
|
#away ⇒ Object
144
145
146
|
# File 'lib/sportradar/api/football/game.rb', line 144
def away
@teams_hash[@away_id] || @away
end
|
#away_alias ⇒ Object
148
149
150
|
# File 'lib/sportradar/api/football/game.rb', line 148
def away_alias
@away_alias || @away&.alias
end
|
#box ⇒ Object
201
202
203
|
# File 'lib/sportradar/api/football/game.rb', line 201
def box
@box ||= get_box
end
|
#cancelled? ⇒ Boolean
298
299
300
|
# File 'lib/sportradar/api/football/game.rb', line 298
def cancelled?
['unnecessary', 'postponed'].include? status
end
|
#changed?(key) ⇒ Boolean
249
250
251
|
# File 'lib/sportradar/api/football/game.rb', line 249
def changed?(key)
@changes[key]
end
|
#check_newness(key, new_object) ⇒ Object
253
254
255
256
|
# File 'lib/sportradar/api/football/game.rb', line 253
def check_newness(key, new_object)
@changes[key] = !not_updated?(key, new_object)
remember(key, new_object)
end
|
#clock_display ⇒ Object
275
276
277
278
279
|
# File 'lib/sportradar/api/football/game.rb', line 275
def clock_display
if clock && quarter
"#{clock} #{quarter_display}"
end
end
|
#closed? ⇒ Boolean
326
327
328
|
# File 'lib/sportradar/api/football/game.rb', line 326
def closed?
'closed' == status
end
|
#completed? ⇒ Boolean
322
323
324
|
# File 'lib/sportradar/api/football/game.rb', line 322
def completed?
'complete' == status
end
|
#current_possession_team_id ⇒ Object
175
176
177
|
# File 'lib/sportradar/api/football/game.rb', line 175
def current_possession_team_id
drives.last&.team_id
end
|
#delayed? ⇒ Boolean
302
303
304
|
# File 'lib/sportradar/api/football/game.rb', line 302
def delayed?
'delayed' == status
end
|
#drives ⇒ Object
216
217
218
|
# File 'lib/sportradar/api/football/game.rb', line 216
def drives
drives_with_events.grep(Sportradar::Api::Football::Drive)
end
|
#drives_with_events ⇒ Object
212
213
214
|
# File 'lib/sportradar/api/football/game.rb', line 212
def drives_with_events
@drives_hash.values
end
|
#events ⇒ Object
225
226
227
|
# File 'lib/sportradar/api/football/game.rb', line 225
def events
drives.flat_map(&:events).compact
end
|
#finished? ⇒ Boolean
318
319
320
|
# File 'lib/sportradar/api/football/game.rb', line 318
def finished?
['complete', 'closed'].include? status
end
|
#future? ⇒ Boolean
306
307
308
|
# File 'lib/sportradar/api/football/game.rb', line 306
def future?
['scheduled', 'created', 'time-tbd'].include? status
end
|
#generate_title ⇒ Object
111
112
113
|
# File 'lib/sportradar/api/football/game.rb', line 111
def generate_title
(home && away && "#{home.full_name} vs #{away.full_name}")
end
|
#get_box ⇒ Object
352
353
354
355
|
# File 'lib/sportradar/api/football/game.rb', line 352
def get_box
data = api.get_data(path_box).to_h
ingest_box(data)
end
|
#get_pbp ⇒ Object
371
372
373
374
|
# File 'lib/sportradar/api/football/game.rb', line 371
def get_pbp
data = api.get_data(path_pbp).to_h
ingest_pbp(data)
end
|
#get_statistics ⇒ Object
392
393
394
395
|
# File 'lib/sportradar/api/football/game.rb', line 392
def get_statistics
data = api.get_data(path_statistics).to_h
ingest_statistics(data)
end
|
#half_quarters ⇒ Object
236
237
238
|
# File 'lib/sportradar/api/football/game.rb', line 236
def half_quarters
quarters.flat_map(&:half_quarters)
end
|
#halftime? ⇒ Boolean
314
315
316
|
# File 'lib/sportradar/api/football/game.rb', line 314
def halftime?
'halftime' == status
end
|
#home ⇒ Object
def update_from_team(id, data) end
136
137
138
|
# File 'lib/sportradar/api/football/game.rb', line 136
def home
@teams_hash[@home_id] || @home
end
|
#home_alias ⇒ Object
140
141
142
|
# File 'lib/sportradar/api/football/game.rb', line 140
def home_alias
@home_alias || @home&.alias
end
|
#ingest_box(data) ⇒ Object
357
358
359
360
361
362
363
364
|
# File 'lib/sportradar/api/football/game.rb', line 357
def ingest_box(data)
data = data
update(data, source: :box)
check_newness(:box, @clock)
data
end
|
#ingest_pbp(data) ⇒ Object
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/sportradar/api/football/game.rb', line 376
def ingest_pbp(data)
data = data
update(data, source: :pbp)
create_data(@quarters_hash, data[period_key], klass: quarter_class, identifier: quarter_class.period_index, api: api, game: self) if data[period_key]
check_newness(:pbp, plays.last&.description)
check_newness(:score, @score)
@pbp = @quarters_hash.values
data
end
|
#ingest_statistics(data) ⇒ Object
402
403
404
405
406
407
408
|
# File 'lib/sportradar/api/football/game.rb', line 402
def ingest_statistics(data)
update(data, source: :statistics)
check_newness(:statistics, @clock)
data
end
|
#leading_team ⇒ Object
171
172
173
|
# File 'lib/sportradar/api/football/game.rb', line 171
def leading_team
@teams_hash[leading_team_id] || (@away_id == leading_team_id && away) || (@home_id == leading_team_id && home)
end
|
#leading_team_id ⇒ Object
166
167
168
169
|
# File 'lib/sportradar/api/football/game.rb', line 166
def leading_team_id
return nil if tied?
score.max_by(&:last).first
end
|
#next_possession_team_id ⇒ Object
179
180
181
|
# File 'lib/sportradar/api/football/game.rb', line 179
def next_possession_team_id
(@teams_hash.keys - [current_possession_team_id]).first || (@away_id == current_possession_team_id && @home_id) || (@home_id == current_possession_team_id && @away_id)
end
|
#not_updated?(key, object) ⇒ Boolean
245
246
247
|
# File 'lib/sportradar/api/football/game.rb', line 245
def not_updated?(key, object)
@updates[key] == object
end
|
#parse_score(data) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/sportradar/api/football/game.rb', line 54
def parse_score(data)
end
|
#path_box ⇒ Object
331
332
333
|
# File 'lib/sportradar/api/football/game.rb', line 331
def path_box
"#{ path_base }/boxscore"
end
|
#path_extended_box ⇒ Object
334
335
336
|
# File 'lib/sportradar/api/football/game.rb', line 334
def path_extended_box
"#{ path_base }/extended-boxscore"
end
|
#path_pbp ⇒ Object
337
338
339
|
# File 'lib/sportradar/api/football/game.rb', line 337
def path_pbp
"#{ path_base }/pbp"
end
|
#path_roster ⇒ Object
340
341
342
|
# File 'lib/sportradar/api/football/game.rb', line 340
def path_roster
"#{ path_base }/roster"
end
|
#path_statistics ⇒ Object
343
344
345
|
# File 'lib/sportradar/api/football/game.rb', line 343
def path_statistics
"#{ path_base }/statistics"
end
|
#path_summary ⇒ Object
346
347
348
|
# File 'lib/sportradar/api/football/game.rb', line 346
def path_summary
"#{ path_base }/summary"
end
|
#pbp ⇒ Object
205
206
207
208
209
210
|
# File 'lib/sportradar/api/football/game.rb', line 205
def pbp
if !future? && quarters.empty?
get_pbp
end
@pbp ||= quarters
end
|
#period ⇒ Object
36
37
38
|
# File 'lib/sportradar/api/football/game.rb', line 36
def period
quarter
end
|
#period_key ⇒ Object
388
389
390
|
# File 'lib/sportradar/api/football/game.rb', line 388
def period_key
'quarters'
end
|
#plays ⇒ Object
221
222
223
|
# File 'lib/sportradar/api/football/game.rb', line 221
def plays
drives.flat_map(&:plays).compact
end
|
#postponed? ⇒ Boolean
290
291
292
|
# File 'lib/sportradar/api/football/game.rb', line 290
def postponed?
'postponed' == status
end
|
#quarter_display ⇒ Object
280
281
282
283
284
285
286
287
288
|
# File 'lib/sportradar/api/football/game.rb', line 280
def quarter_display
if quarter > 5
"#{quarter - 4}OT"
elsif quarter == 5
'OT'
else
"#{quarter}Q"
end
end
|
#quarters ⇒ Object
def summary
@summary ||= get_summary
end
233
234
235
|
# File 'lib/sportradar/api/football/game.rb', line 233
def quarters
@quarters_hash.values
end
|
#queue_pbp ⇒ Object
366
367
368
369
|
# File 'lib/sportradar/api/football/game.rb', line 366
def queue_pbp
url, , options, timeout = api.get_request_info(path_pbp)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_pbp)}
end
|
#queue_statistics ⇒ Object
397
398
399
400
|
# File 'lib/sportradar/api/football/game.rb', line 397
def queue_statistics
url, , options, timeout = api.get_request_info(path_statistics)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_statistics)}
end
|
#realtime_state ⇒ Object
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/sportradar/api/football/game.rb', line 259
def realtime_state
if future?
'Scheduled'
elsif delayed?
'Delayed'
elsif finished?
'Final'
elsif postponed?
'Postponed'
elsif halftime?
'Half'
else
clock_display
end
end
|
#remember(key, object) ⇒ Object
241
242
243
|
# File 'lib/sportradar/api/football/game.rb', line 241
def remember(key, object)
@updates[key] = object&.dup
end
|
#scoring ⇒ Object
155
156
157
|
# File 'lib/sportradar/api/football/game.rb', line 155
def scoring
@scoring_raw.scores
end
|
#sim! ⇒ Object
418
419
420
421
|
# File 'lib/sportradar/api/football/game.rb', line 418
def sim!
@api = api.sim!
self
end
|
#started? ⇒ Boolean
310
311
312
|
# File 'lib/sportradar/api/football/game.rb', line 310
def started?
['inprogress', 'halftime', 'wdelay', 'delayed'].include? status
end
|
#stats(team_id) ⇒ Object
43
44
45
|
# File 'lib/sportradar/api/football/game.rb', line 43
def stats(team_id)
team_id.is_a?(Symbol) ? @team_stats[@team_ids[team_id]] : @team_stats[team_id]
end
|
#summary_stat(team_id, stat_name) ⇒ Object
40
41
42
|
# File 'lib/sportradar/api/football/game.rb', line 40
def summary_stat(team_id, stat_name)
scoring.dig(team_id, stat_name)
end
|
#team(team_id) ⇒ Object
187
188
189
|
# File 'lib/sportradar/api/football/game.rb', line 187
def team(team_id)
@teams_hash[team_id]
end
|
#team_class ⇒ Object
414
415
416
|
# File 'lib/sportradar/api/football/game.rb', line 414
def team_class
Team
end
|
#team_ids ⇒ Object
183
184
185
|
# File 'lib/sportradar/api/football/game.rb', line 183
def team_ids
@teams_hash.keys
end
|
#tied? ⇒ Boolean
152
153
154
|
# File 'lib/sportradar/api/football/game.rb', line 152
def tied?
@score[away_id].to_i == @score[home_id].to_i
end
|
#timeouts ⇒ Object
32
33
34
|
# File 'lib/sportradar/api/football/game.rb', line 32
def timeouts
@teams_hash.map { |t_id, team| [t_id, team.timeouts] }.to_h
end
|
#unnecessary? ⇒ Boolean
294
295
296
|
# File 'lib/sportradar/api/football/game.rb', line 294
def unnecessary?
'unnecessary' == status
end
|
#update(data, source: nil, **opts) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/sportradar/api/football/game.rb', line 67
def update(data, source: nil, **opts)
@id = data['id'] || @id
@week_number = data['week_number'] || week&.number || opts[:week]&.number || @week_number
@year = data['year'] || week&.year || opts[:week]&.year || @year
@type = data['type'] || week&.type || opts[:week]&.type || @type
@coverage = data['coverage']
@scheduled = Time.parse(data["scheduled"]) if data["scheduled"]
update_teams(data)
@status = data['status'] || @status
@clock = data['clock'] || @clock
@quarter = data['quarter'] || @quarter
@home_rotation = data['home_rotation']
@away_rotation = data['away_rotation']
@neutral_site = data['neutral_site']
@home_points = data['home_points']
@away_points = data['away_points']
@venue = Venue.new(data["venue"]) if data["venue"]
@weather = data['weather']
@broadcast = Broadcast.new(data['broadcast']) if !data['broadcast'].to_h.empty?
@attendance = data['attendance']
@title = data['title'] || @title || generate_title
@teams_hash = { @home.id => @home, @away.id => @away } if @home.id && @away.id
@team_ids = { home: (@home&.id || home_alias), away: (@away&.id || away_alias) }
@scoring_raw.update(data, source: source)
if data['statistics']
@home.update({ 'statistics' => data.dig('statistics', 'home')}, game: self)
@away.update({ 'statistics' => data.dig('statistics', 'away')}, game: self)
end
create_data(@teams_hash, data['team'], klass: team_class, api: api, game: self) if data['team']
self
end
|
#update_drives(data, **opts) ⇒ Object
162
163
164
|
# File 'lib/sportradar/api/football/game.rb', line 162
def update_drives(data, **opts)
create_data(@drives_hash, data, klass: drive_class, api: api, game: self, **opts)
end
|
#update_player_stats(player, stats) ⇒ Object
50
51
52
|
# File 'lib/sportradar/api/football/game.rb', line 50
def update_player_stats(player, stats)
@player_stats.merge!(player.id => stats.merge!(player: player))
end
|
#update_score(score) ⇒ Object
158
159
160
|
# File 'lib/sportradar/api/football/game.rb', line 158
def update_score(score)
@score.merge!(score)
end
|
#update_stats(team, stats) ⇒ Object
47
48
49
|
# File 'lib/sportradar/api/football/game.rb', line 47
def update_stats(team, stats)
@team_stats.merge!(team.id => stats)
end
|
#update_teams(data) ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/sportradar/api/football/game.rb', line 115
def update_teams(data)
if data['summary']
@home.update(data.dig('summary', 'home'), game: self)
@away.update(data.dig('summary', 'away'), game: self)
else
@home.update(data['home_team'], game: self) if data['home_team'].is_a?(Hash)
@away.update(data['away_team'], game: self) if data['away_team'].is_a?(Hash)
if data['home'].is_a?(String) @home_alias = data['home']
@home.id ||= @home_alias
end
if data['away'].is_a?(String) @away_alias = data['away']
@away.id ||= @away_alias
end
end
end
|