sports_data_api 0.11.6 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (32) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +1 -0
  3. data/lib/sports_data_api/nba/broadcast.rb +1 -9
  4. data/lib/sports_data_api/nba/game.rb +56 -37
  5. data/lib/sports_data_api/nba/games.rb +16 -13
  6. data/lib/sports_data_api/nba/player.rb +4 -16
  7. data/lib/sports_data_api/nba/season.rb +15 -10
  8. data/lib/sports_data_api/nba/stats.rb +6 -0
  9. data/lib/sports_data_api/nba/team.rb +29 -17
  10. data/lib/sports_data_api/nba/teams.rb +14 -65
  11. data/lib/sports_data_api/nba/venue.rb +10 -12
  12. data/lib/sports_data_api/nba.rb +10 -9
  13. data/lib/sports_data_api/version.rb +1 -1
  14. data/spec/cassettes/sports_data_api_nba.yml +66434 -112102
  15. data/spec/cassettes/sports_data_api_nba_broadcast.yml +67 -56
  16. data/spec/cassettes/sports_data_api_nba_game.yml +31079 -21019
  17. data/spec/cassettes/sports_data_api_nba_games.yml +112 -68
  18. data/spec/cassettes/sports_data_api_nba_league_hierarchy.yml +553 -469
  19. data/spec/cassettes/sports_data_api_nba_player.yml +1173 -212
  20. data/spec/cassettes/sports_data_api_nba_season.yml +18253 -18195
  21. data/spec/cassettes/sports_data_api_nba_team.yml +1429 -1284
  22. data/spec/cassettes/sports_data_api_nba_venue.yml +438 -56
  23. data/spec/lib/sports_data_api/nba/broadcast_spec.rb +8 -4
  24. data/spec/lib/sports_data_api/nba/game_spec.rb +32 -41
  25. data/spec/lib/sports_data_api/nba/games_spec.rb +10 -6
  26. data/spec/lib/sports_data_api/nba/player_spec.rb +86 -56
  27. data/spec/lib/sports_data_api/nba/season_spec.rb +4 -3
  28. data/spec/lib/sports_data_api/nba/team_spec.rb +44 -48
  29. data/spec/lib/sports_data_api/nba/teams_spec.rb +12 -63
  30. data/spec/lib/sports_data_api/nba/venue_spec.rb +23 -6
  31. data/spec/lib/sports_data_api/nba_spec.rb +97 -15
  32. metadata +4 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3511d9beaa61092bb9e781e948e51229d09daeb5
4
- data.tar.gz: e6c9aafd2aacffa71397b865c151cb71a5bfa21c
2
+ SHA256:
3
+ metadata.gz: 8a2556b1cd7f55fd366400025462306b46fb1dc9a45449e4d038b15e3c2dc344
4
+ data.tar.gz: b3a2452b53e1aa99f4ea928bc64ec12f60fc18ae2f3140a8c8379a4d0f2742c2
5
5
  SHA512:
6
- metadata.gz: 9dc906dedbf8503dbe1aed7dc3bedbf78f8f9fdb444fc9d2344bb322c94059224c7335f5558a972511aa24831bcda20e9ebe86d7350511107d90c0e26ab7d9dd
7
- data.tar.gz: 6e7aaebc3155e47b2975c7faf65d55fdd22eb1adfa6217f70af9979676306ae5d468e146c0bb91bd3288ef96c4fd6d96f0a4bc7ee55c9b13ab86ed422533bba2
6
+ metadata.gz: d63d5635e7b7097506310fd81257ad21cd0f209bf42cbc8fa30667ade1ab11feadef2811f4d675da5dc6ffcc8cf2b2e2d03d6b19c0ee7327e3244ae5db58d588
7
+ data.tar.gz: 47adca1c31f93dd5cfadffd80dd475c036cae42ef1cf371d409a92f1c2eca7f8ae21d0bca8d4cfddbb44e97518a25eeec4e82e592fe48aa7e4988e449abdaa1b
data/.travis.yml CHANGED
@@ -5,6 +5,7 @@ rvm:
5
5
  - 2.2
6
6
  - 2.3
7
7
  - 2.4.0
8
+ - 2.5.0
8
9
  - ruby-head
9
10
  addons:
10
11
  code_climate:
