Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore support of URL for CSV #462

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
28 changes: 28 additions & 0 deletions test/roo/test_csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,34 @@ 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_open_stream
file = filename("Bibelbund")
file_contents = File.read File.join(TESTDIR, file)
stream = StringIO.new(file_contents)
csv = roo_class.new(stream)

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

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