Skip to content

Commit

Permalink
json for basketball
Browse files Browse the repository at this point in the history
  • Loading branch information
phoffer committed Jan 28, 2017
1 parent f1dfe84 commit f3d5109
Show file tree
Hide file tree
Showing 66 changed files with 62,950 additions and 153 deletions.
2 changes: 0 additions & 2 deletions lib/sportradar/api/basketball.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
require_relative 'basketball/nba/season'
require_relative 'basketball/nba/schedule'
require_relative 'basketball/nba/game'
require_relative 'basketball/nba/scoring'
require_relative 'basketball/nba/team'
require_relative 'basketball/nba/player'
require_relative 'basketball/nba/quarter'
Expand All @@ -24,7 +23,6 @@
require_relative 'basketball/ncaamb/schedule'
require_relative 'basketball/ncaamb/game'
require_relative 'basketball/ncaamb/half'
require_relative 'basketball/ncaamb/scoring'
require_relative 'basketball/ncaamb/season'
require_relative 'basketball/ncaamb/hierarchy'
require_relative 'basketball/ncaamb/division'
Expand Down
20 changes: 13 additions & 7 deletions lib/sportradar/api/basketball/game.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ def update(data, source: nil, **opts)
# via pbp
@status = data['status'] if data['status']
@coverage = data['coverage'] if data['coverage']
@home_id = data['home_team'] if data['home_team'] # GUID
@away_id = data['away_team'] if data['away_team'] # GUID
@home_id = data['home_team'] || data.dig('home', 'id') if data['home_team'] || data.dig('home', 'id')
@away_id = data['away_team'] || data.dig('away', 'id') if data['away_team'] || data.dig('away', 'id')
@home_points = data['home_points'].to_i if data['home_points']
@away_points = data['away_points'].to_i if data['away_points']

Expand Down Expand Up @@ -209,24 +209,30 @@ def get_box
end

def ingest_box(api_resp)
data = api_resp['game']
data = api_resp
update(data, source: :box)
@period = data.delete(period_name).to_i
check_newness(:box, @clock)
data
end

def queue_pbp
url, headers, options, timeout = api.get_request_info(path_pbp)
{url: url, headers: headers, params: options, timeout: timeout, callback: :ingest_pbp}
end

def get_pbp
api_resp = api.get_data(path_pbp)
ingest_pbp(api_resp)
end

def ingest_pbp(api_resp)
data = api_resp['game']
data = api_resp
period_name = 'periods'
update(data, source: :pbp)
period_data = if data[period_name]
@period = data[period_name].first.to_i
pers = data[period_name][1..-1]
@period = data[period_name].last['sequence'].to_i
pers = data[period_name]
pers.is_a?(Array) && (pers.size == 1) ? pers[0] : pers
else
@period = nil
Expand All @@ -248,7 +254,7 @@ def get_summary
end

def ingest_summary(api_resp)
data = api_resp['game']
data = api_resp
update(data, source: :summary)
@period = data.delete(period_name).to_i
check_newness(:box, @clock)
Expand Down
12 changes: 6 additions & 6 deletions lib/sportradar/api/basketball/injury.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ def initialize(data, **opts)
end

def update(data, **opts)
@id = data.dig('injury', 'id')
@comment = data.dig('injury', 'comment')
@descripton = data.dig('injury', 'desc')
@status = data.dig('injury', 'status')
@start_date = data.dig('injury', 'start_date')
@update_date = data.dig('injury', 'update_date')
@id = data.dig(0, 'id')
@comment = data.dig(0, 'comment')
@descripton = data.dig(0, 'desc')
@status = data.dig(0, 'status')
@start_date = data.dig(0, 'start_date')
@update_date = data.dig(0, 'update_date')
end

def out?
Expand Down
25 changes: 12 additions & 13 deletions lib/sportradar/api/basketball/nba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ def initialize(access_level = default_access_level)
def schedule(season_year = default_year, nba_season = default_season)
raise Sportradar::Api::Error::InvalidSeason unless allowed_seasons.include? nba_season
response = get request_url("games/#{season_year}/#{nba_season}/schedule")
if response.success? && response['league']
Sportradar::Api::Basketball::Nba::Season.new(response['league'], api: self)
if response.success?
Sportradar::Api::Basketball::Nba::Season.new(response.to_h, api: self)
else
@error = response
end
end

def daily_schedule(date = default_date, nba_season = default_season)
response = get request_url("games/#{ date.year }/#{ date.month }/#{ date.day }/schedule")
if response.success? && response['league']
Sportradar::Api::Basketball::Nba::Schedule.new(response['league'], api: self)
if response.success?
Sportradar::Api::Basketball::Nba::Schedule.new(response.to_h, api: self)
else
@error = response
end
end