@@ -1,14 +1,6 @@
1
1
  module SportsDataApi
2
2
  module Nba
3
- class Broadcast
4
- attr_reader :network, :satellite
5
- def initialize(xml)
6
- xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
7
- if xml.is_a? Nokogiri::XML::Element
8
- @network = xml['network']
9
- @satellite = xml['satellite']
10
- end
11
- end
3
+ class Broadcast < SportsDataApi::JsonData
12
4
  end
13
5
  end
14
6
  end
@@ -1,49 +1,56 @@
1
1
  module SportsDataApi
2
2
  module Nba
3
3
  class Game
4
- attr_reader :id, :scheduled, :home, :home_team, :away,
5
- :away_team, :status, :venue, :broadcast, :year, :season,
6
- :date, :quarter, :clock, :home_team_id, :away_team_id
7
-
8
- def initialize(args={})
9
- xml = args.fetch(:xml)
10
- @year = args[:year] ? args[:year].to_i : nil
11
- @season = args[:season] ? args[:season].to_sym : nil
12
- @date = args[:date]
13
-
14
- xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
15
- if xml.is_a? Nokogiri::XML::Element
16
- @id = xml['id']
17
- @scheduled = Time.parse xml['scheduled']
18
- @home = xml['home_team']
19
- @away = xml['away_team']
20
- @home_team_id = xml['home_team']
21
- @away_team_id = xml['away_team']
22
- @status = xml['status']
23
- @clock = xml['clock']
24
- @quarter = xml['quarter'] ? xml['quarter'].to_i : nil
25
-
26
- team_xml = xml.xpath('team')
27
- if team_xml.empty?
28
- # we are coming from the schedule API
29
- @home_team = Team.new(xml.xpath('home'))
30
- @away_team = Team.new(xml.xpath('away'))
31
- else
32
- # we are coming from the Game Summary API
33
- @home_team = Team.new(team_xml.first)
34
- @away_team = Team.new(team_xml.last)
35
- end
36
-
37
- @venue = Venue.new(xml.xpath('venue'))
38
- @broadcast = Broadcast.new(xml.xpath('broadcast'))
39
- end
4
+ attr_reader :id, :status, :year, :season, :clock
5
+
6
+ def initialize(json:, year: nil, season: nil)
7
+ @json = json
8
+ @year = year
9
+ @season = season
10
+ @id = json['id']
11
+ @status = json['status']
12
+ @clock = json['clock']
13
+ end
14
+
15
+ def scheduled
16
+ @scheduled ||= Time.iso8601 json['scheduled']
17
+ end
18
+
19
+ def home_team_id
20
+ json['home']['id']
21
+ end
22
+
23
+ def away_team_id
24
+ json['away']['id']
25
+ end
26
+
27
+ def home_team
28
+ @home_team ||= Team.new(json['home'])
29
+ end
30
+
31
+ def away_team
32
+ @away_team ||= Team.new(json['away'])
33
+ end
34
+
35
+ def quarter
36
+ return unless json['quarter']
37
+ json['quarter'].to_i
38
+ end
39
+
40
+ def venue
41
+ @venue ||= Venue.new(json['venue'])
42
+ end
43
+
44
+ def broadcast
45
+ return nil if json['broadcast'].nil? || json['broadcast'].empty?
46
+ @broadcast ||= Broadcast.new(json['broadcast'])
40
47
  end
41
48
 
42
49
  ##
43
50
  # Wrapper for Nba.game_summary
44
51
  # TODO
45
52
  def summary
46
- Nba.game_summary(@id)
53
+ Nba.game_summary(id)
47
54
  end
48
55
 
49
56
  ##
@@ -59,6 +66,18 @@ module SportsDataApi
59
66
  def boxscore
60
67
  raise NotImplementedError
61
68
  end
69
+
70
+ private
71
+
72
+ attr_reader :json
73
+
74
+ def game_summary?
75
+ !team_json.nil?
76
+ end
77
+
78
+ def team_json
79
+ @team_json ||= json['team']
80
+ end
62
81
  end
63
82
  end
64
83
  end
@@ -2,25 +2,28 @@ module SportsDataApi
2
2
  module Nba
3
3
  class Games
4
4
  include Enumerable
