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.
-
#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 |
#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 345
|
#assign_away(team) ⇒ Object
135 136 137 138 |
# File 'lib/sportradar/api/basketball/game.rb', line 135 def assign_away(team) @away_id = team.id @teams_hash[team.id] = team end |
#assign_home(team) ⇒ Object
131 132 133 134 |
# File 'lib/sportradar/api/basketball/game.rb', line 131 def assign_home(team) @home_id = team.id @teams_hash[team.id] = team end |
#away ⇒ Object
117 118 119 |
# File 'lib/sportradar/api/basketball/game.rb', line 117 def away @teams_hash[@away_id] || @away end |
#box ⇒ Object
140 141 142 |
# File 'lib/sportradar/api/basketball/game.rb', line 140 def box @box ||= get_box end |
#cancelled? ⇒ Boolean
247 248 249 |
# File 'lib/sportradar/api/basketball/game.rb', line 247 def cancelled? ['unnecessary', 'postponed'].include? status end |
#changed?(key) ⇒ Boolean
177 178 179 |
# File 'lib/sportradar/api/basketball/game.rb', line 177 def changed?(key) @changes[key] end |
#check_newness(key, new_object) ⇒ Object
180 181 182 183 |
# File 'lib/sportradar/api/basketball/game.rb', line 180 def check_newness(key, new_object) @changes[key] = !not_updated?(key, new_object) remember(key, new_object) end |
#clock_display ⇒ Object
217 218 219 220 221 |
# File 'lib/sportradar/api/basketball/game.rb', line 217 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
262 263 264 |
# File 'lib/sportradar/api/basketball/game.rb', line 262 def closed? 'closed' == status end |
#completed? ⇒ Boolean
259 260 261 |
# File 'lib/sportradar/api/basketball/game.rb', line 259 def completed? 'complete' == status end |
#finished? ⇒ Boolean
256 257 258 |
# File 'lib/sportradar/api/basketball/game.rb', line 256 def finished? ['complete', 'closed'].include? status end |
#future? ⇒ Boolean
250 251 252 |
# File 'lib/sportradar/api/basketball/game.rb', line 250 def future? ['scheduled', 'delayed', 'created', 'time-tbd', 'if-necessary'].include? status end |
#get_box ⇒ Object
data retrieval
268 269 270 271 |
# File 'lib/sportradar/api/basketball/game.rb', line 268 def get_box data = api.get_data(path_box).to_h ingest_box(data) end |
#get_pbp ⇒ Object
285 286 287 288 |
# File 'lib/sportradar/api/basketball/game.rb', line 285 def get_pbp data = api.get_data(path_pbp).to_h ingest_pbp(data) end |
#get_summary ⇒ Object
313 314 315 316 |
# File 'lib/sportradar/api/basketball/game.rb', line 313 def get_summary data = api.get_data(path_summary).to_h ingest_summary(data) end |
#halftime? ⇒ Boolean
214 215 216 |
# File 'lib/sportradar/api/basketball/game.rb', line 214 def halftime? status == 'halftime' || clock == '00:00' && quarter == 2 end |
#home ⇒ Object
113 114 115 |
# File 'lib/sportradar/api/basketball/game.rb', line 113 def home @teams_hash[@home_id] || @home end |
#ingest_box(data) ⇒ Object
273 274 275 276 277 278 |
# File 'lib/sportradar/api/basketball/game.rb', line 273 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
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/sportradar/api/basketball/game.rb', line 290 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
323 324 325 326 327 328 329 |
# File 'lib/sportradar/api/basketball/game.rb', line 323 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
125 126 127 |
# File 'lib/sportradar/api/basketball/game.rb', line 125 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
121 122 123 124 |
# File 'lib/sportradar/api/basketball/game.rb', line 121 def leading_team_id return nil if score.values.uniq.size == 1 score.max_by(&:last).first end |
#not_updated?(key, object) ⇒ Boolean
174 175 176 |
# File 'lib/sportradar/api/basketball/game.rb', line 174 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
186 187 188 |
# File 'lib/sportradar/api/basketball/game.rb', line 186 def path_base "games/#{ id }" end |
#path_box ⇒ Object
189 190 191 |
# File 'lib/sportradar/api/basketball/game.rb', line 189 def path_box "#{ path_base }/boxscore" end |
#path_pbp ⇒ Object
192 193 194 |
# File 'lib/sportradar/api/basketball/game.rb', line 192 def path_pbp "#{ path_base }/pbp" end |
#path_summary ⇒ Object
195 196 197 |
# File 'lib/sportradar/api/basketball/game.rb', line 195 def path_summary "#{ path_base }/summary" end |
#pbp ⇒ Object
143 144 145 146 147 148 |
# File 'lib/sportradar/api/basketball/game.rb', line 143 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 337
|
#period_display ⇒ Object
231 232 233 234 235 236 237 238 239 |
# File 'lib/sportradar/api/basketball/game.rb', line 231 def period_display if period > 5 "#{period - 4}OT" elsif period == 5 'OT' else "#{period}Q" end end |
#period_display_long ⇒ Object
222 223 224 225 226 227 228 229 230 |
# File 'lib/sportradar/api/basketball/game.rb', line 222 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 341
|
#periods ⇒ Object
165 166 167 |
# File 'lib/sportradar/api/basketball/game.rb', line 165 def periods @periods_hash.values end |
#plays ⇒ Object Also known as: events
149 150 151 |
# File 'lib/sportradar/api/basketball/game.rb', line 149 def plays periods.flat_map(&:plays) end |
#plays_by_type(play_type, *types) ⇒ Object
152 153 154 155 156 157 158 159 |
# File 'lib/sportradar/api/basketball/game.rb', line 152 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
241 242 243 |
# File 'lib/sportradar/api/basketball/game.rb', line 241 def postponed? 'postponed' == status end |
#queue_pbp ⇒ Object
280 281 282 283 |
# File 'lib/sportradar/api/basketball/game.rb', line 280 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
318 319 320 321 |
# File 'lib/sportradar/api/basketball/game.rb', line 318 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
200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/sportradar/api/basketball/game.rb', line 200 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
171 172 173 |
# File 'lib/sportradar/api/basketball/game.rb', line 171 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
331 332 333 334 335 |
# File 'lib/sportradar/api/basketball/game.rb', line 331 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
253 254 255 |
# File 'lib/sportradar/api/basketball/game.rb', line 253 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
160 161 162 |
# File 'lib/sportradar/api/basketball/game.rb', line 160 def summary @summary ||= get_summary end |
#team(team_id) ⇒ Object
128 129 130 |
# File 'lib/sportradar/api/basketball/game.rb', line 128 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
244 245 246 |
# File 'lib/sportradar/api/basketball/game.rb', line 244 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 |
# 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}") @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 |