Drop header column and row when extract tables

This commit is contained in:
Urban Rotnik 2019-06-17 19:05:32 +02:00
parent 805f98374c
commit 7e69375016
3 changed files with 14 additions and 2 deletions

View file

@ -30,7 +30,7 @@ module ProtocolImporters
step = Step.new(step_attributes(s))
step.description = StepDescriptionBuilder.generate(s)
step.assets << AttachmentsBuilder.generate(s)
step.tables << TablesBuilder.extract_tables_from_html_string(s[:description][:body])
step.tables << TablesBuilder.extract_tables_from_html_string(s[:description][:body], true)
step
end
end

View file

@ -2,7 +2,7 @@
module ProtocolImporters
class TablesBuilder
def self.extract_tables_from_html_string(description_string)
def self.extract_tables_from_html_string(description_string, remove_first_column_row = false)
tables = []
doc = Nokogiri::HTML(description_string)
@ -17,7 +17,9 @@ module ProtocolImporters
row.css('td').each_with_index do |cell, j|
two_d_array[i][j] = cell.inner_html
end
two_d_array[i].shift if remove_first_column_row
end
two_d_array.shift if remove_first_column_row
tables << Table.new(contents: { data: two_d_array }.to_json)
end

View file

@ -5,6 +5,7 @@ require 'rails_helper'
RSpec.describe ProtocolImporters::TablesBuilder do
# rubocop:disable Metrics/LineLength
let(:description_string) { '<table><tr><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td><td>9</td><td>10</td></tr><tr><td>a</td><td>b</td><td>c</td><td>d</td><td>e</td><td>f</td><td>g</td><td>h</td><td>a</td><td>a</td></tr><tr><td>1</td><td>1</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>1</td><td>1</td><td>1</td></tr><tr><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td><td>1</td></tr><tr><td>asd</td><td>as</td><td>das</td><td>a</td><td>as</td><td>asd</td><td>sad</td><td>sa</td><td>asd</td><td>as124521</td></tr></table><table><tr><td>1</td><td>2</td><td>3</td></tr></table>' }
let(:description_string_with_headers) { '<table><tr><td></td><td>1</td><td>2</td><td>3</td></tr><tr><td>A</td><td>d1</td><td>d2</td><td>d3</td></tr><tr><td>B</td><td>c1</td><td>c2</td><td>c3</td></tr></table>' }
# rubocop:enable Metrics/LineLength
let(:extract_tables_from_string_result) { described_class.extract_tables_from_html_string(description_string) }
@ -30,5 +31,14 @@ RSpec.describe ProtocolImporters::TablesBuilder do
it 'returns table with 10 columns' do
expect(JSON.parse(first_table_in_result.contents)['data'].first.count).to be == 10
end
context 'when droping headers' do
it 'returns table with 2 rows and 3 columns' do
table = described_class.extract_tables_from_html_string(description_string_with_headers, true).first
expect(JSON.parse(table.contents)['data'].count).to be == 2
expect(JSON.parse(table.contents)['data'].first.count).to be == 3
end
end
end
end