5
- attr_reader :games, :date
6
5
 
7
- def initialize(xml)
8
- @date = xml.first['date']
6
+ attr_reader :date
9
7
 
10
- @games = xml.xpath("games/game").map do |game_xml|
11
- Game.new(date: @date, xml: game_xml)
12
- end
8
+ def initialize(json)
9
+ @json = json
10
+ @date = json['date']
13
11
  end
14
12
 
15
- def each &block
16
- @games.each do |game|
17
- if block_given?
18
- block.call game
19
- else
20
- yield game
21
- end
13
+ def games
14
+ @games ||= json['games'].map do |game_json|
15
+ Game.new(json: game_json)
22
16
  end
23
17
  end
18
+
19
+ def each
20
+ return games.each unless block_given?
21
+ games.each { |game| yield game }
22
+ end
23
+
24
+ private
25
+
26
+ attr_reader :json
24
27
  end
25
28
  end
26
29
  end
@@ -1,21 +1,9 @@
1
1
  module SportsDataApi
2
2
  module Nba
3
- class Player
4
- attr_reader :stats
5
-
6
- def initialize(xml)
7
- if xml.is_a? Nokogiri::XML::Element
8
- player_ivar = self.instance_variable_set("@#{xml.name}", {})
9
- self.class.class_eval { attr_reader :"#{xml.name}" }
10
- xml.attributes.each do | attr_name, attr_value|
11
- player_ivar[attr_name.to_sym] = attr_value.value
12
- end
13
-
14
- stats_xml = xml.xpath('statistics')
15
- if stats_xml.is_a? Nokogiri::XML::NodeSet and stats_xml.count > 0
16
- @stats = SportsDataApi::Stats.new(stats_xml.first)
17
- end
18
- end
3
+ class Player < SportsDataApi::JsonData
4
+ def stats
5
+ return if player[:statistics].nil? || player[:statistics].empty?
6
+ @stats ||= SportsDataApi::Nba::Stats.new(player[:statistics])
19
7
  end
20
8
  end
21
9
  end
@@ -1,17 +1,18 @@
1
1
  module SportsDataApi
2
2
  module Nba
3
3
  class Season
4
- attr_reader :id, :year, :type, :games
4
+ attr_reader :id, :year, :type
5
5
 
6
- def initialize(xml)
7
- if xml.is_a? Nokogiri::XML::NodeSet
8
- @id = xml.first["id"]
9
- @year = xml.first["year"].to_i
10
- @type = xml.first["type"].to_sym
6
+ def initialize(json)
7
+ @json = json
8
+ @id = json['season']['id']
9
+ @year = json['season']['year']
10
+ @type = json['season']['type'].to_sym
11
+ end
11
12
 
12
- @games = xml.first.xpath("games/game").map do |game_xml|
13
- Game.new(year: @year, season: @type, xml: game_xml)
14
- end
13
+ def games
14
+ @games ||= json['games'].map do |game_json|
15
+ Game.new(year: year, season: type, json: game_json)
15
16
  end
16
17
  end
17
18
 
@@ -21,8 +22,12 @@ module SportsDataApi
21
22
  #
22
23
  # The only valid types are: :pre, :reg, :pst
23
24
  def self.valid?(season)
24
- [:PRE, :REG, :PST].include?(season)
25
+ %i[PRE REG PST].include?(season)
25
26
  end
27
+
28
+ private
29
+
30
+ attr_reader :json
26
31
  end
27
32
  end
28
33
  end
@@ -0,0 +1,6 @@
1
+ module SportsDataApi
2
+ module Nba
3
+ class Stats < SportsDataApi::JsonData
4
+ end
5
+ end
6
+ end
@@ -1,23 +1,31 @@
1
1
  module SportsDataApi
2
2
  module Nba
3
3
  class Team
