Class: TableToHashConverter
- Inherits:
-
Object
- Object
- TableToHashConverter
- Defined in:
- lib/rubyslim/table_to_hash_converter.rb
Class Method Summary collapse
- .add_row_to_hash(hash, row) ⇒ Object
- .convert(string) ⇒ Object
- .create_hash_from_rows(rows) ⇒ Object
- .html_to_hash(doc) ⇒ Object
Class Method Details
.add_row_to_hash(hash, row) ⇒ Object
27 28 29 30 31 |
# File 'lib/rubyslim/table_to_hash_converter.rb', line 27 def self.add_row_to_hash(hash, row) columns = row.get_elements('td') raise ArgumentError if columns.size != 2 hash[columns[0].text.strip.to_sym] = columns[1].text.strip end |
.convert(string) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/rubyslim/table_to_hash_converter.rb', line 4 def self.convert(string) begin doc = REXML::Document.new(string.strip) return html_to_hash(doc) rescue return string end end |
.create_hash_from_rows(rows) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/rubyslim/table_to_hash_converter.rb', line 19 def self.create_hash_from_rows(rows) hash = {} rows.elements.each('tr') do |row| add_row_to_hash(hash, row) end hash end |
.html_to_hash(doc) ⇒ Object
13 14 15 16 17 |
# File 'lib/rubyslim/table_to_hash_converter.rb', line 13 def self.html_to_hash(doc) table = doc.elements['table'] raise ArgumentError if table.nil? create_hash_from_rows(table) end |