class SportsDataApi::Nfl::Team

Constants

EXTRA_POINTS_KEY
PLAYER_KEYS

Attributes

alias[R]
id[R]
json[R]
market[R]
name[R]
remaining_challenges[R]
remaining_timeouts[R]
statistics[R]

Public Class Methods

new(json, conference: nil, division: nil, statistics: nil) click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 7
def initialize(json, conference: nil, division: nil, statistics: nil)
  @json = json
  @conference = conference
  @division = division
  @statistics = statistics

  @id = json['id']
  @name = json['name']
  @alias = json['alias']
  @market = json['market']
  @remaining_challenges = json['remaining_challenges']
  @remaining_timeouts = json['remaining_timeouts']
end

Public Instance Methods

conference() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 21
def conference
  @conference || json.dig('conference', 'name')
end
division() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 25
def division
  @division || json.dig('division', 'name')
end
players() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 38
def players
  players_json.map { |p| Player.new(p) }
end
points() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 34
def points
  json['points']
end
venue() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 29
def venue
  return unless json['venue']
  Venue.new(json['venue'])
end

Private Instance Methods

add_stat(players, player_json, key) click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 75
def add_stat(players, player_json, key)
  base, stats = split_player_json(player_json)
  player = players[player_json['id']] || base
  player['statistics'][key] = stats
  players[player['id']] = player
end
players_from_stats() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 57
def players_from_stats
  statistics.each_with_object({}) do |(key, data), players|
    next unless data.is_a?(Hash)
    if key == EXTRA_POINTS_KEY
      data.each do |nested_key, nested_data|
        nested_data.fetch('players', []).each do |player_json|
          category = player_json.delete('category') || nested_key
          add_stat(players, player_json, "#{key}_#{category}")
        end
      end
    else
      data.fetch('players', []).each do |player_json|
        add_stat(players, player_json, key)
      end
    end
  end
end
players_json() click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 49
def players_json
  if statistics
    players_from_stats.values
  else
    json.fetch('players', [])
  end
end
split_player_json(player_json) click to toggle source
# File lib/sports_data_api/nfl/team.rb, line 82
def split_player_json(player_json)
  player = { 'statistics' => {} }
  stats = {}
  player_json.each do |k, v|
    hash = PLAYER_KEYS.include?(k) ? player : stats
    hash[k] = v
  end
  [player, stats]
end