4
- attr_reader :id, :name, :market, :alias, :conference, :division,
5
- :stats, :players, :points
6
-
7
- def initialize(xml, conference = nil, division = nil)
8
- xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
9
- if xml.is_a? Nokogiri::XML::Element
10
- @id = xml['id']
11
- @name = xml['name']
12
- @market = xml['market']
13
- @alias = xml['alias']
14
- @points = xml['points'] ? xml['points'].to_i : nil
15
- @conference = conference
16
- @division = division
17
- @players = xml.xpath("players/player").map do |player_xml|
18
- Player.new(player_xml)
19
- end
20
- end
4
+ attr_reader :id, :name, :market, :alias, :conference, :division, :stats
5
+
6
+ def initialize(json, conference = nil, division = nil)
7
+ @conference = conference
8
+ @division = division
9
+ @id = json['id']
10
+ @name = json['name']
11
+ @market = json['market']
12
+ @alias = json['alias']
13
+ @points = json['points']
14
+ @alias = json['alias']
15
+ @json = json
16
+ end
17
+
18
+ def points
19
+ @points ||= json['points'] ? json['points'].to_i : nil
20
+ end
21
+
22
+ def players
23
+ return [] if json['players'].nil? || json['players'].empty?
24
+ @players ||= json['players'].map { |x| Player.new(x) }
25
+ end
26
+
27
+ def venue
28
+ @venue ||= Venue.new(json['venue']) if json['venue']
21
29
  end
22
30
 
23
31
  ##
@@ -34,6 +42,10 @@ module SportsDataApi
34
42
  super(other)
35
43
  end
36
44
  end
45
+
46
+ private
47
+
48
+ attr_reader :json
37
49
  end
38
50
  end
39
51
  end
@@ -3,81 +3,30 @@ module SportsDataApi
3
3
  class Teams
4
4
  include Enumerable
5
5
 
6
- attr_reader :conferences, :divisions
7
-
8
- ##
9
- # Initialize by passing the raw XML returned from the API
10
- def initialize(xml)
11
- @teams = []
12
- xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
13
- if xml.is_a? Nokogiri::XML::Element
14
- xml.xpath('conference').each do |conference|
15
- # Conference ID, e.g., EASTERN or WESTERN
16
- cname = conference['alias']
17
-
18
- conference.xpath('division').each do |division|
19
- # Division ID, e.g., ATLANTIC, PACIFIC
20
- dname = division['alias']
6
+ def initialize(json)
7
+ @json = json
8
+ end
21
9
 
22
- # Create a new team
23
- @teams << division.xpath('team').map { |team| Team.new(team, cname, dname) }
10
+ def teams
11
+ @teams ||= json['conferences'].flat_map do |conference|
12
+ conference['divisions'].flat_map do |division|
13
+ division['teams'].map do |json|
14
+ Team.new(json, conference['alias'], division['alias'])
24
15
  end
25
16
  end
26
17
  end
27
-
28
- @teams.flatten!
29
-
30
- uniq_conferences = @teams.map { |team| team.conference.upcase }.uniq
31
- @allowed_conferences = uniq_conferences.map { |str| str.to_sym }.concat(uniq_conferences.map { |str| str.downcase.to_sym })
32
- @conferences = uniq_conferences.map { |conf| conf.to_sym }
33
-
34
- uniq_divisions = @teams.map { |team| team.division.upcase }.uniq
35
- @divisions = @teams.map { |team| team.division.to_sym }.uniq
36
- @allowed_divisions = uniq_divisions.map { |str| str.to_sym }.concat(uniq_divisions.map { |str| str.downcase.to_sym })
37
- end
38
-
39
- def [](search_index)
40
- found_index = @teams.index(search_index)
41
- unless found_index.nil?
42
- @teams[found_index]
43
- end
44
18
  end
45
19
 
46
20
  ##
47
- #
48
- def respond_to?(method)
49
- @allowed_conferences.include?(method) || @allowed_divisions.include?(method)
21
+ # Make the class Enumerable
22
+ def each
23
+ return teams.each unless block_given?
24
+ teams.each { |team| yield team }
50
25
  end
51
26
 
52
- ##
53
- #
54
- def method_missing(method)
55
- if @allowed_conferences.include?(method)
56
- @teams.select do |team|
57
- up = team.conference.upcase.to_sym
58
- dw = team.conference.downcase.to_sym
59
- up === method || dw === method
60
- end
61
- elsif @allowed_divisions.include?(method)
62
- @teams.select do |team|
63
- up = team.division.upcase.to_sym
64
- dw = team.division.downcase.to_sym
65
- up === method || dw === method
66
- end
67
- end
68
- end
27
+ private
69
28
 