def league_hierarchy
response = get request_url("league/hierarchy")
if response.success? && response["league"]
Sportradar::Api::Basketball::Nba::Hierarchy.new(response["league"], api: self)
if response.success?
Sportradar::Api::Basketball::Nba::Hierarchy.new(response.to_h, api: self)
else
response
end
end

def standings(season_year = default_year, nba_season = default_season)
response = get request_url("seasontd/#{season_year}/#{nba_season}/standings")
if response.success? && response["league"]
Sportradar::Api::Basketball::Nba::Hierarchy.new(response['league']["season"], api: self)
if response.success?
Sportradar::Api::Basketball::Nba::Hierarchy.new(response.to_h, api: self)
else
response
end
Expand All @@ -51,11 +51,6 @@ def get_data(url)
get request_url(url)
end

def get_pbp(*args)
'pbp'
end


def default_year
2016
end
Expand All @@ -73,6 +68,10 @@ def default_access_level
end
end

def content_format
'json'
end

private

def check_simulation(game_id)
Expand Down
2 changes: 1 addition & 1 deletion lib/sportradar/api/basketball/nba/conference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(data, **opts)
@id = data["id"]
@name = data["name"]
@alias = data["alias"]
@divisions_hash = create_data({}, response["division"], klass: Division, conference: self, api: @api) # if response["division"]
@divisions_hash = create_data({}, data["divisions"], klass: Division, conference: self, api: @api) # if response["division"]
end

def divisions
Expand Down
2 changes: 1 addition & 1 deletion lib/sportradar/api/basketball/nba/division.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize(data, **opts)
@name = data["name"]
@alias = data["alias"]
@assigned_teams = nil
@teams_hash = create_data({}, response["team"], klass: Team, division: self, api: @api) # if response["team"]
@teams_hash = create_data({}, data["teams"], klass: Team, division: self, api: @api) # if response["team"]
end

def teams
Expand Down
9 changes: 5 additions & 4 deletions lib/sportradar/api/basketball/nba/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def initialize(data, **opts)
@response = data
@api = opts[:api]

@id = data["id"]
@name = data["name"]
@alias = data["alias"]
@conferences_hash = create_data({}, data['conference'], klass: Conference, hierarchy: self, api: @api)
@id = data.dig('league', "id")
@name = data.dig('league', "name")
@alias = data.dig('league', "alias")
@season = Season.new(data['season']) if data['season']
@conferences_hash = create_data({}, data['conferences'], klass: Conference, hierarchy: self, api: @api)
end
def conferences
@conferences_hash.values
Expand Down
4 changes: 2 additions & 2 deletions lib/sportradar/api/basketball/nba/player.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ def update(data, **opts)
update_draft(data)

@team.update_player_stats(self, data['statistics'], opts[:game]) if data['statistics']
if avgs = data.dig('overall', 'average')
@totals = data.dig('overall', 'total')
if avgs = data['average']
@totals = data['total']
@averages = avgs.except(:player)
@team.update_player_stats(self, avgs)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sportradar/api/basketball/nba/quarter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def initialize(data, **opts)
update(data)
end
def update(data, **opts)
create_data(@plays_hash, data.dig('events', 'event'), klass: Play, api: @api, quarter: self)
create_data(@plays_hash, data.dig('events'), klass: Play, api: @api, quarter: self)
# rescue => e
# binding.pry
end
Expand Down
12 changes: 7 additions & 5 deletions lib/sportradar/api/basketball/nba/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ def initialize(data, **opts)
@response = data
@api = opts[:api]

@id = response['id']
@name = response['name']
@alias = response['alias']
@date = response.dig('daily_schedule', 'date')
# binding.pry
# ['league']
@id = data.dig('league', 'id')
@name = data.dig('league', 'name')
@alias = data.dig('league', 'alias')
@date = data.dig('date')

@games_hash = {}
update_games(data.dig('daily_schedule', 'games', 'game'))
update_games(data.dig('games'))
end

def games
Expand Down
9 changes: 5 additions & 4 deletions lib/sportradar/api/basketball/nba/season.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def initialize(data, **opts)
@response = data
@api = opts[:api]

@id = response['id']
@name = response['name']
@alias = response['alias']
@id = data.dig('league', 'id')
@name = data.dig('league', 'name')
@alias = data.dig('league', 'alias')
@games_hash = {}

update_games(data.dig('season_schedule', 'games', 'game'))
update_games(data['games'])
end

def games
Expand All @@ -39,4 +39,5 @@ def update_games(data)

