lib/sportradar/api/nfl/depth_chart.rb in sportradar-api-0.1.32 vs lib/sportradar/api/nfl/depth_chart.rb in sportradar-api-0.1.33

- old
+ new

@@ -1,14 +1,34 @@ module Sportradar module Api class Nfl::DepthChart < Data - attr_accessor :response, :season, :teams + attr_accessor :response, :chart def initialize(data) - @response = data - @season = Sportradar::Api::Nfl::Season.new data["season"] if data["season"] - @teams = data["season"]["depth_charts"]["team"].map {|team| Sportradar::Api::Nfl::Team.new team } if data["season"]["depth_charts"]["team"] + generate_chart(data) + end + def team(number) + teams[number] + end + + private + + def teams + @teams ||= Hash.new { |hash, number| hash[number] = generate_team(number) } + end + + def generate_team(number) + @chart.each_with_object({}) do |(pos_name, groups), memo| + memo[pos_name] = groups[number.to_s] + end + end + + def generate_chart(data) + @chart = data['position'].each_with_object({}) do |position, memo| + players = position['player'].map { |h| Sportradar::Api::Nfl::Player.new(h) } + memo[position['name']] = players.group_by(&:depth) + end end end end end