Class: DynportTools::AsciiTable

Inherits:
Object
  • Object
show all
Defined in:
lib/dynport_tools/ascii_table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ AsciiTable

Returns a new instance of AsciiTable.



7
8
9
10
# File 'lib/dynport_tools/ascii_table.rb', line 7

def initialize(attributes = {})
  self.headers = attributes[:headers] || []
  self.rows = attributes[:rows] || []
end

Instance Attribute Details

#headersObject

Returns the value of attribute headers.



4
5
6
# File 'lib/dynport_tools/ascii_table.rb', line 4

def headers
  @headers
end

#rowsObject

Returns the value of attribute rows.



5
6
7
# File 'lib/dynport_tools/ascii_table.rb', line 5

def rows
  @rows
end

Instance Method Details

#html2ascii(html) ⇒ Object



36
37
38
39
40
41
42
43
# 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") ⇒ Object



23
24
25
26
# 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) ⇒ Object



32
33
34
# File 'lib/dynport_tools/ascii_table.rb', line 32

def to(format)
  send(:"to_#{format}")
end

#to_asciiObject



28
29
30
# File 'lib/dynport_tools/ascii_table.rb', line 28

def to_ascii
  html2ascii(to_html)
end

#to_htmlObject



16
17
18
19
20
21
# 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_tsvObject



12
13
14
# File 'lib/dynport_tools/ascii_table.rb', line 12

def to_tsv
  ([headers] + rows).map { |line| line.join("\t") }.join("\n")
end