Class: Sportradar::Api::Soccer::Competition

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/soccer/competition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Data

#all_attributes, #attributes, #create_data, #parse_into_array, #parse_into_array_with_options, #parse_out_hashes, #structure_links, #update_data

Constructor Details

#initialize(data = {}, league_group: nil, **opts) ⇒ Competition

Returns a new instance of Competition.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sportradar/api/soccer/competition.rb', line 9

def initialize(data = {}, league_group: nil, **opts)
  @response     = data
  @id           = data["id"]
  @api          = opts[:api]

  @seasons_hash   = {}
  @teams_hash     = {}
  @standings_hash = {}
  @groups_hash    = {}

  update(data, **opts)
end

Instance Attribute Details

#categoryObject (readonly)

Returns the value of attribute category.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def category
  @category
end

#coverage_infoObject (readonly)

Returns the value of attribute coverage_info.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def coverage_info
  @coverage_info
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def id
  @id
end

#league_groupObject (readonly)

Returns the value of attribute league_group.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def league_group
  @league_group
end

#live_coverageObject (readonly)

Returns the value of attribute live_coverage.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def live_coverage
  @live_coverage
end

#nameObject (readonly) Also known as: display_name, alias

Returns the value of attribute name.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def name
  @name
end

#season_coverage_infoObject (readonly)

Returns the value of attribute season_coverage_info.



5
6
7
# File 'lib/sportradar/api/soccer/competition.rb', line 5

def season_coverage_info
  @season_coverage_info
end

Class Method Details

.tournament_idsObject



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/sportradar/api/soccer/competition.rb', line 226

def self.tournament_ids
  @tournament_ids ||= {
    # Europe group
    'eu.uefa_champions_league'  => "sr:tournament:7",
    'eu.la_liga'                => "sr:tournament:8",
    'eu.eng_premier_league'     => "sr:tournament:17",
    'eu.premier_league'         => "sr:tournament:17",
    'eu.serie_a'                => "sr:tournament:23",
    'eu.ligue_1'                => "sr:tournament:34",
    'eu.bundesliga'             => "sr:tournament:35",
    'eu.eredivisie'             => "sr:tournament:37",
    'eu.first_division_a'       => "sr:tournament:38",
    'eu.super_lig'              => "sr:tournament:52",
    'eu.super_league'           => "sr:tournament:185",
    'eu.rus_premier_league'     => "sr:tournament:203",
    'eu.ukr_premier_league'     => "sr:tournament:218",
    'eu.primeira_liga'          => "sr:tournament:238",
    'eu.uefa_super_cup'         => "sr:tournament:465",
    'eu.uefa_europa_league'     => "sr:tournament:679",
    'eu.uefa_youth_league'      => "sr:tournament:2324",
    # international (partial listing)
    "intl.world_cup"              => "sr:tournament:16",
    "intl.copa_america"           => "sr:tournament:133",
    "intl.gold_cup"               => "sr:tournament:140",
    "intl.africa_cup_of_nations"  => "sr:tournament:270",
    "intl.womens_world_cup"       => "sr:tournament:290",
    "intl.olympic_games"          => "sr:tournament:436",
    "intl.olympic_games_women"    => "sr:tournament:437",
    # other groups below
  }
end

Instance Method Details

#apiObject



131
132
133
# File 'lib/sportradar/api/soccer/competition.rb', line 131

def api
  @api ||= Sportradar::Api::Soccer::Api.new
end

#current_seasonObject



68
69
70
# File 'lib/sportradar/api/soccer/competition.rb', line 68

def current_season
  seasons.detect(&:current?)
end

#get_infoObject



195
196
197
198
# File 'lib/sportradar/api/soccer/competition.rb', line 195

def get_info
  data = api.get_data(path_info).to_h
  ingest_info(data)
end

#get_leadersObject



212
213
214
215
# File 'lib/sportradar/api/soccer/competition.rb', line 212

def get_leaders
  data = api.get_data(path_leaders).to_h
  ingest_leaders(data)
end

#get_live_standingsObject Also known as: get_standings



143
144
145
146
# File 'lib/sportradar/api/soccer/competition.rb', line 143

