Skip to content

Commit

Permalink
Merge pull request #462 from chopraanmol1/restore_support_or_url_for_csv
Browse files Browse the repository at this point in the history
Restore support of URL for CSV
  • Loading branch information
chopraanmol1 authored Oct 7, 2018
2 parents 8d5ea7a + e29ffe7 commit 4053242
Show file tree
Hide file tree
Showing 2 changed files with 48 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
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

0 comments on commit 4053242

Please sign in to comment.