Sha256: 2383b9e48556ede847437f9228b92706f9a66652a9ba870d253afaf6bd9249b7

Contents?: true

Size: 1.95 KB

Versions: 3

Compression:

Stored size: 1.95 KB

Contents

module SodaXmlTeam

  require 'nokogiri'

  class Standings

    def self.parse_standings(document={})

      output = []

      unless document.is_a? Nokogiri::XML::Document
        raise "Invalid XML standings."
      end

      document.css('sports-content standing').each do |division|

        row = {}
        row[:division] = division['content-label']

        division.css('standing-metadata sports-content-codes sports-content-code[code-type="conference"]').each do |standingmetadata|
          row[:conference] = standingmetadata['code-name']
        end

        row[:teams] = []

        division.css('team').each do |team|

          team_record = {}

          team.css('team-metadata name').each do |teammetaname|
            team_record[:name] = "#{teammetaname[:first]} #{teammetaname[:last]}"
          end

          team.css('team-stats').each do |teamstats|
            next if teamstats['standing-points'].nil?
            team_record[:standing_points] = teamstats['standing-points'].to_i
          end

          team.css('team-stats rank').each do |teamstatsrank|
            team_record[:division_rank] = teamstatsrank[:value].to_i
            team_record[:conference_rank] = teamstatsrank['xts:conference-rank'].to_i
          end

          team.css('team-stats outcome-totals').each do |outcometotals|
            next if outcometotals['competition-scope'] != "league"
            next if outcometotals['date-coverage-type'] != "season-regular"
            next if !outcometotals['duration-scope'].nil?
            next if !outcometotals['alignment-scope'].nil?
            outcometotals.keys.each do |outcometotalskey|
              team_record[outcometotalskey] = outcometotals[outcometotalskey].to_i if Float(outcometotals[outcometotalskey]) rescue team_record[outcometotalskey] = outcometotals[outcometotalskey]
            end
          end

          row[:teams] << team_record

        end

        output << row
      end

      return output

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
soda_xml_team-1.3.1 lib/soda_xml_team/standings.rb
soda_xml_team-1.3.0 lib/soda_xml_team/standings.rb
soda_xml_team-1.2.0 lib/soda_xml_team/standings.rb