class Sportradar::Nhl::Parsers::BoxscoreParser

Attributes

json[R]

Public Class Methods

new(json: {}) click to toggle source
# File lib/sportradar/nhl/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/nhl/parsers/boxscore_parser.rb, line 13
def attendance
  json['attendance']
end
away_team_id() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 115
def away_team_id
  away_team_json['id']
end
away_team_json() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 111
def away_team_json
  json['away'] || {}
end
away_team_outcome() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 199
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/nhl/parsers/boxscore_parser.rb, line 195
def away_team_score
  away_team_scoring[:goals] || 0
end
away_team_scoring() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 123
def away_team_scoring
  {
    goals: away_team_json['points'],
  }.merge(away_team_scoring_periods).
    compact
end
away_team_scoring_data() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 119
def away_team_scoring_data
  away_team_json['scoring'] || {}
end
away_team_scoring_periods() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 130
def away_team_scoring_periods
  team_scoring_periods(data: away_team_scoring_data)
end
clock() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 42
def clock
  json['clock']
end
clock_attributes() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 46
def clock_attributes
  {
    clock: clock,
    clock_secs: clock_secs,
    duration: duration_secs,
    period: period,
    ended_at: ended_at,
    status: status,
  }.compact
end
clock_secs() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 57
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/nhl/parsers/boxscore_parser.rb, line 70
def duration
  json['total_game_duration']
end
duration_secs() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 74
def duration_secs
  begin
    if duration && duration.include?(':')
      mins, secs = duration.split(':').map(&:to_i)
      hours = mins / 60
      mins = mins % 60
      Time.parse("#{hours}:#{mins}:#{secs}").
         seconds_since_midnight.to_i
    end
  rescue => e
    return 0
  end
end
ended_at() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 29
def ended_at
  json['end_time'] && json['end_time'].to_datetime
end
Also aliased as: completed
game_attributes() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 213
def game_attributes
  clock_attributes.merge(game_scoring_attributes).
    compact
end
game_id() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 9
def game_id
  json['id']
end
game_scoring_attributes() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 167
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/nhl/parsers/boxscore_parser.rb, line 92
def home_team_id
  home_team_json['id']
end
home_team_json() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 88
def home_team_json
  json['home'] || {}
end
home_team_outcome() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 181
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/nhl/parsers/boxscore_parser.rb, line 177
def home_team_score
  home_team_scoring[:goals] || 0
end
home_team_scoring() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 100
def home_team_scoring
  {
    goals: home_team_json['points'],
  }.merge(home_team_scoring_periods).
    compact
end
home_team_scoring_data() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 96
def home_team_scoring_data
  home_team_json['scoring'] || {}
end
home_team_scoring_periods() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 107
def home_team_scoring_periods
  team_scoring_periods(data: home_team_scoring_data)
end
over?() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 34
def over?
  status == 'closed'
end
period() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 17
def period
  json['period'].to_i
end
scheduled_at() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 21
def scheduled_at
  json['scheduled'] && json['scheduled'].to_datetime
end
started_at() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 25
def started_at
  json['start_time'] && json['start_time'].to_datetime
end
status() click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 38
def status
  json['status']
end
team_scoring_periods(data:) click to toggle source
# File lib/sportradar/nhl/parsers/boxscore_parser.rb, line 134
def team_scoring_periods(data:)
  {}.tap do |scoring_periods|
    overtime_points = 0
    scoring_periods[:goals_overtime] = overtime_points
    scoring_periods[:goals_shootout] = 0

    data.map do |scoring_data|
      if period = scoring_data['sequence'].to_i
        period_type = (scoring_data['type'] || 'period').downcase

        if (period > 0 && period <= 3) && (period_type == 'period')
          key = "goals_period_#{period}".to_sym
          scoring_periods[key] =
            scoring_data['points'].to_i
        elsif period > 3
          if period_type == 'overtime'
            key = "goals_#{period_type}_#{scoring_data['number'].to_i}".to_sym
            scoring_periods[key] =
              scoring_data['points'].to_i
            overtime_points += scoring_data['points'].to_i
            scoring_periods[:goals_overtime] = overtime_points
          elsif period_type == 'shootout'
            key = "goals_#{period_type}".to_sym
            scoring_periods[key] =
              scoring_data['points'].to_i
          end
        end
      end
    end
    scoring_periods[:goals_overtime] = overtime_points
  end
end