def get_live_standings
  data = api.get_data(path_live_standings).to_h
  ingest_live_standings(data)
end

#get_resultsObject



161
162
163
164
# File 'lib/sportradar/api/soccer/competition.rb', line 161

def get_results
  data = api.get_data(path_results).to_h
  ingest_results(data)
end

#get_seasonsObject



178
179
180
181
# File 'lib/sportradar/api/soccer/competition.rb', line 178

def get_seasons
  data = api.get_data(path_seasons).to_h
  ingest_seasons(data)
end

#group(name = nil) ⇒ Object

nil represents the complete team listing



88
89
90
# File 'lib/sportradar/api/soccer/competition.rb', line 88

def group(name = nil) # nil represents the complete team listing
  @groups_hash[name]
end

#groupsObject



84
85
86
# File 'lib/sportradar/api/soccer/competition.rb', line 84

def groups
  @groups_hash.values
end

#hierarchyObject



55
56
57
58
59
60
# File 'lib/sportradar/api/soccer/competition.rb', line 55

def hierarchy
  self.get_seasons
  season = self.latest_season
  season.get_competitors
  season.competitors
end

#ingest_info(data) ⇒ Object



199
200
201
202
203
# File 'lib/sportradar/api/soccer/competition.rb', line 199

def ingest_info(data)
  update(data)
  # TODO parse the rest of the data. keys: ["tournament", "season", "round", "season_coverage_info", "coverage_info", "groups"]
  data
end

#ingest_leaders(data) ⇒ Object



216
217
218
219
220
# File 'lib/sportradar/api/soccer/competition.rb', line 216

def ingest_leaders(data)
  update(data)
  # TODO parse the rest of the data. keys: ["tournament", "season_coverage_info", "top_points", "top_goals", "top_assists", "top_cards", "top_own_goals"]
  data
end

#ingest_live_standings(data) ⇒ Object



148
149
150
151
152
# File 'lib/sportradar/api/soccer/competition.rb', line 148

def ingest_live_standings(data)
  update(data)
  # TODO parse the rest of the data. keys: ["tournament", "season", "standings"]
  data
end

#ingest_results(data) ⇒ Object



165
166
167
168
169
# File 'lib/sportradar/api/soccer/competition.rb', line 165

def ingest_results(data)
  update(data)
  # TODO parse the rest of the data. keys: ["tournament", "results"]
  data
end

#ingest_seasons(data) ⇒ Object



182
183
184
185
186
# File 'lib/sportradar/api/soccer/competition.rb', line 182

def ingest_seasons(data)
  update(data)
  # TODO parse the rest of the data. keys: ["tournament", "seasons"]
  data
end

#latest_seasonObject



72
73
74
# File 'lib/sportradar/api/soccer/competition.rb', line 72

def latest_season
  seasons.max_by(&:end_date)
end

#matchesObject Also known as: games



92
93
94
# File 'lib/sportradar/api/soccer/competition.rb', line 92

def matches
  @matches_hash.values
end

#parse_info(data) ⇒ Object

parsing helpers



98
99
100
101
102
# File 'lib/sportradar/api/soccer/competition.rb', line 98

def parse_info(data)
  if data['groups']
    create_data(@groups_hash, data['groups'], klass: TeamGroup, api: api, competition: self, identifier: 'name')
  end
end

#parse_results(data) ⇒ Object



118
119
120
121
122
123
# File 'lib/sportradar/api/soccer/competition.rb', line 118

def parse_results(data)
  if data['results']
    merged_data = Soccer.parse_results(data['results'])
    create_data(@matches_hash, merged_data, klass: Match, api: api, competition: self)
  end
end

#parse_season(data) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sportradar/api/soccer/competition.rb', line 104

def parse_season(data)
  if data['season_coverage_info']
    @season_coverage_info = data['season_coverage_info'] if data['season_coverage_info']
    data['season_coverage_info']['id'] ||= data['season_coverage_info'].delete('season_id')
    create_data(@seasons_hash, data['season_coverage_info'], klass: Season, api: api, competition: self)
  end
  if data['current_season']
    create_data(@seasons_hash, data['current_season'], klass: Season, api: api, competition: self, current: true)
  end
  if data['seasons']
    create_data(@seasons_hash, data['seasons'], klass: Season, api: api, competition: self)
  end
