class DynportTools::AsciiTable

Attributes

headers[RW]
rows[RW]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 7
def initialize(attributes = {})
  self.headers = attributes[:headers] || []
  self.rows = attributes[:rows] || []
end

Public Instance Methods

html2ascii(html) click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 36
def html2ascii(html)
  tempfile = Tempfile.new("html2ascii")
  tempfile.print(html)
  tempfile.close
  ascii = Kernel.send(:`, "links -dump #{tempfile.path}")
  tempfile.delete
  ascii
end
html_table_cell(text_or_array, node = "td") click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 23
def html_table_cell(text_or_array, node = "td")
  text, options = text_or_array
  "<#{node}#{options ? options.map { |key, value| " #{key}=#{value}" }.join("") : ""}>#{text}"
end
to(format) click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 32
def to(format)
  send(:"to_#{format}")
end
to_ascii() click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 28
def to_ascii
  html2ascii(to_html)
end
to_html() click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 16
def to_html
  html = "<table border=1 align=center>"
  html << "<tr>" + headers.map { |header| html_table_cell(header, "th") }.join("") if headers.any?
  html << rows.map { |row| "<tr>" + row.map { |r| html_table_cell(r, "td") }.join("") }.join("")
  html + "</table>"
end
to_tsv() click to toggle source
# File lib/dynport_tools/ascii_table.rb, line 12
def to_tsv
  ([headers] + rows).map { |line| line.join("\t") }.join("\n")
end