Class: SportsDataApi::Mlb::Boxscore

Inherits:
Object
  • Object
show all
Defined in:
lib/sports_data_api/mlb/boxscore.rb

Constant Summary collapse

VALID_GAME_STATUSES =
['closed', 'inprogress']

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Boxscore

Returns a new instance of Boxscore.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sports_data_api/mlb/boxscore.rb', line 6

def initialize(xml)
  self.class.class_eval { attr_reader :"game_state" }

  xml = xml.first if xml.is_a? Nokogiri::XML::NodeSet
  return unless VALID_GAME_STATUSES.include? xml['status'].to_s.downcase

  boxscore_ivar = self.instance_variable_set("@game_state", {})
  visitor = xml.xpath("visitor").first
  boxscore_ivar[:visitor_score] = visitor.attributes["runs"].value

  home = xml.xpath("home").first
  boxscore_ivar[:home_score] = home.attributes["runs"].value
  boxscore_ivar[:status] = xml['status'].to_s.downcase
  if xml['status'] == 'closed'
    inning = xml.xpath('final').first
    if inning
      inning.attributes.each do | attr_name, attr_value|
        boxscore_ivar[attr_name.to_sym] = attr_value.value
      end
    end
  elsif xml['status'] == 'inprogress'
    inning = xml.xpath('outcome').first
    if inning
      boxscore_ivar[:inning] = inning.attributes['current_inning'].value
      boxscore_ivar[:inning_half] = inning.attributes['current_inning_half'].value
    end
  end
end