Class: Sportradar::Api::Football::Team

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/football/team.rb

Direct Known Subclasses

Ncaafb::Team, Nfl::Team

Instance Attribute 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, **opts) ⇒ Team

Returns a new instance of Team.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sportradar/api/football/team.rb', line 7

def initialize(data, **opts)
  @response = data
  @api      = opts[:api]

  @games_hash   = {}
  @players_hash = {}
  @player_stats = {}
  @records      = {}
  @stats        = nil
  @points       = nil

  case data
  when String
    @id = data
  when Hash
    @id = data['id']
    update(data, **opts)
  end
end

Instance Attribute Details

#aliasObject

Returns the value of attribute alias.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def alias
  @alias
end

#full_nameObject

Returns the value of attribute full_name.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def full_name
  @full_name
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def id
  @id
end

#marketObject

Returns the value of attribute market.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def market
  @market
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def name
  @name
end

#player_statsObject

Returns the value of attribute player_stats.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def player_stats
  @player_stats
end

#recordsObject

Returns the value of attribute records.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def records
  @records
end

#remaining_timeoutsObject

Returns the value of attribute remaining_timeouts.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def remaining_timeouts
  @remaining_timeouts
end

#responseObject

Returns the value of attribute response.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def response
  @response
end

#seasonObject

Returns the value of attribute season.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def season
  @season
end

#seedObject

Returns the value of attribute seed.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def seed
  @seed
end

#statsObject

Returns the value of attribute stats.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def stats
  @stats
end

#team_statsObject

Returns the value of attribute team_stats.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def team_stats
  @team_stats
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def type
  @type
end

#used_timeoutsObject

Returns the value of attribute used_timeouts.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def used_timeouts
  @used_timeouts
end

#venueObject

Returns the value of attribute venue.



5
6
7
# File 'lib/sportradar/api/football/team.rb', line 5

def venue
  @venue
end

Instance Method Details

#add_game(game) ⇒ Object



73
74
75
# File 'lib/sportradar/api/football/team.rb', line 73

def add_game(game)
  @games_hash[game.id] = game.id if game
end

#gamesObject



70
71
72
# File 'lib/sportradar/api/football/team.rb', line 70

def games
  @games_hash.values
end

#get_rosterObject



93
94
95
96
# File 'lib/sportradar/api/football/team.rb', line 93

def get_roster
  data = api.get_data(path_roster).to_h
  ingest_roster(data)
end

#get_season_stats(year = default_year) ⇒ Object



107
108
109
110
# File 'lib/sportradar/api/football/team.rb', line 107

def get_season_stats(year = default_year)
  data = api.get_data(path_season_stats(year)).to_h
  ingest_season_stats(data)
end

#handle_names(data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/sportradar/api/football/team.rb', line 55

def handle_names(data)
  # need to do some more work here
  @name = data['name'] if data['name']
  if data['name'] && !data.key?('market')
    @full_name = data['name']
  elsif data['name'] && data['market']
    @market = data['market']
    @full_name = [@market, data['name']].join(' ')
  end
end

#ingest_roster(data) ⇒ Object



97
98
99
100
# File 'lib/sportradar/api/football/team.rb', line 97

def ingest_roster(data)
  update(data)
  data
end

#ingest_season_stats(data) ⇒ Object



111
112
113
# File 'lib/sportradar/api/football/team.rb', line 111

def ingest_season_stats(data)
  parse_season_stats(data)
end

#parse_players(data, game) ⇒ Object



86
87
88
# File 'lib/sportradar/api/football/team.rb', line 86

def parse_players(data, game)
  create_data(@players_hash, data, klass: player_class, api: api, team: self, game: game)
end

#parse_records(data) ⇒ Object



125
126
127
128
# File 'lib/sportradar/api/football/team.rb', line 125

def parse_records(data)
  @records['overall'] = Record.new(data, type: 'overall')
  data['records'].each { |record| @records[record['record_type']] = Record.new(record, type: record['record_type']) }
