Class: Kit::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/kit/table.rb

Overview

Wraps the results table HTML. It iterates over each race-result pair, and exposes the table’s column headings.

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ Table

Creates a new Table

Parameters:

  • html (String)

    the raw HTML which contains a results table



13
14
15
16
# File 'lib/kit/table.rb', line 13

def initialize(html)
  @html = Nokogiri::HTML(html)
  @date_parser = DateParser.new
end

Instance Method Details

#each {|race_row, result_row| ... } ⇒ Object

Yields each race-result pair from the table

Yield Parameters:

  • race_row (Nokogiri::XML::Element)

    the row which has the race data

  • result_row (Nokogiri::XML::Element)

    the row which has the result data



24
25
26
# File 'lib/kit/table.rb', line 24

def each(&block)
  race_rows.each { |race_row, result_row| block.call(race_row, result_row) }
end

#headingsArray<String>

Returns a lightly-sanitized list of column headings

Returns:

  • (Array<String>)


31
32
33
# File 'lib/kit/table.rb', line 31

def headings
  headings_row.css('th').map { |th| th.text.scan(/\w|\s|#/).join }
end

#to_htmlString

Returns an HTML document which only contains the table of race data

Returns:

  • (String)


38
39
40
41
42
43
44
45
46
# 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