Class: Sportradar::Api::Soccer::Match
- Inherits:
-
Data
- Object
- Data
- Sportradar::Api::Soccer::Match
show all
- Defined in:
- lib/sportradar/api/soccer/match.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#api ⇒ Object
-
#cancelled? ⇒ Boolean
-
#changed?(key) ⇒ Boolean
-
#check_newness(key, new_object) ⇒ Object
-
#clock_display ⇒ Object
(also: #clock)
-
#closed? ⇒ Boolean
-
#completed? ⇒ Boolean
-
#finished? ⇒ Boolean
-
#future? ⇒ Boolean
-
#get_facts ⇒ Object
-
#get_lineups ⇒ Object
-
#get_probabilities ⇒ Object
-
#get_summary ⇒ Object
-
#get_timeline ⇒ Object
-
#get_tournament_id(data, **opts) ⇒ Object
-
#halftime? ⇒ Boolean
-
#ingest_facts(data) ⇒ Object
-
#ingest_lineups(data) ⇒ Object
-
#ingest_probabilities(data) ⇒ Object
-
#ingest_summary(data) ⇒ Object
-
#ingest_timeline(data) ⇒ Object
-
#initialize(data = {}, league_group: nil, **opts) ⇒ Match
constructor
-
#lineups(which = nil) ⇒ Object
-
#match_seconds ⇒ Object
(also: #game_seconds)
-
#not_updated?(key, object) ⇒ Boolean
-
#path_base ⇒ Object
-
#path_facts ⇒ Object
-
#path_lineups ⇒ Object
-
#path_probabilities ⇒ Object
-
#path_summary ⇒ Object
-
#path_timeline ⇒ Object
-
#plays_by_minute(type, minute_start, minute_end = nil) ⇒ Object
-
#postponed? ⇒ Boolean
-
#queue_facts ⇒ Object
-
#queue_lineups ⇒ Object
-
#queue_probabilities ⇒ Object
-
#queue_summary ⇒ Object
-
#queue_timeline ⇒ Object
-
#realtime_state ⇒ Object
-
#realtime_state_short ⇒ Object
-
#remember(key, object) ⇒ Object
-
#scoring ⇒ Object
-
#started? ⇒ Boolean
-
#stats(team_id) ⇒ Object
-
#team(place_or_id) ⇒ Object
-
#timeline(type = nil) ⇒ Object
-
#timeline_by_minute(minute_start, minute_end = nil) ⇒ Object
-
#title ⇒ Object
-
#unnecessary? ⇒ Boolean
-
#update(data, **opts) ⇒ Object
-
#update_player_stats(player, stats) ⇒ 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 = {}, league_group: nil, **opts) ⇒ Match
Returns a new instance of Match.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/sportradar/api/soccer/match.rb', line 13
def initialize(data = {}, league_group: nil, **opts)
@response = data
@id = data['id']
@api = opts[:api]
@updates = {}
@changes = {}
@league_group = league_group || data['league_group'] || @api&.league_group
@timeline_hash = {}
@lineups_hash = {}
get_tournament_id(data, **opts)
@scoring_raw = Scoring.new(data, game: self)
@home = Team.new(data['home'].to_h, api: api, match: self)
@away = Team.new(data['away'].to_h, api: api, match: self)
@teams_hash = { away: @away, home: @home }
@team_ids = { away: @away.id, home: @home.id }
@team_stats = {}
@player_stats = {}
update(data, **opts)
end
|
Instance Attribute Details
#aggregate_away_score ⇒ Object
Returns the value of attribute aggregate_away_score.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def aggregate_away_score
@aggregate_away_score
end
|
#aggregate_home_score ⇒ Object
Returns the value of attribute aggregate_home_score.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def aggregate_home_score
@aggregate_home_score
end
|
#aggregate_winner_id ⇒ Object
Returns the value of attribute aggregate_winner_id.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def aggregate_winner_id
@aggregate_winner_id
end
|
#away ⇒ Object
Returns the value of attribute away.
8
9
10
|
# File 'lib/sportradar/api/soccer/match.rb', line 8
def away
@away
end
|
#away_score ⇒ Object
Returns the value of attribute away_score.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def away_score
@away_score
end
|
#broadcast ⇒ Object
these are for consistency with other sports
9
10
11
|
# File 'lib/sportradar/api/soccer/match.rb', line 9
def broadcast
@broadcast
end
|
#coverage ⇒ Object
these are for consistency with other sports
9
10
11
|
# File 'lib/sportradar/api/soccer/match.rb', line 9
def coverage
@coverage
end
|
#coverage_info ⇒ Object
Returns the value of attribute coverage_info.
7
8
9
|
# File 'lib/sportradar/api/soccer/match.rb', line 7
def coverage_info
@coverage_info
end
|
#home ⇒ Object
Returns the value of attribute home.
8
9
10
|
# File 'lib/sportradar/api/soccer/match.rb', line 8
def home
@home
end
|
#home_score ⇒ Object
Returns the value of attribute home_score.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def home_score
@home_score
end
|
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def id
@id
end
|
#league_group ⇒ Object
Returns the value of attribute league_group.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def league_group
@league_group
end
|
#match_status ⇒ Object
Returns the value of attribute match_status.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def match_status
@match_status
end
|
#match_time ⇒ Object
Returns the value of attribute match_time.
10
11
12
|
# File 'lib/sportradar/api/soccer/match.rb', line 10
def match_time
@match_time
end
|
#period ⇒ Object
these are for consistency with other sports
9
10
11
|
# File 'lib/sportradar/api/soccer/match.rb', line 9
def period
@period
end
|
#player_stats ⇒ Object
Returns the value of attribute player_stats.
11
12
13
|
# File 'lib/sportradar/api/soccer/match.rb', line 11
def player_stats
@player_stats
end
|
#probabilities ⇒ Object
Returns the value of attribute probabilities.
7
8
9
|
# File 'lib/sportradar/api/soccer/match.rb', line 7
def probabilities
@probabilities
end
|
#referee ⇒ Object
Returns the value of attribute referee.
7
8
9
|
# File 'lib/sportradar/api/soccer/match.rb', line 7
def referee
@referee
end
|
#scheduled ⇒ Object
Returns the value of attribute scheduled.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def scheduled
@scheduled
end
|
#score ⇒ Object
these are for consistency with other sports
9
10
11
|
# File 'lib/sportradar/api/soccer/match.rb', line 9
def score
@score
end
|
#start_time_tbd ⇒ Object
Returns the value of attribute start_time_tbd.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def start_time_tbd
@start_time_tbd
end
|
#status ⇒ Object
Returns the value of attribute status.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def status
@status
end
|
#stoppage_time ⇒ Object
Returns the value of attribute stoppage_time.
10
11
12
|
# File 'lib/sportradar/api/soccer/match.rb', line 10
def stoppage_time
@stoppage_time
end
|
#team_stats ⇒ Object
Returns the value of attribute team_stats.
11
12
13
|
# File 'lib/sportradar/api/soccer/match.rb', line 11
def team_stats
@team_stats
end
|
#tournament_id ⇒ Object
Returns the value of attribute tournament_id.
8
9
10
|
# File 'lib/sportradar/api/soccer/match.rb', line 8
def tournament_id
@tournament_id
end
|
#tournament_round ⇒ Object
Returns the value of attribute tournament_round.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def tournament_round
@tournament_round
end
|
#venue ⇒ Object
Returns the value of attribute venue.
5
6
7
|
# File 'lib/sportradar/api/soccer/match.rb', line 5
def venue
@venue
end
|
#weather_info ⇒ Object
Returns the value of attribute weather_info.
7
8
9
|
# File 'lib/sportradar/api/soccer/match.rb', line 7
def weather_info
@weather_info
end
|
#winner_id ⇒ Object
Returns the value of attribute winner_id.
6
7
8
|
# File 'lib/sportradar/api/soccer/match.rb', line 6
def winner_id
@winner_id
end
|
Instance Method Details
#api ⇒ Object
250
251
252
|
# File 'lib/sportradar/api/soccer/match.rb', line 250
def api
@api ||= Sportradar::Api::Soccer::Api.new(league_group: @league_group)
end
|
#cancelled? ⇒ Boolean
139
140
141
|
# File 'lib/sportradar/api/soccer/match.rb', line 139
def cancelled?
['unnecessary', 'postponed'].include? status
end
|
#changed?(key) ⇒ Boolean
347
348
349
|
# File 'lib/sportradar/api/soccer/match.rb', line 347
def changed?(key)
@changes[key]
end
|
#check_newness(key, new_object) ⇒ Object
350
351
352
353
|
# File 'lib/sportradar/api/soccer/match.rb', line 350
def check_newness(key, new_object)
@changes[key] = !not_updated?(key, new_object)
remember(key, new_object)
end
|
#clock_display ⇒ Object
Also known as:
clock
158
159
160
161
162
163
164
165
|
# File 'lib/sportradar/api/soccer/match.rb', line 158
def clock_display
return unless @match_time
if @match_time == '45:00' || @match_time == '90:00' @match_time.split(':').first + ?'
else
@match_time.split(':').first + ?'
end
end
|
#closed? ⇒ Boolean
154
155
156
|
# File 'lib/sportradar/api/soccer/match.rb', line 154
def closed?
'closed' == status
end
|
#completed? ⇒ Boolean
151
152
153
|
# File 'lib/sportradar/api/soccer/match.rb', line 151
def completed?
['ended', 'complete'].include? status
end
|
#finished? ⇒ Boolean
148
149
150
|
# File 'lib/sportradar/api/soccer/match.rb', line 148
def finished?
['ended', 'complete', 'closed'].include?(@status) || ['ended'].include?(@match_status)
end
|
#future? ⇒ Boolean
142
143
144
|
# File 'lib/sportradar/api/soccer/match.rb', line 142
def future?
['not_started', 'scheduled', 'delayed', 'created', 'time-tbd', 'if-necessary'].include? status
end
|
#get_facts ⇒ Object
293
294
295
296
|
# File 'lib/sportradar/api/soccer/match.rb', line 293
def get_facts
data = api.get_data(path_facts).to_h
ingest_facts(data)
end
|
#get_lineups ⇒ Object
277
278
279
280
|
# File 'lib/sportradar/api/soccer/match.rb', line 277
def get_lineups
data = api.get_data(path_lineups).to_h
ingest_lineups(data)
end
|
#get_probabilities ⇒ Object
309
310
311
312
|
# File 'lib/sportradar/api/soccer/match.rb', line 309
def get_probabilities
data = api.get_data(path_probabilities).to_h
ingest_probabilities(data)
end
|
#get_summary ⇒ Object
261
262
263
264
|
# File 'lib/sportradar/api/soccer/match.rb', line 261
def get_summary
data = api.get_data(path_summary).to_h
ingest_summary(data)
end
|
#get_timeline ⇒ Object
325
326
327
328
|
# File 'lib/sportradar/api/soccer/match.rb', line 325
def get_timeline
data = api.get_data(path_timeline).to_h
ingest_timeline(data)
end
|
#get_tournament_id(data, **opts) ⇒ Object
203
204
205
206
207
208
209
210
211
212
213
214
215
|
# File 'lib/sportradar/api/soccer/match.rb', line 203
def get_tournament_id(data, **opts)
@tournament_id ||= if opts[:tournament]
opts[:tournament].id
elsif opts[:season]
opts[:season].tournament_id
elsif opts[:match]
opts[:match].tournament_id
elsif data['tournament']
data.dig('tournament', 'id')
elsif data['season']
data.dig('season', 'tournament_id')
end
end
|
#halftime? ⇒ Boolean
129
130
131
|
# File 'lib/sportradar/api/soccer/match.rb', line 129
def halftime?
@match_status == "halftime"
end
|
#ingest_facts(data) ⇒ Object
297
298
299
300
|
# File 'lib/sportradar/api/soccer/match.rb', line 297
def ingest_facts(data)
update(data)
data
end
|
#ingest_lineups(data) ⇒ Object
281
282
283
284
|
# File 'lib/sportradar/api/soccer/match.rb', line 281
def ingest_lineups(data)
update(data)
data
end
|
#ingest_probabilities(data) ⇒ Object
313
314
315
316
|
# File 'lib/sportradar/api/soccer/match.rb', line 313
def ingest_probabilities(data)
update(data)
data
end
|
#ingest_summary(data) ⇒ Object
265
266
267
268
|
# File 'lib/sportradar/api/soccer/match.rb', line 265
def ingest_summary(data)
update(data)
data
end
|
#ingest_timeline(data) ⇒ Object
329
330
331
332
333
334
|
# File 'lib/sportradar/api/soccer/match.rb', line 329
def ingest_timeline(data)
update(data, source: :pbp)
check_newness(:pbp, timeline.last&.updated)
check_newness(:clock, self.match_seconds.to_s)
data
end
|
#lineups(which = nil) ⇒ Object
242
243
244
245
246
247
248
|
# File 'lib/sportradar/api/soccer/match.rb', line 242
def lineups(which = nil)
if which
@lineups_hash[which.to_s]
else
@lineups_hash.values
end
end
|
#match_seconds ⇒ Object
Also known as:
game_seconds
168
169
170
171
172
173
174
175
176
177
178
|
# File 'lib/sportradar/api/soccer/match.rb', line 168
def match_seconds
return nil unless @match_time
mm, ss = @match_time.split(':').map(&:to_i)
time = mm * 60 + ss
if @stoppage_time && (@match_time == '45:00' || @match_time == '90:00') mm, ss = @stoppage_time.split(':').map(&:to_i)
stop_time = mm * 60 + ss
time += stop_time
end
time
end
|
#not_updated?(key, object) ⇒ Boolean
344
345
346
|
# File 'lib/sportradar/api/soccer/match.rb', line 344
def not_updated?(key, object)
@updates[key] == object
end
|
#path_base ⇒ Object
254
255
256
|
# File 'lib/sportradar/api/soccer/match.rb', line 254
def path_base
"matches/#{ id }"
end
|
#path_facts ⇒ Object
290
291
292
|
# File 'lib/sportradar/api/soccer/match.rb', line 290
def path_facts
"#{ path_base }/funfacts"
end
|
#path_lineups ⇒ Object
274
275
276
|
# File 'lib/sportradar/api/soccer/match.rb', line 274
def path_lineups
"#{ path_base }/lineups"
end
|
#path_probabilities ⇒ Object
306
307
308
|
# File 'lib/sportradar/api/soccer/match.rb', line 306
def path_probabilities
"#{ path_base }/probabilities"
end
|
#path_summary ⇒ Object
258
259
260
|
# File 'lib/sportradar/api/soccer/match.rb', line 258
def path_summary
"#{ path_base }/summary"
end
|
#path_timeline ⇒ Object
322
323
324
|
# File 'lib/sportradar/api/soccer/match.rb', line 322
def path_timeline
"#{ path_base }/timeline"
end
|
#plays_by_minute(type, minute_start, minute_end = nil) ⇒ Object
237
238
239
|
# File 'lib/sportradar/api/soccer/match.rb', line 237
def plays_by_minute(type, minute_start, minute_end = nil)
timeline_by_minute(minute_start, minute_end).select { |ev| ev.type == type }
end
|
#postponed? ⇒ Boolean
133
134
135
|
# File 'lib/sportradar/api/soccer/match.rb', line 133
def postponed?
'postponed' == status
end
|
#queue_facts ⇒ Object
301
302
303
304
|
# File 'lib/sportradar/api/soccer/match.rb', line 301
def queue_facts
url, , options, timeout = api.get_request_info(path_facts)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_facts)}
end
|
#queue_lineups ⇒ Object
285
286
287
288
|
# File 'lib/sportradar/api/soccer/match.rb', line 285
def queue_lineups
url, , options, timeout = api.get_request_info(path_lineups)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_lineups)}
end
|
#queue_probabilities ⇒ Object
317
318
319
320
|
# File 'lib/sportradar/api/soccer/match.rb', line 317
def queue_probabilities
url, , options, timeout = api.get_request_info(path_probabilities)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_probabilities)}
end
|
#queue_summary ⇒ Object
269
270
271
272
|
# File 'lib/sportradar/api/soccer/match.rb', line 269
def queue_summary
url, , options, timeout = api.get_request_info(path_summary)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_summary)}
end
|
#queue_timeline ⇒ Object
335
336
337
338
|
# File 'lib/sportradar/api/soccer/match.rb', line 335
def queue_timeline
url, , options, timeout = api.get_request_info(path_timeline)
{url: url, headers: , params: options, timeout: timeout, callback: method(:ingest_timeline)}
end
|
#realtime_state ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/sportradar/api/soccer/match.rb', line 115
def realtime_state
if future?
'Scheduled'
elsif finished?
'Final'
elsif postponed?
'Postponed'
elsif halftime?
'Halftime'
else
clock_display
end
end
|
#realtime_state_short ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/sportradar/api/soccer/match.rb', line 101
def realtime_state_short
if future?
'Scheduled' elsif finished?
'FT'
elsif postponed?
'PPD'
elsif halftime?
'HT'
else
clock_display
end
end
|
#remember(key, object) ⇒ Object
341
342
343
|
# File 'lib/sportradar/api/soccer/match.rb', line 341
def remember(key, object)
@updates[key] = object&.dup
end
|
#scoring ⇒ Object
93
94
95
|
# File 'lib/sportradar/api/soccer/match.rb', line 93
def scoring
@scoring_raw.scores.each { |period, hash| hash[@home.id] = hash['home']; hash[@away.id] = hash['away'] }
end
|
#started? ⇒ Boolean
145
146
147
|
# File 'lib/sportradar/api/soccer/match.rb', line 145
def started?
['live'].include?(@status) || ['halftime', '1st_half', '2nd_half'].include?(@match_status)
end
|
#stats(team_id) ⇒ Object
199
200
201
|
# File 'lib/sportradar/api/soccer/match.rb', line 199
def stats(team_id)
team_id.is_a?(Symbol) ? @team_stats[@team_ids[team_id]] : @team_stats[team_id]
end
|
#team(place_or_id) ⇒ Object
217
218
219
|
# File 'lib/sportradar/api/soccer/match.rb', line 217
def team(place_or_id)
@teams_hash[place_or_id]
end
|
#timeline(type = nil) ⇒ Object
221
222
223
224
225
226
227
|
# File 'lib/sportradar/api/soccer/match.rb', line 221
def timeline(type = nil)
if type
@timeline_hash.each_value.select { |ev| ev.type == type }
else
@timeline_hash.values
end
end
|
#timeline_by_minute(minute_start, minute_end = nil) ⇒ Object
229
230
231
232
233
234
235
|
# File 'lib/sportradar/api/soccer/match.rb', line 229
def timeline_by_minute(minute_start, minute_end = nil)
if minute_end
@timeline_hash.each_value.select { |ev| (minute_start..minute_end).cover?(ev.match_time.to_i) }
else
@timeline_hash.each_value.select { |ev| minute_start === ev.match_time }
end
end
|
#title ⇒ Object
90
91
92
|
# File 'lib/sportradar/api/soccer/match.rb', line 90
def title
[@home, @away].compact.map(&:name).join(' vs ')
end
|
#unnecessary? ⇒ Boolean
136
137
138
|
# File 'lib/sportradar/api/soccer/match.rb', line 136
def unnecessary?
'unnecessary' == status
end
|
#update(data, **opts) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/sportradar/api/soccer/match.rb', line 36
def update(data, **opts)
@league_group = opts[:league_group] || data['league_group'] || @league_group
get_tournament_id(data, **opts)
if data["sport_event"]
update(data["sport_event"])
end
if data["sport_event_status"]
update(data["sport_event_status"])
end
if data['sport_event_conditions']
update(data['sport_event_conditions'])
end
if data['probabilities']
@probabilities = data['probabilities'] end
if data['lineups']
create_data(@lineups_hash, data['lineups'], klass: Lineup, identifier: 'team', api: api)
end
if (stats = data.dig('statistics', 'teams'))
update_teams(stats)
end
@scheduled = Time.parse(data['scheduled']) if data['scheduled']
@start_time_tbd = data['start_time_tbd'] if data.key?('start_time_tbd')
@status = data['status'] if data['status']
@match_status = data['match_status'] if data['match_status']
@tournament_round = data['tournament_round'] if data['tournament_round']
@venue = Venue.new(data['venue']) if data['venue']
@weather_info = OpenStruct.new(data["weather_info"]) if data["weather_info"]
@referee = OpenStruct.new(data["referee"]) if data["referee"]
@coverage_info = OpenStruct.new(data["coverage_info"]) if data["coverage_info"]
@home_score = data['home_score'] if data['home_score']
@away_score = data['away_score'] if data['away_score']
@period = data['period'] if data['period']
@match_time = data.dig('clock', 'match_time') if data.dig('clock', 'match_time')
@stoppage_time = data.dig('clock', 'stoppage_time')
@ball_locations = data['ball_locations'] if data['ball_locations']
@winner_id = data['winner_id'] if data['winner_id']
@aggregate_home_score = data['aggregate_home_score'] if data['aggregate_home_score']
@aggregate_away_score = data['aggregate_away_score'] if data['aggregate_away_score']
@aggregate_winner_id = data['aggregate_winner_id'] if data['aggregate_winner_id']
@scoring_raw.update(data, source: opts[:source])
create_data(@timeline_hash, data['timeline'], klass: Event, api: api)
if data['competitors']
update_teams(data['competitors'])
end
end
|
#update_player_stats(player, stats) ⇒ Object
196
197
198
|
# File 'lib/sportradar/api/soccer/match.rb', line 196
def update_player_stats(player, stats)
@player_stats.merge!(player.id => stats.merge!(player: player))
end
|
#update_stats(team, stats) ⇒ Object
193
194
195
|
# File 'lib/sportradar/api/soccer/match.rb', line 193
def update_stats(team, stats)
@team_stats.merge!(team.id => stats.merge(team: team))
end
|
#update_teams(data) ⇒ Object
181
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/sportradar/api/soccer/match.rb', line 181
def update_teams(data)
home_hash = data.detect { |team_hash| team_hash["qualifier"] == "home" || team_hash["team"] == "home" }
away_hash = (data - [home_hash]).first
if home_hash && away_hash
@home.update(home_hash, match: self)
@away.update(away_hash, match: self)
@teams_hash[@home.id] = @home
@teams_hash[@away.id] = @away
@team_ids = { away: @away.id, home: @home.id }
end
end
|