Skip to content

Commit

Permalink
Restore support of URL for CSV
Browse files Browse the repository at this point in the history
### Summary

Fixes roo-rb#378 and roo-rb#374 (Introduced in roo-rb#368)

### Benchmark

```
file_name = 'test/files/Bibelbund.csv'

MemoryProfiler.report{ Roo::Spreadsheet.open(file_name).tap{|x|(2..x.last_row).each{|i| x.row(i)}} }
puts Benchmark.measure{ Roo::Spreadsheet.open(file_name).tap{|x|(2..x.last_row).each{|i| x.row(i)}} }
```

Master
```
Total allocated: 39705265 bytes (561479 objects)
Total retained:  768 bytes (4 objects)

  0.300000   0.000000   0.300000 (  0.304877)
```

Modified:
```
Total allocated: 16952085 bytes (234487 objects)
Total retained:  768 bytes (4 objects)

  0.190000   0.000000   0.190000 (  0.181199)
```
  • Loading branch information
chopraanmol1 committed Oct 5, 2018
1 parent 6eb877e commit 921dfad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
32 changes: 20 additions & 12 deletions lib/roo/csv.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "csv"
require "time"

Expand Down Expand Up @@ -63,50 +65,56 @@ def celltype_class(value)
def read_cells(sheet = default_sheet)
sheet ||= default_sheet
return if @cells_read[sheet]
set_row_count(sheet)
set_column_count(sheet)
row_num = 1
row_num = 0
max_col_num = 0

each_row csv_options do |row|
row.each_with_index do |elem, col_num|
coordinate = [row_num, col_num + 1]
row_num += 1
col_num = 0

row.each do |elem|
col_num += 1
coordinate = [row_num, col_num]
@cell[coordinate] = elem
@cell_type[coordinate] = celltype_class(elem)
end
row_num += 1

max_col_num = col_num if col_num > max_col_num
end

set_row_count(sheet, row_num)
set_column_count(sheet, max_col_num)
@cells_read[sheet] = true
end

def each_row(options, &block)
if uri?(filename)
each_row_using_temp_dir(filename)
each_row_using_tempdir(options, &block)
elsif is_stream?(filename_or_stream)
::CSV.new(filename_or_stream, options).each(&block)
else
::CSV.foreach(filename, options, &block)
end
end

def each_row_using_tempdir
def each_row_using_tempdir(options, &block)
::Dir.mktmpdir(Roo::TEMP_PREFIX, ENV["ROO_TMP"]) do |tmpdir|
tmp_filename = download_uri(filename, tmpdir)
::CSV.foreach(tmp_filename, options, &block)
end
end

def set_row_count(sheet)
def set_row_count(sheet, last_row)
@first_row[sheet] = 1
@last_row[sheet] = ::CSV.readlines(@filename, csv_options).size
@last_row[sheet] = last_row
@last_row[sheet] = @first_row[sheet] if @last_row[sheet].zero?

nil
end

def set_column_count(sheet)
def set_column_count(sheet, last_col)
@first_column[sheet] = 1
@last_column[sheet] = (::CSV.readlines(@filename, csv_options).max_by(&:length) || []).size
@last_column[sheet] = last_col
@last_column[sheet] = @first_column[sheet] if @last_column[sheet].zero?

nil
Expand Down
16 changes: 16 additions & 0 deletions test/roo/test_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ def test_sheets
end
end

def test_download_uri_with_query_string
file = filename("Bibelbund")
port = 12_344
url = "#{local_server(port)}/#{file}?query-param=value"

start_local_server(file, port) do
csv = roo_class.new(url)
assert_equal "Aktuelle Seite", csv.cell("h", 12)
assert_equal 1, csv.first_row
assert_equal 3735, csv.last_row
assert_equal 1, csv.first_column
assert_equal 8, csv.last_column
end
end


def test_nil_rows_and_lines_csv
# x_123
oo = Roo::CSV.new(File.join(TESTDIR,'Bibelbund.csv'))
Expand Down

0 comments on commit 921dfad

Please sign in to comment.