class Kit::Table
Wraps the results table HTML. It iterates over each race-result pair, and exposes the table's column headings.
Public Class Methods
new(html)
click to toggle source
Creates a new Table
@param html [String] the raw HTML which contains a results table
# File lib/kit/table.rb, line 13 def initialize(html) @html = Nokogiri::HTML(html) @date_parser = DateParser.new end
Public Instance Methods
each(&block)
click to toggle source
Yields each race-result pair from the table
@yieldparam race_row [Nokogiri::XML::Element] the row which has the race
data
@yieldparam result_row [Nokogiri::XML::Element] the row which has the
result data
# File lib/kit/table.rb, line 24 def each(&block) race_rows.each { |race_row, result_row| block.call(race_row, result_row) } end
headings()
click to toggle source
Returns a lightly-sanitized list of column headings
@return [Array<String>]
# File lib/kit/table.rb, line 31 def headings headings_row.css('th').map { |th| th.text.scan(/\w|\s|#/).join } end
to_html()
click to toggle source
Returns an HTML document which only contains the table of race data
@return [String]
# File lib/kit/table.rb, line 38 def to_html if race_rows.any? content = headings_row.to_html + race_rows.flatten.map(&:to_html).join else content = nil end Nokogiri::HTML("<table>#{content}</table>").to_html end
Private Instance Methods
has_date?(tr)
click to toggle source
# File lib/kit/table.rb, line 66 def has_date?(tr) @date_parser.parse(tr) end
headings_row()
click to toggle source
# File lib/kit/table.rb, line 62 def headings_row @_headings_row ||= @html.css('th').first.parent end
race_rows()
click to toggle source
# File lib/kit/table.rb, line 50 def race_rows @_race_rows ||= @html.css('tr').select(&method(:has_date?)).map do |tr| sibling = tr.next_sibling until sibling.name == 'tr' sibling = sibling.next_sibling end [tr, sibling] end end