Class: Sportradar::Api::Basketball::Game
- Defined in:
- lib/sportradar/api/basketball/game.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attendance ⇒ Object
Returns the value of attribute attendance.
-
#away_id ⇒ Object
Returns the value of attribute away_id.
-
#broadcast ⇒ Object
Returns the value of attribute broadcast.
-
#changes ⇒ Object
Returns the value of attribute changes.
-
#clock ⇒ Object
Returns the value of attribute clock.
-
#coverage ⇒ Object
Returns the value of attribute coverage.
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#home_id ⇒ Object
Returns the value of attribute home_id.
-
#id ⇒ Object
Returns the value of attribute id.
-
#media_timeouts ⇒ Object
Returns the value of attribute media_timeouts.
-
#period ⇒ Object
Returns the value of attribute period.
-
#player_stats ⇒ Object
Returns the value of attribute player_stats.
-
#response ⇒ Object
Returns the value of attribute response.
-
#scheduled ⇒ Object
Returns the value of attribute scheduled.
-
#score ⇒ Object
Returns the value of attribute score.
-
#status ⇒ Object
Returns the value of attribute status.
-
#team_stats ⇒ Object
Returns the value of attribute team_stats.
-
#time_zone ⇒ Object
Returns the value of attribute time_zone.
-
#title ⇒ Object
Returns the value of attribute title.
-
#venue ⇒ Object
Returns the value of attribute venue.
Instance Method Summary collapse
-
#api ⇒ Object
The base for the requests needed for a subclass.
- #assign_away(team) ⇒ Object
- #assign_home(team) ⇒ Object
- #away ⇒ Object
- #box ⇒ Object
- #cancelled? ⇒ Boolean
- #changed?(key) ⇒ Boolean
- #check_newness(key, new_object) ⇒ Object
- #clock_display ⇒ Object
- #clock_seconds ⇒ Object
- #closed? ⇒ Boolean
- #completed? ⇒ Boolean
- #finished? ⇒ Boolean
- #future? ⇒ Boolean
-
#get_box ⇒ Object
data retrieval.
- #get_pbp ⇒ Object
- #get_summary ⇒ Object
- #halftime? ⇒ Boolean
- #home ⇒ Object
- #ingest_box(data) ⇒ Object
- #ingest_pbp(data) ⇒ Object
- #ingest_summary(data) ⇒ Object
-
#initialize(data, **opts) ⇒ Game
constructor
def self.new(data, **opts) existing = @all_hash[data] if existing existing.update(data, **opts) existing else @all_hash[data] = super end end def self.all @all_hash.values end.
- #leading_team ⇒ Object
- #leading_team_id ⇒ Object
- #not_updated?(key, object) ⇒ Boolean
- #parse_score(data) ⇒ Object
-
#path_base ⇒ Object
url paths.
- #path_box ⇒ Object
- #path_pbp ⇒ Object
- #path_summary ⇒ Object
- #pbp ⇒ Object
-
#period_class ⇒ Object
The class used for game periods.
- #period_display ⇒ Object
- #period_display_long ⇒ Object
-
#period_name ⇒ Object
The string name used for game periods.
- #periods ⇒ Object
- #plays ⇒ Object (also: #events)
- #plays_by_type(play_type, *types) ⇒ Object
- #points(team_id) ⇒ Object
- #postponed? ⇒ Boolean
- #queue_pbp ⇒ Object
- #queue_summary ⇒ Object
-
#realtime_state ⇒ Object
status helpers.
-
#remember(key, object) ⇒ Object
tracking updates.
- #scoring ⇒ Object
- #set_pbp(data) ⇒ Object
- #started? ⇒ Boolean
- #stats(team_id) ⇒ Object
- #summary ⇒ Object
- #team(team_id) ⇒ Object
- #tied? ⇒ Boolean
- #timeouts ⇒ Object
- #unnecessary? ⇒ Boolean
- #update(data, source: nil, **opts) ⇒ Object
- #update_player_stats(player, stats) ⇒ Object
- #update_score(score) ⇒ Object
- #update_stats(team, stats) ⇒ 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
def self.new(data, **opts)
existing = @all_hash[data['id']]
if existing
existing.update(data, **opts)
existing
else
@all_hash[data['id']] = super
end
end def self.all
@all_hash.values
end
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/sportradar/api/basketball/game.rb', line 22 def initialize(data, **opts) @response = data @api = opts[:api] # @season = opts[:season] @updates = {} @changes = {} @score = {} @team_stats = {} @player_stats = {} @scoring_raw = Scoring.new(data, game: self) @teams_hash = {} @periods_hash = {} @home_points = nil @away_points = nil @home_id = nil @away_id = nil @id = data['id'] update(data, **opts) end |
Instance Attribute Details
#attendance ⇒ Object
Returns the value of attribute attendance.
5 6 7 |
# File 'lib/sportradar/api/basketball/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/basketball/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/basketball/game.rb', line 5 def broadcast @broadcast end |
#changes ⇒ Object
Returns the value of attribute changes.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def changes @changes end |
#clock ⇒ Object
Returns the value of attribute clock.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def clock @clock end |
#coverage ⇒ Object
Returns the value of attribute coverage.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def coverage @coverage end |
#duration ⇒ Object
Returns the value of attribute duration.
5 6 7 |
# File 'lib/sportradar/api/basketball/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/basketball/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/basketball/game.rb', line 5 def id @id end |
#media_timeouts ⇒ Object
Returns the value of attribute media_timeouts.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def media_timeouts @media_timeouts end |
#period ⇒ Object
Returns the value of attribute period.
7 8 9 |
# File 'lib/sportradar/api/basketball/game.rb', line 7 def period @period end |
#player_stats ⇒ Object
Returns the value of attribute player_stats.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def player_stats @player_stats end |
#response ⇒ Object
Returns the value of attribute response.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def response @response end |
#scheduled ⇒ Object
Returns the value of attribute scheduled.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def scheduled @scheduled end |
#score ⇒ Object
Returns the value of attribute score.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def score @score end |
#status ⇒ Object
Returns the value of attribute status.
5 6 7 |
# File 'lib/sportradar/api/basketball/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/basketball/game.rb', line 5 def team_stats @team_stats end |
#time_zone ⇒ Object
Returns the value of attribute time_zone.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def time_zone @time_zone end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def title @title end |
#venue ⇒ Object
Returns the value of attribute venue.
5 6 7 |
# File 'lib/sportradar/api/basketball/game.rb', line 5 def venue @venue end |
Instance Method Details
#api ⇒ Object
The base for the requests needed for a subclass
|
# File 'lib/sportradar/api/basketball/game.rb', line 346
|
#assign_away(team) ⇒ Object
136 137 138 139 |
# File 'lib/sportradar/api/basketball/game.rb', line 136 def assign_away(team) @away_id = team.id @teams_hash[team.id] = team end |
#assign_home(team) ⇒ Object
132 133 134 135 |
# File 'lib/sportradar/api/basketball/game.rb', line 132 def assign_home(team) @home_id = team.id @teams_hash[team.id] = team end |
#away ⇒ Object
118 119 120 |
# File 'lib/sportradar/api/basketball/game.rb', line 118 def away @teams_hash[@away_id] || @away end |
#box ⇒ Object
141 142 143 |
# File 'lib/sportradar/api/basketball/game.rb', line 141 def box @box ||= get_box end |
#cancelled? ⇒ Boolean
248 249 250 |
# File 'lib/sportradar/api/basketball/game.rb', line 248 def cancelled? ['unnecessary', 'postponed'].include? status end |
#changed?(key) ⇒ Boolean
178 179 180 |
# File 'lib/sportradar/api/basketball/game.rb', line 178 def changed?(key) @changes[key] end |
#check_newness(key, new_object) ⇒ Object
181 182 183 184 |
# File 'lib/sportradar/api/basketball/game.rb', line 181 def check_newness(key, new_object) @changes[key] = !not_updated?(key, new_object) remember(key, new_object) end |
#clock_display ⇒ Object
218 219 220 221 222 |
# File 'lib/sportradar/api/basketball/game.rb', line 218 def clock_display if clock && period "#{clock} #{period_display}" end end |
#clock_seconds ⇒ Object
75 76 77 78 79 |
# File 'lib/sportradar/api/basketball/game.rb', line 75 def clock_seconds return unless @clock m,s = @clock.split(':') m.to_i * 60 + s.to_i end |
#closed? ⇒ Boolean
263 264 265 |
# File 'lib/sportradar/api/basketball/game.rb', line 263 def closed? 'closed' == status end |
#completed? ⇒ Boolean
260 261 262 |
# File 'lib/sportradar/api/basketball/game.rb', line 260 def completed? 'complete' == status end |
#finished? ⇒ Boolean
257 258 259 |
# File 'lib/sportradar/api/basketball/game.rb', line 257 def finished? ['complete', 'closed'].include? status end |
#future? ⇒ Boolean
251 252 253 |
# File 'lib/sportradar/api/basketball/game.rb', line 251 def future? ['scheduled', 'delayed', 'created', 'time-tbd', 'if-necessary'].include? status end |
#get_box ⇒ Object
data retrieval
269 270 271 272 |
# File 'lib/sportradar/api/basketball/game.rb', line 269 def get_box data = api.get_data(path_box).to_h ingest_box(data) end |
#get_pbp ⇒ Object
286 287 288 289 |
# File 'lib/sportradar/api/basketball/game.rb', line 286 def get_pbp data = api.get_data(path_pbp).to_h ingest_pbp(data) end |
#get_summary ⇒ Object
314 315 316 317 |
# File 'lib/sportradar/api/basketball/game.rb', line 314 def get_summary data = api.get_data(path_summary).to_h ingest_summary(data) end |
#halftime? ⇒ Boolean
215 216 217 |
# File 'lib/sportradar/api/basketball/game.rb', line 215 def halftime? status == 'halftime' || clock == '00:00' && quarter == 2 end |
#home ⇒ Object
114 115 116 |
# File 'lib/sportradar/api/basketball/game.rb', line 114 def home @teams_hash[@home_id] || @home end |
#ingest_box(data) ⇒ Object
274 275 276 277 278 279 |
# File 'lib/sportradar/api/basketball/game.rb', line 274 def ingest_box(data) update(data, source: :box) @period = data.delete(period_name).to_i check_newness(:box, @clock) data end |
#ingest_pbp(data) ⇒ Object
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/sportradar/api/basketball/game.rb', line 291 def ingest_pbp(data) period_name = 'periods' update(data, source: :pbp) period_data = if data[period_name] && !data[period_name].empty? @period = data[period_name].last['sequence'].to_i pers = data[period_name] pers.is_a?(Array) && (pers.size == 1) ? pers[0] : pers else @period = nil [] end if data['overtime'] extra_periods = data['overtime'].is_a?(Hash) ? [data['overtime']] : data['overtime'] period_data.concat(extra_periods) end set_pbp(period_data) @pbp = @periods_hash.values check_newness(:pbp, plays.last&.updated) check_newness(:clock, @clock) check_newness(:score, @score) data end |
#ingest_summary(data) ⇒ Object
324 325 326 327 328 329 330 |
# File 'lib/sportradar/api/basketball/game.rb', line 324 def ingest_summary(data) update(data, source: :summary) @period = data.delete(period_name).to_i check_newness(:box, @clock) check_newness(:score, @score) data end |
#leading_team ⇒ Object
126 127 128 |
# File 'lib/sportradar/api/basketball/game.rb', line 126 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
122 123 124 125 |
# File 'lib/sportradar/api/basketball/game.rb', line 122 def leading_team_id return nil if score.values.uniq.size == 1 score.max_by(&:last).first end |
#not_updated?(key, object) ⇒ Boolean
175 176 177 |
# File 'lib/sportradar/api/basketball/game.rb', line 175 def not_updated?(key, object) @updates[key] == object end |
#parse_score(data) ⇒ Object
71 72 73 74 |
# File 'lib/sportradar/api/basketball/game.rb', line 71 def parse_score(data) update_score(data.dig('home', 'id') => data.dig('home', 'points').to_i) update_score(data.dig('away', 'id') => data.dig('away', 'points').to_i) end |
#path_base ⇒ Object
url paths
187 188 189 |
# File 'lib/sportradar/api/basketball/game.rb', line 187 def path_base "games/#{ id }" end |
#path_box ⇒ Object
190 191 192 |
# File 'lib/sportradar/api/basketball/game.rb', line 190 def path_box "#{ path_base }/boxscore" end |
#path_pbp ⇒ Object
193 194 195 |
# File 'lib/sportradar/api/basketball/game.rb', line 193 def path_pbp "#{ path_base }/pbp" end |
#path_summary ⇒ Object
196 197 198 |
# File 'lib/sportradar/api/basketball/game.rb', line 196 def path_summary "#{ path_base }/summary" end |
#pbp ⇒ Object
144 145 146 147 148 149 |
# File 'lib/sportradar/api/basketball/game.rb', line 144 def pbp if !future? && periods.empty? get_pbp end @pbp ||= periods end |
#period_class ⇒ Object
The class used for game periods
|
# File 'lib/sportradar/api/basketball/game.rb', line 338
|
#period_display ⇒ Object
232 233 234 235 236 237 238 239 240 |
# File 'lib/sportradar/api/basketball/game.rb', line 232 def period_display if period > 5 "#{period - 4}OT" elsif period == 5 'OT' else "#{period}Q" end end |
#period_display_long ⇒ Object
223 224 225 226 227 228 229 230 231 |
# File 'lib/sportradar/api/basketball/game.rb', line 223 def period_display_long if period > 5 "Overtime #{period - 4}" elsif period == 5 'Overtime' else "#{Sportradar.ordinalize_period(period)} Quarter" end end |
#period_name ⇒ Object
The string name used for game periods
|
# File 'lib/sportradar/api/basketball/game.rb', line 342
|
#periods ⇒ Object
166 167 168 |
# File 'lib/sportradar/api/basketball/game.rb', line 166 def periods @periods_hash.values end |
#plays ⇒ Object Also known as: events
150 151 152 |
# File 'lib/sportradar/api/basketball/game.rb', line 150 def plays periods.flat_map(&:plays) end |
#plays_by_type(play_type, *types) ⇒ Object
153 154 155 156 157 158 159 160 |
# File 'lib/sportradar/api/basketball/game.rb', line 153 def plays_by_type(play_type, *types) if types.empty? plays.grep(Play.subclass(play_type.delete('_'))) else play_classes = [play_type, *types].map { |type| Play.subclass(type.delete('_')) } plays.select { |play| play_classes.any? { |klass| play.kind_of?(klass) } } end end |
#points(team_id) ⇒ Object
51 52 53 |
# File 'lib/sportradar/api/basketball/game.rb', line 51 def points(team_id) team_id.is_a?(Symbol) ? @score[@team_ids[team_id]].to_i : @score[team_id].to_i end |
#postponed? ⇒ Boolean
242 243 244 |
# File 'lib/sportradar/api/basketball/game.rb', line 242 def postponed? 'postponed' == status end |
#queue_pbp ⇒ Object
281 282 283 284 |
# File 'lib/sportradar/api/basketball/game.rb', line 281 def queue_pbp url, headers, , timeout = api.get_request_info(path_pbp) {url: url, headers: headers, params: , timeout: timeout, callback: method(:ingest_pbp)} end |
#queue_summary ⇒ Object
319 320 321 322 |
# File 'lib/sportradar/api/basketball/game.rb', line 319 def queue_summary url, headers, , timeout = api.get_request_info(path_summary) {url: url, headers: headers, params: , timeout: timeout, callback: method(:ingest_summary)} end |
#realtime_state ⇒ Object
status helpers
201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/sportradar/api/basketball/game.rb', line 201 def realtime_state if future? 'Scheduled' elsif finished? 'Final' elsif postponed? 'Postponed' elsif halftime? 'Halftime' else clock_display end end |
#remember(key, object) ⇒ Object
tracking updates
172 173 174 |
# File 'lib/sportradar/api/basketball/game.rb', line 172 def remember(key, object) @updates[key] = object&.dup end |
#scoring ⇒ Object
58 59 60 |
# File 'lib/sportradar/api/basketball/game.rb', line 58 def scoring @scoring_raw.scores end |
#set_pbp(data) ⇒ Object
332 333 334 335 336 |
# File 'lib/sportradar/api/basketball/game.rb', line 332 def set_pbp(data) create_data(@periods_hash, data, klass: period_class, api: api, game: self) @plays = nil # to clear empty array empty @periods_hash end |
#started? ⇒ Boolean
254 255 256 |
# File 'lib/sportradar/api/basketball/game.rb', line 254 def started? ['inprogress', 'halftime', 'delayed'].include? status end |
#stats(team_id) ⇒ Object
54 55 56 |
# File 'lib/sportradar/api/basketball/game.rb', line 54 def stats(team_id) team_id.is_a?(Symbol) ? @team_stats[@team_ids[team_id]].to_i : @team_stats[team_id].to_i end |
#summary ⇒ Object
161 162 163 |
# File 'lib/sportradar/api/basketball/game.rb', line 161 def summary @summary ||= get_summary end |
#team(team_id) ⇒ Object
129 130 131 |
# File 'lib/sportradar/api/basketball/game.rb', line 129 def team(team_id) @teams_hash[team_id] end |
#tied? ⇒ Boolean
48 49 50 |
# File 'lib/sportradar/api/basketball/game.rb', line 48 def tied? @score[away_id].to_i == @score[home_id].to_i end |
#timeouts ⇒ Object
44 45 46 |
# File 'lib/sportradar/api/basketball/game.rb', line 44 def timeouts {} end |
#unnecessary? ⇒ Boolean
245 246 247 |
# File 'lib/sportradar/api/basketball/game.rb', line 245 def unnecessary? 'unnecessary' == status end |
#update(data, source: nil, **opts) ⇒ Object
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 110 111 112 |
# File 'lib/sportradar/api/basketball/game.rb', line 81 def update(data, source: nil, **opts) # via pbp @status = data['status'] if data['status'] @coverage = data['coverage'] if data['coverage'] @home_id = data['home_team'] || data.dig('home', 'id') if data['home_team'] || data.dig('home', 'id') @away_id = data['away_team'] || data.dig('away', 'id') if data['away_team'] || data.dig('away', 'id') @home_points = data['home_points'].to_i if data['home_points'] @away_points = data['away_points'].to_i if data['away_points'] @scheduled = Time.parse(data["scheduled"]) if data["scheduled"] @venue = Venue.new(data['venue']) if data['venue'] @broadcast = Broadcast.new(data['broadcast']) if !data['broadcast'].to_h.empty? @home = team_class.new(data['home'], api: api, game: self) if data['home'] @away = team_class.new(data['away'], api: api, game: self) if data['away'] @title = data['title'] || @title || (home && away && "#{home.full_name} vs #{away.full_name}") @time_zone = data.dig("time_zones", "venue") if data.dig("time_zones", "venue") @duration = data['duration'] if data['duration'] @clock = data['clock'] if data['clock'] @attendance = data['attendance'] if data['attendance'] @lead_changes = data['lead_changes'] if data['lead_changes'] @times_tied = data['times_tied'] if data['times_tied'] @team_ids = { home: @home_id, away: @away_id} update_score(@home_id => @home_points.to_i) if @home_points update_score(@away_id => @away_points.to_i) if @away_points parse_score(data['scoring']) if data['scoring'] @scoring_raw.update(data, source: source) create_data(@teams_hash, data['team'], klass: team_class, api: api, game: self) if data['team'] end |
#update_player_stats(player, stats) ⇒ Object
67 68 69 |
# File 'lib/sportradar/api/basketball/game.rb', line 67 def update_player_stats(player, stats) @player_stats.merge!(player.id => stats.merge!(player: player)) end |
#update_score(score) ⇒ Object
61 62 63 |
# File 'lib/sportradar/api/basketball/game.rb', line 61 def update_score(score) @score.merge!(score) end |
#update_stats(team, stats) ⇒ Object
64 65 66 |
# File 'lib/sportradar/api/basketball/game.rb', line 64 def update_stats(team, stats) @team_stats.merge!(team.id => stats.merge!(team: team)) end |