Class: Jsoner::TableFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/jsoner/table_factory.rb

Overview

build Hash below from doc parsed by Nokogiki

table = { :header => ["First Name", "Last Name", "Score"]
          :body   => [["Jill", "Smith", "50"],
                      ["Eve", "Jackson", "94"],
                      ["John", "Doe", "80"],
                      ["Adam", "Johnson", "67"]
                     ]
        }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(doc) ⇒ TableFactory

Returns a new instance of TableFactory.



13
14
15
# File 'lib/jsoner/table_factory.rb', line 13

def initialize doc
  @table_rows = remove_housing(doc).search('tr')
end

Class Method Details

.check(doc) ⇒ Object

check whether table is full a full table should include th, td, tr elements



35
36
37
38
39
40
41
42
# File 'lib/jsoner/table_factory.rb', line 35

def self.check doc
  table = doc.search('table').first
  unless table.search('tr').empty? || table.search('td').empty? || table.search('th').empty?
    TableFactory.new doc
  else
  raise Jsoner::NotFullTable 
  end
end

Instance Method Details

#build_bodyObject



25
26
27
28
29
30
# File 'lib/jsoner/table_factory.rb', line 25

def build_body
  row_number = @table_rows.count - 1
  (1..row_number).map do |row|
    @table_rows[row].search('td').map(&:content)
  end
end

#build_headerObject



21
22
23
# File 'lib/jsoner/table_factory.rb', line 21

def build_header
  @table_rows[0].search('th').map(&:content)
end

#createObject



17
18
19
# File 'lib/jsoner/table_factory.rb', line 17

def create
  { :header => build_header, :body => build_body }
end