Class: Sportradar::Nhl::Parsers::BoxscoreParser

Inherits:
Object
  • Object
show all
Defined in:
lib/sportradar/nhl/parsers/boxscore_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(json: {}) ⇒ BoxscoreParser

Returns a new instance of BoxscoreParser.



5
6
7
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 5

def initialize(json: {})
  @json = json['game'] || json
end

Instance Method Details

#attendanceObject



13
14
15
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 13

def attendance
  json['attendance']
end

#away_team_idObject



115
116
117
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 115

def away_team_id
  away_team_json['id']
end

#away_team_jsonObject



111
112
113
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 111

def away_team_json
  json['away'] || {}
end

#away_team_outcomeObject



199
200
201
202
203
204
205
206
207
208
209
210
211
# 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_scoreObject



195
196
197
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 195

def away_team_score
  away_team_scoring[:goals] || 0
end

#away_team_scoringObject



123
124
125
126
127
128
# 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_dataObject



119
120
121
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 119

def away_team_scoring_data
  away_team_json['scoring'] || {}
end

#away_team_scoring_periodsObject



130
131
132
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 130

def away_team_scoring_periods
  team_scoring_periods(data: away_team_scoring_data)
end

#clockObject



42
43
44
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 42

def clock
  json['clock']
end

#clock_attributesObject



46
47
48
49
50
51
52
53
54
55
# 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_secsObject



57
58
59
60
61
62
63
64
65
66
67
68
# 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

#durationObject



70
71
72
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 70

def duration
  json['total_game_duration']
end

#duration_secsObject



74
75
76
77
78
79
80
81
82
83
84
85
86
# 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_atObject Also known as: completed



29
30
31
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 29

def ended_at
  json['end_time'] && json['end_time'].to_datetime
end

#game_attributesObject



213
214
215
216
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 213

def game_attributes
  clock_attributes.merge(game_scoring_attributes).
    compact
end

#game_idObject



9
10
11
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 9

def game_id
  json['id']
end

#game_scoring_attributesObject



167
168
169
170
171
172
173
174
175
# 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_idObject



92
93
94
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 92

def home_team_id
  home_team_json['id']
end

#home_team_jsonObject



88
89
90
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 88

def home_team_json
  json['home'] || {}
end

#home_team_outcomeObject



181
182
183
184
185
186
187
188
189
190
191
192
193
# 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_scoreObject



177
178
179
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 177

def home_team_score
  home_team_scoring[:goals] || 0
end

#home_team_scoringObject



100
101
102
103
104
105
# 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_dataObject



96
97
98
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 96

def home_team_scoring_data
  home_team_json['scoring'] || {}
end

#home_team_scoring_periodsObject



107
108
109
# 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?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 34

def over?
  status == 'closed'
end

#periodObject



17
18
19
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 17

def period
  json['period'].to_i
end

#scheduled_atObject



21
22
23
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 21

def scheduled_at
  json['scheduled'] && json['scheduled'].to_datetime
end

#started_atObject



25
26
27
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 25

def started_at
  json['start_time'] && json['start_time'].to_datetime
end

#statusObject



38
39
40
# File 'lib/sportradar/nhl/parsers/boxscore_parser.rb', line 38

def status
  json['status']
end

#team_scoring_periods(data:) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# 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