class NHLStats::Game

Attributes

away_score[R]
away_team_id[R]
date[R]
home_score[R]
home_team_id[R]
id[R]

Public Class Methods

find(id) click to toggle source
# File lib/nhl_stats/game.rb, line 20
def self.find(id)
  response = Faraday.get("#{API_ROOT}/game/#{id}/linescore")
  attributes = JSON.parse(response.body)
  new(attributes.merge(:id => id))
end
list(params = {}) click to toggle source
# File lib/nhl_stats/game.rb, line 26
def self.list(params = {})
  response = Faraday.get("#{API_ROOT}/schedule", params)
  JSON.parse(response.body).
    fetch("dates", []).
    map { |d| d.fetch("games", []).map { |g| NHLStats::Game.new(g) } }.
    flatten
end
new(game_data) click to toggle source
# File lib/nhl_stats/game.rb, line 5
def initialize(game_data)
  @id = game_data[:id] || game_data.dig("gamePk")
  home_team_data(game_data)
  away_team_data(game_data)
  @date = Time.parse(game_data.dig("periods", 0, "startTime") || game_data.dig("gameDate"))
end

Public Instance Methods

away_team() click to toggle source
# File lib/nhl_stats/game.rb, line 16
def away_team
  NHLStats::Team.find(away_team_id)
end
home_team() click to toggle source
# File lib/nhl_stats/game.rb, line 12
def home_team
  NHLStats::Team.find(home_team_id)
end

Private Instance Methods

away_team_data(game_data) click to toggle source
# File lib/nhl_stats/game.rb, line 42
def away_team_data(game_data)
  away = game_data.dig("teams", "away")
  @away_team_id = away.dig("team", "id")
  @away_score = away.dig("goals") || away.dig("score")
end
home_team_data(game_data) click to toggle source
# File lib/nhl_stats/game.rb, line 36
def home_team_data(game_data)
  home = game_data.dig("teams", "home")
  @home_team_id = home.dig("team", "id")
  @home_score = home.dig("goals") || home.dig("score")
end