70
- ##
71
- # Make the class Enumerable
72
- def each(&block)
73
- @teams.each do |team|
74
- if block_given?
75
- block.call team
76
- else
77
- yield team
78
- end
79
- end
80
- end
29
+ attr_reader :json
81
30
  end
82
31
  end
83
32
  end
@@ -2,18 +2,16 @@ module SportsDataApi
2
2
  module Nba
3
3
  class Venue
4
4
  attr_reader :id, :name, :address, :city, :state, :zip, :country, :capacity
5
- def initialize(xml)
6
- xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
7
- if xml.is_a? Nokogiri::XML::Element
8
- @id = xml['id']
9
- @name = xml['name']
10
- @address = xml['address']
11
- @city = xml['city']
12
- @state = xml['state']
13
- @zip = xml['zip']
14
- @country = xml['country']
15
- @capacity = xml['capacity']
16
- end
5
+
6
+ def initialize(json)
7
+ @id = json['id']
8
+ @name = json['name']
9
+ @address = json['address']
10
+ @city = json['city']
11
+ @state = json['state']
12
+ @zip = json['zip']
13
+ @country = json['country']
14
+ @capacity = json['capacity']
17
15
  end
18
16
  end
19
17
  end
@@ -5,8 +5,8 @@ module SportsDataApi
5
5
  class Exception < ::Exception
6
6
  end
7
7
 
8
- API_VERSION = 3
9
- BASE_URL = 'https://api.sportsdatallc.org/nba-%{access_level}%{version}'
8
+ API_VERSION = 4
9
+ BASE_URL = 'https://api.sportradar.us/nba/%{access_level}/v%{version}/en'
10
10
  DIR = File.join(File.dirname(__FILE__), 'nba')
11
11
  SPORT = :nba
12
12
 
@@ -16,6 +16,7 @@ module SportsDataApi
16
16
  autoload :Game, File.join(DIR, 'game')
17
17
  autoload :Games, File.join(DIR, 'games')
18
18
  autoload :Season, File.join(DIR, 'season')
19
+ autoload :Stats, File.join(DIR, 'stats')
19
20
  autoload :Venue, File.join(DIR, 'venue')
20
21
  autoload :Broadcast, File.join(DIR, 'broadcast')
21
22
 
@@ -26,31 +27,31 @@ module SportsDataApi
26
27
  season = season.to_s.upcase.to_sym
27
28
  raise Exception.new("#{season} is not a valid season") unless Season.valid?(season)
28
29
 
29
- Season.new(response_xml_xpath("/games/#{year}/#{season}/schedule.xml", "/league/season-schedule"))
30
+ Season.new(response_json("/games/#{year}/#{season}/schedule.json"))
30
31
  end
31
32
 
32
33
  ##
33
34
  # Fetches NBA team roster
34
- def team_roster(team)
35
- Team.new(response_xml_xpath("/teams/#{team}/profile.xml", 'team'))
35
+ def team_roster(team_id)
36
+ Team.new(response_json("/teams/#{team_id}/profile.json"))
36
37
  end
37
38
 
38
39
  ##
39
40
  # Fetches NBA game summary for a given game
40
- def game_summary(game)
41
- Game.new(xml: response_xml_xpath("/games/#{game}/summary.xml", '/game'))
41
+ def game_summary(game_id)
42
+ Game.new(json: response_json("/games/#{game_id}/summary.json"))
42
43
  end
43
44
 
44
45
  ##
45
46
  # Fetches all NBA teams
46
47
  def teams
47
- Teams.new(response_xml_xpath('/league/hierarchy.xml', '/league'))
48
+ Teams.new(response_json('/league/hierarchy.json'))
48
49
  end
49
50
 
50
51
  ##
51
52
  # Fetches NBA daily schedule for a given date
52
53
  def daily(year, month, day)
53
- Games.new(response_xml_xpath("/games/#{year}/#{month}/#{day}/schedule.xml", 'league/daily-schedule'))
54
+ Games.new(response_json("/games/#{year}/#{month}/#{day}/schedule.json"))
54
55
  end
55
56
  end
56
57
  end
@@ -1,3 +1,3 @@
1
1
  module SportsDataApi
2
- VERSION = '0.11.6'
2
+ VERSION = '0.12.0'
3
3
  end