nba-player-api 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 69b2bede5fc55e9fc61d0d38de8f69d3c0135d8afe65eba08e0cca52828a323b
4
+ data.tar.gz: f44a25c933220e75907ef8d5ac90f464e200c66adf024b2517a6b53e5ec7bd6e
5
+ SHA512:
6
+ metadata.gz: 0f73e5ebb2027253f75fab8d7faffce7e6d98902b87fe94f17ed0b1d11be9d418491761cd33a135b3bfb8789da54ac1a12028ac82cd1ad8aedbca444785a0aa1
7
+ data.tar.gz: 559c6660bd24b8a2a849ab81659318e6f93ebf4ecfa220c60e9da361a0e8330c4560122f72d396d8f0ae6fa086993f8fdc7ddff57839e44f0380bdcca25d07e4
@@ -0,0 +1,24 @@
1
+ require 'Faraday'
2
+ require_relative 'nba_player_api/player'
3
+ require_relative 'nba_player_api/constants'
4
+
5
+ module NBAPlayerAPI
6
+ def get_player_data(year = "#{Time.now.year - 1}-#{Time.now.strftime('%y')}")
7
+
8
+ nba_url = "https://stats.nba.com/stats/playerindex?Active=&LeagueID=00&Season=#{year}&TeamID=0"
9
+
10
+ conn = Faraday.new(
11
+ url: nba_url,
12
+ headers: NBAPlayerAPI::STAT_HEADERS
13
+ )
14
+ response = conn.get()
15
+ data = JSON.parse(response.body)
16
+ players = data['resultSets'][0]['rowSet'].map do |player|
17
+ NBAPlayerAPI::Player.new(player)
18
+ end
19
+
20
+ players
21
+ end
22
+
23
+ module_function :get_player_data
24
+ end
@@ -0,0 +1,24 @@
1
+ module NBAPlayerAPI
2
+ HEADERS = [
3
+ "PERSON_ID", "PLAYER_LAST_NAME", "PLAYER_FIRST_NAME", "PLAYER_SLUG",
4
+ "TEAM_ID", "TEAM_SLUG", "IS_DEFUNCT", "TEAM_CITY",
5
+ "TEAM_NAME", "TEAM_ABBREVIATION", "JERSEY_NUMBER", "POSITION",
6
+ "HEIGHT", "WEIGHT", "COLLEGE", "COUNTRY",
7
+ "DRAFT_YEAR", "DRAFT_ROUND", "DRAFT_NUMBER", "ROSTER_STATUS",
8
+ "FROM_YEAR", "TO_YEAR", "PTS", "REB", "AST", "STATS_TIMEFRAME"
9
+ ]
10
+
11
+ STAT_HEADERS = {
12
+ "Host"=>"stats.nba.com",
13
+ "User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:72.0) Gecko/20100101 Firefox/72.0",
14
+ "Accept"=>"application/json, text/plain, */*",
15
+ "Accept-Language"=>"en-US,en;q=0.5",
16
+ "Accept-Encoding"=>"utf-8",
17
+ "x-nba-stats-origin"=>"stats",
18
+ "x-nba-stats-token"=>"true",
19
+ "Connection"=>"keep-alive",
20
+ "Referer"=>"https://stats.nba.com/",
21
+ "Pragma"=>"no-cache",
22
+ "Cache-Control"=>"no-cache"
23
+ }
24
+ end
@@ -0,0 +1,53 @@
1
+ require_relative 'stat_card'
2
+ require_relative 'constants'
3
+
4
+ module NBAPlayerAPI
5
+ class Player
6
+
7
+ attr_reader :nba_id, :first_name, :last_name, :slug,
8
+ :current_team, :position, :active, :drafted_year,
9
+ :stat_cards
10
+
11
+ #basic initialization with attributes
12
+
13
+ def initialize(player_data)
14
+ @player_data_hash = create_player_data_hash(player_data)
15
+ @nba_id = @player_data_hash['PERSON_ID']
16
+ @first_name = @player_data_hash['PLAYER_FIRST_NAME']
17
+ @last_name = @player_data_hash['PLAYER_LAST_NAME']
18
+ @slug = @player_data_hash['PLAYER_SLUG']
19
+ @current_team = @player_data_hash['TEAM_ABBREVIATION']
20
+ @active = @player_data_hash['ROSTER_STATUS']
21
+ @position = @player_data_hash['POSITION']
22
+ @drafted_year = @player_data_hash['DRAFT_YEAR']
23
+ @stat_cards = []
24
+ @stat_cards << create_stat_card
25
+ end
26
+
27
+ def to_s
28
+ "Player: #{first_name} #{last_name} | Position: #{position} | Team: #{current_team}"
29
+ end
30
+
31
+ private
32
+
33
+ def create_player_data_hash(array)
34
+ result = {}
35
+ array.length.times do |i|
36
+ result[NBAPlayerAPI::HEADERS[i]] = array[i]
37
+ end
38
+ result
39
+ end
40
+
41
+ def create_stat_card
42
+ return nil if @player_data_hash['STATS_TIMEFRAME'] != 'Season'
43
+ stat_data = [
44
+ @player_data_hash['TO_YEAR'],
45
+ @player_data_hash['PTS'],
46
+ @player_data_hash['REB'],
47
+ @player_data_hash['AST'],
48
+ ]
49
+ NBAPlayerAPI::StatCard.new(self, stat_data)
50
+ end
51
+
52
+ end
53
+ end
@@ -0,0 +1,21 @@
1
+ module NBAPlayerAPI
2
+ class StatCard
3
+
4
+ attr_reader :player, :year, :points, :rebounds, :assists
5
+
6
+ #basic initialization with attributes
7
+
8
+ def initialize(player, stats)
9
+ @player = player
10
+ @year = stats[0].to_i
11
+ @points = stats[1]
12
+ @rebounds = stats[2]
13
+ @assists = stats[3]
14
+ end
15
+
16
+ def to_s
17
+ "Player: #{player.to_s} | Year: #{year} | Points: #{points} | Rebounds: #{rebounds} | Assists: #{assists} "
18
+ end
19
+
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nba-player-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Usborn Ocampo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2024-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Gets basic NBA player data from NBA.com
56
+ email: usborn116@gmail.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/nba-player-api.rb
62
+ - lib/nba_player_api/constants.rb
63
+ - lib/nba_player_api/player.rb
64
+ - lib/nba_player_api/stat_card.rb
65
+ homepage: https://rubygems.org/gems/nba_player_api
66
+ licenses:
67
+ - MIT
68
+ metadata: {}
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 2.7.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.5.17
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: NBA player data
88
+ test_files: []