Sha256: 0118e07f19005406eab160fc1979c94819829ea7a10575d873db1583318f714e

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

module SportsDataApi
  module Mlb
    class GameStats
      include Enumerable

      def initialize(xml)
        @stats = {}
        @stats[:hitting] = []
        @stats[:pitching] = []
        xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
        @stats[:status] = xml['status']
        xml.children.each do |child|
          next unless child.is_a? Nokogiri::XML::Element
          child.xpath("hitting").xpath("players").xpath("player").each do |player|
            @stats[:hitting] << GameStat.new(player)
          end

          child.xpath("pitching").xpath("players").xpath("player").each do |player|
            @stats[:pitching] << GameStat.new(player)
          end
        end
        @stats
      end

      def [](search_index)
        if @stats.has_key?(search_index)
          @stats[search_index]
        end
      end

      ##
      # Make the class Enumerable
      def each(&block)
        @stats.each do |stat|
          if block_given?
            block.call stat
          else
            yield stat
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
sports_data_api-0.10.1 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.10.0 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.9.2 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.2.4 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.2.3 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.2.2 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.2.1 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.2.0 lib/sports_data_api/mlb/game_stats.rb
sports_data_api-0.1.0 lib/sports_data_api/mlb/game_stats.rb