Module: NBAPlayerAPI

Defined in:
lib/nba-player-api.rb,
lib/nba_player_api/player.rb,
lib/nba_player_api/constants.rb,
lib/nba_player_api/stat_card.rb

Defined Under Namespace

Classes: Player, StatCard

Constant Summary collapse

HEADERS =
[
  "PERSON_ID", "PLAYER_LAST_NAME", "PLAYER_FIRST_NAME", "PLAYER_SLUG", 
  "TEAM_ID", "TEAM_SLUG", "IS_DEFUNCT", "TEAM_CITY", 
  "TEAM_NAME", "TEAM_ABBREVIATION", "JERSEY_NUMBER", "POSITION", 
  "HEIGHT", "WEIGHT", "COLLEGE", "COUNTRY", 
  "DRAFT_YEAR", "DRAFT_ROUND", "DRAFT_NUMBER", "ROSTER_STATUS", 
  "FROM_YEAR", "TO_YEAR", "PTS", "REB", "AST", "STATS_TIMEFRAME"
]
STAT_HEADERS =
{
  "Host"=>"stats.nba.com",
  "User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
  "Accept"=>"application/json, text/plain, */*",
  "Accept-Language"=>"en-US,en;q=0.5",
  "Accept-Encoding"=>"utf-8",
  "x-nba-stats-origin"=>"stats",
  "x-nba-stats-token"=>"true",
  "Connection"=>"keep-alive",
  "Referer"=>"https://stats.nba.com/",
  "Pragma"=>"no-cache",
  "Cache-Control"=>"no-cache"
}

Class Method Summary collapse

Class Method Details

.get_player_data(year = "#{Time.now.year - 1}-#{Time.now.strftime('%y')}") ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/nba-player-api.rb', line 6

def get_player_data(year = "#{Time.now.year - 1}-#{Time.now.strftime('%y')}")

  nba_url = "https://stats.nba.com/stats/playerindex?Active=&LeagueID=00&Season=#{year}&TeamID=0"

  conn = Faraday.new(
      url: nba_url, 
      headers: NBAPlayerAPI::STAT_HEADERS
  )
  response = conn.get()
  data = JSON.parse(response.body)    
  players = data['resultSets'][0]['rowSet'].map do |player|
    NBAPlayerAPI::Player.new(player)
  end

  players
end