Class: Sportradar::Api::Football::PlayStatistics

Inherits:
Data
  • Object
show all
Defined in:
lib/sportradar/api/football/play_statistics.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) ⇒ PlayStatistics

Returns a new instance of PlayStatistics.



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

def initialize(data)
  data = [data] if data.is_a?(Hash)
  @response = data
  if data.first['name'] # indicates college data structures. we want to convert it to nfl data structures
    data = data.flat_map do |hash|
      types = self.class.college_type_translations.each_key.select { |str| hash.key?(str) }
      types.map do |type|
        stats = hash.delete(type)
        new_team = { 'id' => hash['team'], 'alias' => hash['team'] } # use intermediate variable to avoid temp memory blowup
        { 'player' => hash, 'team' => new_team, 'stat_type' => self.class.college_type_translations[type] }.merge(stats)
      end
    end
  end
  data.each do |hash|
    var = instance_variable_get("@#{hash['stat_type']}")
    unless var
      instance_variable_set("@#{hash['stat_type']}", [])
      var = instance_variable_get("@#{hash['stat_type']}")
    end
    klass = self.class.stat_type_classes[hash['stat_type']] || MiscStatistics
    var << klass.new(hash)
  end
end

Instance Attribute Details

#defenseObject

Returns the value of attribute defense.



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

def defense
  @defense
end

#down_conversionObject

Returns the value of attribute down_conversion.



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

def down_conversion
  @down_conversion
end

#extra_pointObject

Returns the value of attribute extra_point.



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

def extra_point
  @extra_point
end

#field_goalObject

Returns the value of attribute field_goal.



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

def field_goal
  @field_goal
end

#first_downObject

Returns the value of attribute first_down.



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

def first_down
  @first_down
end

#fumbleObject

Returns the value of attribute fumble.



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

def fumble
  @fumble
end

#kickObject

Returns the value of attribute kick.



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

def kick
  @kick
end

#passObject

Returns the value of attribute pass.



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

def pass
  @pass
end

#penaltyObject

Returns the value of attribute penalty.



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

def penalty
  @penalty
end

#puntObject

Returns the value of attribute punt.



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

def punt
  @punt
end

#receiveObject

Returns the value of attribute receive.



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

def receive
  @receive
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#returnObject

Returns the value of attribute return.



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

def return
  @return
end

#rushObject

Returns the value of attribute rush.



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

def rush
  @rush
end

Class Method Details

.college_type_translationsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/sportradar/api/football/play_statistics.rb', line 40

def self.college_type_translations
  @college_type_translations ||= {
    "kickoffs"                  => 'kick',
    "kick_return"               => 'return',
    "rushing"                   => 'rush',
    "fumble"                    => 'fumble',
    "defense"                   => 'defense',
    "receiving"                 => 'receive',
    "punting"                   => 'punt',
    "penalty"                   => 'penalty',
    "passing"                   => 'pass',
    "field_goal"                => 'field_goal',
    "extra_point"               => 'extra_point',
    "blocked_field_goal_return" => 'block',
    "interception_return"       => 'return',
    "fumble_return"             => 'return',
    "punt_return"               => 'return',
    "two_point_conversion"      => 'conversion',
    'misc'                      => 'misc',
  }.freeze
end

.stat_type_classesObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/sportradar/api/football/play_statistics.rb', line 62

def self.stat_type_classes
  @stat_type_classes ||= {
    'kick'            => PlayKickStatistics,
    'return'          => PlayReturnStatistics,
    'rush'            => PlayRushStatistics,
    'fumble'          => PlayFumbleStatistics,
    'defense'         => PlayDefenseStatistics,
    'receive'         => PlayReceiveStatistics,
    'punt'            => PlayPuntStatistics,
    'penalty'         => PlayPenaltyStatistics,
    'pass'            => PlayPassingStatistics,
    'first_down'      => PlayFirstDownStatistics,
    'field_goal'      => PlayFieldGoalStatistics,
    'extra_point'     => PlayExtraPointStatistics,
    'down_conversion' => PlayDownConversionStatistics,
    'conversion'      => ConversionStatistics,
    'block'           => BlockStatistics,
    'misc'            => MiscStatistics,
  }.freeze
end

Instance Method Details

#playersObject



30
31
32
33
34
# File 'lib/sportradar/api/football/play_statistics.rb', line 30

def players
  @players ||= [:kick, :return, :rush, :defense, :receive, :punt, :penalty, :pass, :field_goal, :defense, :down_conversion].flat_map do |stat_type|
    Array(send(stat_type)).map { |stat| (stat.player.team = stat.team.id; stat.player) if stat.player }.compact # this is cause of kick returns w/ touchbacks
  end
end

#players_by_teamObject



36
37
38
# File 'lib/sportradar/api/football/play_statistics.rb', line 36

def players_by_team
  @players_by_team ||= players.group_by(&:team)
end