class TableCSV

Constants

VERSION

Attributes

content[R]

Public Class Methods

new() click to toggle source
# File lib/TableCSV.rb, line 7
def initialize
  @content = ''
end

Public Instance Methods

attach(html) click to toggle source
# File lib/TableCSV.rb, line 11
def attach(html)
  @content = "#{@content}#{build_csv_array(html)}"
  self
end

Private Instance Methods

build_csv_array(html) click to toggle source
# File lib/TableCSV.rb, line 17
def build_csv_array(html)
  require 'nokogiri'

  table = Nokogiri::HTML(html).css('table')
  capture_table(table)
end
capture_table(table) click to toggle source
# File lib/TableCSV.rb, line 24
def capture_table(table)
  rows = @content.length == 0 ? header_row(table.first) : []
  table.css('tr').drop(1).each {|r| rows << csv_row(r.css('td')) }
  rows
end
csv_row(cells) click to toggle source
# File lib/TableCSV.rb, line 41
def csv_row(cells)
  row = ''
  cells.each {|cell| row = "#{row}#{cell.text},"}
  "#{row[0..-2]}\n"
end
header_cells(row) click to toggle source
# File lib/TableCSV.rb, line 34
def header_cells(row)
  if (cells = row.css('th')).length == 0
    cells = row.css('td')
  end
  cells
end
header_row(row) click to toggle source
# File lib/TableCSV.rb, line 30
def header_row(row)
  csv_row(header_cells(row))
end