Class: NHLStats::Player

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(player_data) ⇒ Player

Returns a new instance of Player.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/nhl_stats/player.rb', line 6

def initialize(player_data)
  @id = player_data["id"]
  @full_name = player_data["fullName"]
  @number = (player_data["primaryNumber"] || player_data["jerseyNumber"]).to_i
  @position = player_data.dig("primaryPosition", "abbreviation") || player_data.dig("position", "abbreviation")

  # Roster response returns an array of players w/o the following
  if player_data.key?("firstName")
    roster_response_data(player_data)
  else
    @active = true
  end
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



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

def active
  @active
end

#birth_dateObject (readonly)

Returns the value of attribute birth_date.



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

def birth_date
  @birth_date
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



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

def first_name
  @first_name
end

#full_nameObject (readonly)

Returns the value of attribute full_name.



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

def full_name
  @full_name
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



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

def last_name
  @last_name
end

#nationalityObject (readonly)

Returns the value of attribute nationality.



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

def nationality
  @nationality
end

#numberObject (readonly)

Returns the value of attribute number.



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

def number
  @number
end

#positionObject (readonly)

Returns the value of attribute position.



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

def position
  @position
end

Class Method Details

.find(id) ⇒ Object



20
21
22
23
24
# File 'lib/nhl_stats/player.rb', line 20

def self.find(id)
  response = Faraday.get("#{API_ROOT}/people/#{id}")
  attributes = JSON.parse(response.body).dig("people", 0)
  new(attributes)
end