Class: SportsDataApi::Mlb::GameStats
- Inherits:
-
Object
- Object
- SportsDataApi::Mlb::GameStats
- Includes:
- Enumerable
- Defined in:
- lib/sports_data_api/mlb/game_stats.rb
Instance Method Summary collapse
- #[](search_index) ⇒ Object
-
#each(&block) ⇒ Object
Make the class Enumerable.
-
#initialize(xml) ⇒ GameStats
constructor
A new instance of GameStats.
Constructor Details
#initialize(xml) ⇒ GameStats
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/sports_data_api/mlb/game_stats.rb', line 6 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 |
Instance Method Details
#[](search_index) ⇒ Object
25 26 27 28 29 |
# File 'lib/sports_data_api/mlb/game_stats.rb', line 25 def [](search_index) if @stats.has_key?(search_index) @stats[search_index] end end |
#each(&block) ⇒ Object
Make the class Enumerable
33 34 35 36 37 38 39 40 41 |
# File 'lib/sports_data_api/mlb/game_stats.rb', line 33 def each(&block) @stats.each do |stat| if block_given? block.call stat else yield stat end end end |