Class: NHLStats::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/nhl_stats/game.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(game_data) ⇒ Game

Returns a new instance of Game.



5
6
7
8
9
10
# 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

Instance Attribute Details

#away_scoreObject (readonly)

Returns the value of attribute away_score.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def away_score
  @away_score
end

#away_team_idObject (readonly)

Returns the value of attribute away_team_id.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def away_team_id
  @away_team_id
end

#dateObject (readonly)

Returns the value of attribute date.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def date
  @date
end

#home_scoreObject (readonly)

Returns the value of attribute home_score.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def home_score
  @home_score
end

#home_team_idObject (readonly)

Returns the value of attribute home_team_id.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def home_team_id
  @home_team_id
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/nhl_stats/game.rb', line 3

def id
  @id
end

Class Method Details

.find(id) ⇒ Object



20
21
22
23
24
# 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 = {}) ⇒ Object



26
27
28
29
30
31
32
# 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

Instance Method Details

#away_teamObject



16
17
18
# File 'lib/nhl_stats/game.rb', line 16

def away_team
  NHLStats::Team.find(away_team_id)
end

#home_teamObject



12
13
14
# File 'lib/nhl_stats/game.rb', line 12

def home_team
  NHLStats::Team.find(home_team_id)
end