end

#parse_standings(data) ⇒ Object



125
126
127
128
129
# File 'lib/sportradar/api/soccer/competition.rb', line 125

def parse_standings(data)
  if data['standings']
    create_data(@standings_hash, data['standings'], klass: Standing, api: api, competition: self, identifier: 'type')
  end
end

#path_baseObject

url path helpers



136
137
138
# File 'lib/sportradar/api/soccer/competition.rb', line 136

def path_base
  "competitions/#{ id }"
end

#path_infoObject



192
193
194
# File 'lib/sportradar/api/soccer/competition.rb', line 192

def path_info
  "#{ path_base }/info"
end

#path_leadersObject



209
210
211
# File 'lib/sportradar/api/soccer/competition.rb', line 209

def path_leaders
  "#{ path_base }/leaders"
end

#path_live_standingsObject



140
141
142
# File 'lib/sportradar/api/soccer/competition.rb', line 140

def path_live_standings
  "#{ path_base }/live_standings"
end

#path_resultsObject



158
159
160
# File 'lib/sportradar/api/soccer/competition.rb', line 158

def path_results
  "#{ path_base }/results"
end

#path_seasonsObject



175
176
177
# File 'lib/sportradar/api/soccer/competition.rb', line 175

def path_seasons
  "#{ path_base }/seasons"
end

#queue_infoObject



204
205
206
207
# File 'lib/sportradar/api/soccer/competition.rb', line 204

def queue_info
  url, headers, options, timeout = api.get_request_info(path_info)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_info)}
end

#queue_leadersObject



221
222
223
224
# File 'lib/sportradar/api/soccer/competition.rb', line 221

def queue_leaders
  url, headers, options, timeout = api.get_request_leaders(path_leaders)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_leaders)}
end

#queue_live_standingsObject



153
154
155
156
# File 'lib/sportradar/api/soccer/competition.rb', line 153

def queue_live_standings
  url, headers, options, timeout = api.get_request_info(path_live_standings)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_live_standings)}
end

#queue_resultsObject



170
171
172
173
# File 'lib/sportradar/api/soccer/competition.rb', line 170

def queue_results
  url, headers, options, timeout = api.get_request_info(path_results)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_results)}
end

#queue_seasonsObject



187
188
189
190
# File 'lib/sportradar/api/soccer/competition.rb', line 187

def queue_seasons
  url, headers, options, timeout = api.get_request_info(path_seasons)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_seasons)}
end

#scheduleObject



43
44
45
46
47
# File 'lib/sportradar/api/soccer/competition.rb', line 43

def schedule
  return self if @schedule_retrieved
  get_schedule
  self
end

#seasonsObject



39
40
41
# File 'lib/sportradar/api/soccer/competition.rb', line 39

def seasons
  @seasons_hash.values
end

#standings(type = nil) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/sportradar/api/soccer/competition.rb', line 76

def standings(type = nil)
  if type
    @standings_hash[type]
  else
    @standings_hash.values
  end
end

#teamsObject



62
63
64
65
66
# File 'lib/sportradar/api/soccer/competition.rb', line 62

def teams
  season = self.latest_season
  season.get_competitors if season.competitors.empty?
  season.competitors
end

#update(data, **opts) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sportradar/api/soccer/competition.rb', line 22

def update(data, **opts)
  # if data['tournament']
  #   update(data['tournament'])
  # end

  @name           = data["name"]          || @name
  @category       = data['category']      || @category
  @gender         = data['gender']        || @gender
  @coverage_info  = data['coverage_info'] || @coverage_info
  @live_coverage  = data.dig('coverage_info', 'live_coverage') || @live_coverage

  # parse_info(data)
  parse_season(data)
  # parse_results(data)
  # parse_standings(data)
end

#yearObject



49
50
51
52
53
# File 'lib/sportradar/api/soccer/competition.rb', line 49

def year
  if current_season&.year&.split('/')&.last
    2000 + current_season.year.split('/').last.to_i
  end
end