lib/sportradar/api/soccer/match.rb in sportradar-api-0.12.3 vs lib/sportradar/api/soccer/match.rb in sportradar-api-0.13.0
- old
+ new
@@ -1,55 +1,235 @@
module Sportradar
module Api
- class Soccer::Match < Data
+ module Soccer
+ class Match < Data
+ attr_reader :id, :league_group, :scheduled, :start_time_tbd, :status, :tournament_round, :match_status, :venue
+ attr_reader :home_score, :away_score, :winner_id, :aggregate_home_score, :aggregate_away_score, :aggregate_winner_id
+ attr_reader :referee, :weather_info, :coverage_info, :probabilities
+ attr_reader :home, :away, :tournament_id
- attr_accessor :id, :status, :scheduled, :scratched, :season_id, :reference_id, :category, :tournament_group, :tournament, :home, :away, :venue, :round, :coverage, :period, :clock, :referee, :facts, :response, :future_game, :winner
+ def initialize(data = {}, league_group: nil, **opts)
+ @response = data
+ @id = data['id']
+ @api = opts[:api]
- def initialize(data)
- @response = data
- @id = data["id"]
- @reference_id = data["reference_id"]
- @scheduled = Time.parse(data["scheduled"]) if data["scheduled"]
- @scratched = data["scratched"] == "true"
- @season_id = data["season_id"]
- @status = data["status"]
- @category = Sportradar::Api::Soccer::Category.new data["category"] if data["category"]
- @coverage = OpenStruct.new data["coverage"]
- @round = OpenStruct.new data["round"]
- @tournament = Sportradar::Api::Soccer::Tournament.new data["tournament"] if data["tournament"]
- @tournament_group = Sportradar::Api::Soccer::TournamentGroup.new data["tournament_group"] if data["tournament_group"]
+ @league_group = league_group || data['league_group'] || @api&.league_group
- @away = Sportradar::Api::Soccer::Team.new data["away"] if data["away"]
- @home = Sportradar::Api::Soccer::Team.new data["home"] if data["home"]
- @venue = Sportradar::Api::Soccer::Venue.new data["venue"] if data["venue"]
+ @timeline_hash = {}
+ @lineups_hash = {}
+ get_tournament_id(data, **opts)
+ @home = Team.new({}, api: api, match: self)
+ @away = Team.new({}, api: api, match: self)
+ @teams_hash = { away: @away, home: @home }
- @future_game = @scheduled > Time.now.utc
- @winner = find_winner if @home && @away && !future_game
+ update(data, **opts)
+ end
- # Actual stats from match summary
- @period = data["period"]
- @clock = data["clock"]
- @referee = OpenStruct.new data["referee"] if data["referee"]
- @facts = data["facts"]["fact"].map {|fact| Sportradar::Api::Soccer::Fact.new fact } if data["facts"]
- end
+ def update(data, **opts)
+ @league_group = opts[:league_group] || data['league_group'] || @league_group
+ get_tournament_id(data, **opts)
+ if data["sport_event"]
+ update(data["sport_event"])
+ end
+ if data["sport_event_status"]
+ update(data["sport_event_status"])
+ end
+ if data['sport_event_conditions']
+ update(data['sport_event_conditions'])
+ end
+ if data['probabilities']
+ # update(data['probabilities'])
+ @probabilities = data['probabilities'] # tidy this up later
+ end
+ if data['lineups']
+ create_data(@lineups_hash, data['lineups'], klass: Lineup, identifier: 'team', api: api)
+ end
+ if (stats = data.dig('statistics', 'teams'))
+ update_teams(stats)
+ end
- def find_winner
- if @home.winner == 'draw'
- 'draw'
- else
- [@home, @away].find {|team| team.winner == "true" }
+
+ @scheduled = Time.parse(data['scheduled']) if data['scheduled']
+ @start_time_tbd = data['start_time_tbd'] if data.key?('start_time_tbd')
+ @status = data['status'] if data['status']
+ @match_status = data['match_status'] if data['match_status']
+ @tournament_round = data['tournament_round'] if data['tournament_round']
+ @venue = Venue.new(data['venue']) if data['venue']
+ @weather_info = OpenStruct.new(data["weather_info"]) if data["weather_info"]
+ @referee = OpenStruct.new(data["referee"]) if data["referee"]
+ @coverage_info = OpenStruct.new(data["coverage_info"]) if data["coverage_info"]
+
+ @home_score = data['home_score'] if data['home_score']
+ @away_score = data['away_score'] if data['away_score']
+ @winner_id = data['winner_id'] if data['winner_id']
+
+ @aggregate_home_score = data['aggregate_home_score'] if data['aggregate_home_score']
+ @aggregate_away_score = data['aggregate_away_score'] if data['aggregate_away_score']
+ @aggregate_winner_id = data['aggregate_winner_id'] if data['aggregate_winner_id']
+ create_data(@timeline_hash, data['timeline'], klass: Event, api: api)
+ # @season
+ # @tournament
+ if data['competitors']
+ update_teams(data['competitors'])
+ end
+
+ # parse_nested_data(data)
end
- end
+ def update_teams(data)
+ home_hash = data.detect { |team_hash| team_hash["qualifier"] == "home" || team_hash["team"] == "home" }
+ away_hash = (data - [home_hash]).first
+ if home_hash && away_hash
+ @home.update(home_hash, match: self)
+ @away.update(away_hash, match: self)
+ @teams_hash[@home.id] = @home
+ @teams_hash[@away.id] = @away
+ end
+ end
- def period_name
- period_names = {"P1" => "Period one", "H" => "Halftime", "P2" => "Period two", "PX1" => "Pre-extra time one", "X1" => "Extra time one", "PX2" => "Pre-extra time two", "X2" => "Extra time two", "PP" => "Pre-penalty", "P" => "Penalty"}
- period_names[period] if period
- end
+ def get_tournament_id(data, **opts)
+ @tournament_id ||= if opts[:tournament]
+ opts[:tournament].id
+ elsif opts[:season]
+ opts[:season].tournament_id
+ elsif opts[:match]
+ opts[:match].tournament_id
+ elsif data['tournament']
+ data.dig('tournament', 'id')
+ elsif data['season']
+ data.dig('season', 'tournament_id')
+ end
+ end
- def status_description
- status_descriptions = {"scheduled" => "The match is scheduled to be played", "inprogress" => "The match is currently in progress", "postponed" => "The match has been postponed to a future date", "delayed" => "The match has been temporarily delayed and will be continued", "canceled" => "The match has been canceled and will not be played", "closed" => "The match is over"}
- status_descriptions[status] if status
+ def team(place_or_id)
+ @teams_hash[place_or_id]
+ end
+
+ def timeline(type = nil)
+ if type
+ @timeline_hash.each_value.select { |ev| ev.type == type }
+ else
+ @timeline_hash.values
+ end
+ end
+
+ def lineups(which = nil)
+ if which
+ @lineups_hash[which.to_s]
+ else
+ @lineups_hash.values
+ end
+ end
+
+ def api
+ @api ||= Sportradar::Api::Soccer::Api.new(league_group: @league_group)
+ end
+
+ def path_base
+ "matches/#{ id }"
+ end
+
+ def path_summary
+ "#{ path_base }/summary"
+ end
+ def get_summary
+ data = api.get_data(path_summary).to_h
+ ingest_summary(data)
+ end
+ def ingest_summary(data)
+ update(data)
+ data
+ end
+ def queue_summary
+ url, headers, options, timeout = api.get_request_info(path_summary)
+ {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_summary)}
+ end
+
+ def path_lineups
+ "#{ path_base }/lineups"
+ end
+ def get_lineups
+ data = api.get_data(path_lineups).to_h
+ ingest_lineups(data)
+ end
+ def ingest_lineups(data)
+ update(data)
+ data
+ end
+ def queue_lineups
+ url, headers, options, timeout = api.get_request_info(path_lineups)
+ {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_lineups)}
+ end
+
+ def path_facts
+ "#{ path_base }/funfacts"
+ end
+ def get_facts
+ data = api.get_data(path_facts).to_h
+ ingest_facts(data)
+ end
+ def ingest_facts(data)
+ update(data)
+ data
+ end
+ def queue_facts
+ url, headers, options, timeout = api.get_request_info(path_facts)
+ {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_facts)}
+ end
+
+ def path_probabilities
+ "#{ path_base }/probabilities"
+ end
+ def get_probabilities
+ data = api.get_data(path_probabilities).to_h
+ ingest_probabilities(data)
+ end
+ def ingest_probabilities(data)
+ update(data)
+ data
+ end
+ def queue_probabilities
+ url, headers, options, timeout = api.get_request_info(path_probabilities)
+ {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_probabilities)}
+ end
+
+ def path_timeline
+ "#{ path_base }/timeline"
+ end
+ def get_timeline
+ data = api.get_data(path_timeline).to_h
+ ingest_timeline(data)
+ end
+ def ingest_timeline(data)
+ update(data)
+ data
+ end
+ def queue_timeline
+ url, headers, options, timeout = api.get_request_info(path_timeline)
+ {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_timeline)}
+ end
+
end
end
end
end
+
+__END__
+
+"id"=>"sr:match:12090446",
+"scheduled"=>"2018-05-20T18:45:00+00:00",
+"start_time_tbd"=>true,
+"status"=>"not_started",
+"tournament_round"=>{"type"=>"group", "number"=>38},
+"season"=>{"id"=>"sr:season:42720", "name"=>"Serie A 17/18", "start_date"=>"2017-08-19", "end_date"=>"2018-05-21", "year"=>"17/18", "tournament_id"=>"sr:tournament:23"},
+"tournament"=>{"id"=>"sr:tournament:23", "name"=>"Serie A", "sport"=>{"id"=>"sr:sport:1", "name"=>"Soccer"}, "category"=>{"id"=>"sr:category:31", "name"=>"Italy", "country_code"=>"ITA"}},
+"competitors"=>
+ [{"id"=>"sr:competitor:2793", "name"=>"US Sassuolo", "country"=>"Italy", "country_code"=>"ITA", "abbreviation"=>"SAS", "qualifier"=>"home"},
+ {"id"=>"sr:competitor:2702", "name"=>"AS Roma", "country"=>"Italy", "country_code"=>"ITA", "abbreviation"=>"ROM", "qualifier"=>"away"}]
+
+
+m = Sportradar::Api::Soccer::Match.new({"id"=>"sr:match:12090446"}, league_group: 'eu')
+res = m.get_summary
+res = m.get_lineups
+res = m.get_facts
+res = m.get_probabilities
+res = m.get_timeline