end

#parse_season_stats(data) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/sportradar/api/football/team.rb', line 130

def parse_season_stats(data)
  @team_stats = data.dig('statistics')
  update(data)
  player_data = data.dig('players')
  create_data(@players_hash, player_data, klass: player_class, api: api, team: self)
  data
end

#path_baseObject



138
139
140
# File 'lib/sportradar/api/football/team.rb', line 138

def path_base
  "teams/#{ id }"
end

#path_base_stats(year = season_year, season = default_season) ⇒ Object



141
142
143
# File 'lib/sportradar/api/football/team.rb', line 141

def path_base_stats(year = season_year, season = default_season)
  "seasons/#{year}/#{season}/teams/#{id}"
end

#path_rosterObject



144
145
146
# File 'lib/sportradar/api/football/team.rb', line 144

def path_roster
  "#{ path_base }/profile" # nfl is profile, ncaa is roster
end

#path_season_stats(year = season_year, season = default_season) ⇒ Object



147
148
149
# File 'lib/sportradar/api/football/team.rb', line 147

def path_season_stats(year = season_year, season = default_season)
  "#{ path_base_stats(year, season) }/statistics"
end

#playersObject Also known as: roster

def parse_stats(data)

@stats = data

end



81
82
83
84
# File 'lib/sportradar/api/football/team.rb', line 81

def players
  get_roster if @players_hash.empty?
  @players_hash.values
end

#queue_rosterObject



101
102
103
104
# File 'lib/sportradar/api/football/team.rb', line 101

def queue_roster
  url, headers, options, timeout = api.get_request_info(path_roster)
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_roster)}
end

#queue_season_stats(year = default_year) ⇒ Object

def ingest_season_stats(data)

update(data, source: :teams)
data

end



120
121
122
123
# File 'lib/sportradar/api/football/team.rb', line 120

def queue_season_stats(year = default_year)
  url, headers, options, timeout = api.get_request_info(path_season_stats(year))
  {url: url, headers: headers, params: options, timeout: timeout, callback: method(:ingest_season_stats)}
end

#record(type = 'overall') ⇒ Object



66
67
68
# File 'lib/sportradar/api/football/team.rb', line 66

def record(type = 'overall')
  @records[type]
end

#season_yearObject Also known as: year



152
153
154
# File 'lib/sportradar/api/football/team.rb', line 152

def season_year
  @season || default_year
end

#timeoutsObject



52
53
54
# File 'lib/sportradar/api/football/team.rb', line 52

def timeouts
  {'used' => used_timeouts, 'remaining' => remaining_timeouts}
end

#update(data, **opts) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sportradar/api/football/team.rb', line 27

def update(data, **opts)
  handle_names(data)
  @id = data['id'] || @id

  @venue    = Venue.new(data['venue']) if data['venue']

  @seed     = data['seed'].to_i             if data['seed']
  @alias    = data['alias']                 if data['alias']
  @points   = data['points'].to_i           if data['points'] && data['points'].kind_of?(Integer)
  @used_timeouts      = data['used_timeouts']       if data['used_timeouts']
  @remaining_timeouts = data['remaining_timeouts']  if data['remaining_timeouts']

  parse_records(data)                                          if data['records'] || data['overall']
  parse_players(data.dig('players'), opts[:game])   if data.dig('players')
  # parse_stats(data['statistics'])                             if data['statistics']
  if opts[:game]
    add_game(opts[:game])
    opts[:game].update_score(id => @points)             if @points
    if data['statistics']
      @stats = Sportradar::Api::Football::GameStats.new(data['statistics'])
      opts[:game].update_stats(self, @stats)
    end
  end
end

#update_player_stats(player, stats, game = nil) ⇒ Object



89
90
91
# File 'lib/sportradar/api/football/team.rb', line 89

def update_player_stats(player, stats, game = nil)
  game ? game.update_player_stats(player, stats) : @player_stats.merge!(player.id => stats.merge(player: player))
end