sd = sr.daily_schedule;
sr = Sportradar::Api::Basketball::Nba.new
ss = sr.standings;
ss = sr.schedule;
25 changes: 12 additions & 13 deletions lib/sportradar/api/basketball/ncaamb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,35 @@ def initialize(access_level = default_access_level)
def schedule(season_year = default_year, ncaa_season = default_season)
raise Sportradar::Api::Error::InvalidSeason unless allowed_seasons.include? ncaa_season
response = get request_url("games/#{season_year}/#{ncaa_season}/schedule")
if response.success? && response['league']
Sportradar::Api::Basketball::Ncaamb::Season.new(response['league'], api: self)
if response.success?
Sportradar::Api::Basketball::Ncaamb::Season.new(response.to_h, api: self)
else
@error = response
end
end

def daily_schedule(date = default_date, ncaa_season = default_season)
response = get request_url("games/#{ date.year }/#{ date.month }/#{ date.day }/schedule")
if response.success? && response['league']
Sportradar::Api::Basketball::Ncaamb::Schedule.new(response['league'], api: self)
if response.success?
Sportradar::Api::Basketball::Ncaamb::Schedule.new(response.to_h, api: self)
else
@error = response
end
end

def hierarchy
response = get request_url("league/hierarchy")
if response.success? && response["league"]
Sportradar::Api::Basketball::Ncaamb::Hierarchy.new(response["league"], api: self)
if response.success?
Sportradar::Api::Basketball::Ncaamb::Hierarchy.new(response.to_h, api: self)
else
response
end
end

def standings(season_year = default_year, ncaa_season = default_season)
response = get request_url("seasontd/#{season_year}/#{ncaa_season}/standings")
if response.success? && response["league"]
Sportradar::Api::Basketball::Ncaamb::Division.new(response['league']['season'], api: self)
if response.success?
Sportradar::Api::Basketball::Ncaamb::Division.new(response.to_h, api: self)
else
response
end
Expand All @@ -51,11 +51,6 @@ def get_data(url)
get request_url(url)
end

def get_pbp(*args)
'pbp'
end


def default_year
2016
end
Expand All @@ -73,6 +68,10 @@ def default_access_level
end
end

def content_format
'json'
end

private

def check_simulation(game_id)
Expand Down
6 changes: 3 additions & 3 deletions lib/sportradar/api/basketball/ncaamb/conference.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ def initialize(data, **opts)
# @response = data
@api = opts[:api]

@id = data["id"]
@name = data["name"]
@id = data["id"]
@name = data["name"]
@alias = data["alias"]
@assigned_teams = nil

@teams_hash = create_data({}, data["team"], klass: Team, conference: self, api: @api) # if data["team"]
@teams_hash = create_data({}, data["teams"], klass: Team, conference: self, api: @api) # if data["team"]
end

def teams
Expand Down
8 changes: 4 additions & 4 deletions lib/sportradar/api/basketball/ncaamb/division.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ def initialize(data, **opts)
# @response = data
@api = opts[:api]

@id = data["id"]
@id = data["id"]
@name = data["name"]
@alias = data["alias"]
update(data, **opts)
end

def update(data, **opts)
@name = data["name"]
@alias = data["alias"]
@conferences_hash = create_data({}, data["conference"], klass: Conference, division: self, api: @api) if data["conference"]
@conferences_hash = create_data({}, data["conferences"], klass: Conference, division: self, api: @api) if data["conferences"]
end

def conferences
Expand Down
4 changes: 1 addition & 3 deletions lib/sportradar/api/basketball/ncaamb/half.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def initialize(data, **opts)
update(data)
end
def update(data, **opts)
create_data(@plays_hash, data.dig('events', 'event'), klass: Play, api: @api, half: self)
rescue => e
binding.pry
create_data(@plays_hash, data.dig('events'), klass: Play, api: @api, half: self)
end

def plays
Expand Down
10 changes: 5 additions & 5 deletions lib/sportradar/api/basketball/ncaamb/hierarchy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ def initialize(data, **opts)
# @response = data
@api = opts[:api]

@id = data["id"]
@name = data["name"]
@alias = data["alias"]
@id = data.dig('league', 'id')
@name = data.dig('league', 'name')
@alias = data.dig('league', 'alias')

@divisions_hash = create_data({}, data['division'], klass: Division, hierarchy: self, api: @api)
@divisions_hash = create_data({}, data['divisions'], klass: Division, hierarchy: self, api: @api)
end
def divisions
@divisions_hash.values
Expand Down Expand Up @@ -48,9 +48,9 @@ def api

st = sr.standings;

st = sr.standings;
sr = Sportradar::Api::Basketball::Ncaamb.new;
lh = sr.hierarchy;
st = sr.standings;
div = lh.division('D1');
div.teams.count
div.conferences.count
Expand Down

0 comments on commit f3d5109

Please sign in to comment.