Sha256: 5cbdd950d8deab897c405281b7bb090b7cf9d97b712f2ff09fe8407bb8083c14

Contents?: true

Size: 1018 Bytes

Versions: 4

Compression:

Stored size: 1018 Bytes

Contents

module Sportradar
  module Api
    class Nfl::DepthChart < Data
      include Enumerable
      attr_accessor :response, :chart

      def initialize(data)
        generate_chart(data)
      end

      def team(number)
        teams[number]
      end

      def each
        populate_teams
        teams.each { |team| yield team }
      end

      private

      def teams
        @teams ||= Hash.new { |hash, number| hash[number] = generate_team(number) }
      end

      def populate_teams
        (1..3).each(&method(:team)) if teams.empty?
      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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
sportradar-api-0.9.3 lib/sportradar/api/nfl/depth_chart.rb
sportradar-api-0.9.2 lib/sportradar/api/nfl/depth_chart.rb
sportradar-api-0.9.1 lib/sportradar/api/nfl/depth_chart.rb
sportradar-api-0.9.0 lib/sportradar/api/nfl/depth_chart.rb