class Sportradar::Nba::Parsers::BoxscoreParser

Attributes

json[R]

Public Class Methods

new(json: {}) click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 5
def initialize(json: {})
  @json = json['game'] || json
end

Public Instance Methods

attendance() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 13
def attendance
  json['attendance']
end
away_team_id() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 122
def away_team_id
  away_team_json['id']
end
away_team_json() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 118
def away_team_json
  json['away'] || {}
end
away_team_outcome() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 200
def away_team_outcome
  if over?
    if home_team_score < away_team_score
      'win'
    elsif home_team_score > away_team_score
      'loss'
    else
      'tie'
    end
  else
    'undecided'
  end
end
away_team_score() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 196
def away_team_score
  away_team_scoring[:points_scored_total] || 0
end
away_team_scoring() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 130
def away_team_scoring
  {
    points_scored_total: away_team_json['points'],
  }.merge(away_team_scoring_quarters).
    compact
end
away_team_scoring_data() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 126
def away_team_scoring_data
  away_team_json['scoring'] || []
end
away_team_scoring_quarters() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 137
def away_team_scoring_quarters
  team_scoring_quarters(data: away_team_scoring_data)
end
clock() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 49
def clock
  json['clock']
end
clock_attributes() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 53
def clock_attributes
  {
    clock: clock,
    clock_secs: clock_secs,
    duration: duration_secs,
    period: period,
    ended_at: ended_at,
    status: status,
    lead_changes: lead_changes,
    times_tied: times_tied,
  }.compact
end
clock_secs() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 66
def clock_secs
  begin
    if clock && clock.include?(':')
      mins, secs = clock.split(':').map(&:to_i)
      Time.parse("00:#{mins}:#{secs}").
         seconds_since_midnight.to_i
    end
  rescue => e
    puts e
    return 0
  end
end
completed()
Alias for: ended_at
duration() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 79
def duration
  json['duration']
end
duration_secs() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 83
def duration_secs
  begin
    if duration && duration.include?(':')
      hours, mins = duration.split(':').map(&:to_i)
      Time.parse("#{hours}:#{mins}:00").
         seconds_since_midnight.to_i
    end
  rescue => e
    return 0
  end
end
ended_at() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 26
def ended_at
  if over?
    scheduled_at + duration_secs.seconds
  end
end
Also aliased as: completed
game_attributes() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 214
def game_attributes
  clock_attributes.merge(game_scoring_attributes).
    compact
end
game_id() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 9
def game_id
  json['id']
end
game_scoring_attributes() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 168
def game_scoring_attributes
  {
    attendance: attendance,
    home_team_outcome: home_team_outcome,
    home_team_score: home_team_score,
    away_team_outcome: away_team_outcome,
    away_team_score: away_team_score,
  }
end
home_team_id() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 99
def home_team_id
  home_team_json['id']
end
home_team_json() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 95
def home_team_json
  json['home'] || {}
end
home_team_outcome() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 182
def home_team_outcome
  if over?
    if home_team_score > away_team_score
      'win'
    elsif home_team_score < away_team_score
      'loss'
    else
      'tie'
    end
  else
    'undecided'
  end
end
home_team_score() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 178
def home_team_score
  home_team_scoring[:points_scored_total] || 0
end
home_team_scoring() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 107
def home_team_scoring
  {
    points_scored_total: home_team_json['points'],
  }.merge(home_team_scoring_quarters).
    compact
end
home_team_scoring_data() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 103
def home_team_scoring_data
  home_team_json['scoring'] || []
end
home_team_scoring_quarters() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 114
def home_team_scoring_quarters
  team_scoring_quarters(data: home_team_scoring_data)
end
lead_changes() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 41
def lead_changes
  json['lead_changes'] || 0
end
over?() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 33
def over?
  status == 'closed'
end
period() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 17
def period
  json['quarter'].to_i
end
scheduled_at() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 21
def scheduled_at
  json['scheduled'] && json['scheduled'].to_datetime
end
Also aliased as: started_at
started_at()
Alias for: scheduled_at
status() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 37
def status
  json['status']
end
team_scoring_quarters(data:) click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 141
def team_scoring_quarters(data:)
  {}.tap do |scoring_quarters|
    overtime_points = 0
    scoring_quarters[:points_overtime] = overtime_points

    data.map do |scoring_data|
      if quarter = scoring_data['sequence'].to_i
        if quarter > 0 && quarter <= 4
          key = "points_quarter_#{quarter}".to_sym
          scoring_quarters[key] =
            scoring_data['points'].to_i
        elsif quarter > 4
          quarter_type = (scoring_data['type'] || 'quarter').downcase
          if quarter_type == 'overtime'
            key = "points_#{quarter_type}_#{scoring_data['number'].to_i}".to_sym
            scoring_quarters[key] =
              scoring_data['points'].to_i
            overtime_points += scoring_data['points'].to_i
            scoring_quarters[:points_overtime] = overtime_points
          end
        end
      end
    end
    scoring_quarters[:points_overtime] = overtime_points
  end
end
times_tied() click to toggle source
# File lib/sportradar/nba/parsers/boxscore_parser.rb, line 45
def times_tied
  json['times_tied'] || 0
end