lib/remote_table/delimited.rb in remote_table-3.0.0.rc1 vs lib/remote_table/delimited.rb in remote_table-3.0.0.rc2
- old
+ new
@@ -21,11 +21,11 @@
skip_rows!
end
# Yield each row using Ruby's CSV parser (FasterCSV on Ruby 1.8).
def _each
- Engine.new(local_copy.encoded_io, csv_options).each do |row|
+ Engine.new(local_copy.encoded_io, csv_options.merge(headers: headers)).each do |row|
some_value_present = false
if not headers
@@ -63,11 +63,10 @@
end
def csv_options
{
skip_blanks: !keep_blank_rows,
- headers: headers,
col_sep: delimiter,
quote_char: quote_char,
}
end
@@ -76,13 +75,20 @@
@_headers = case @headers
when FalseClass, NilClass
false
when :first_row, TrueClass
i = 0
- line = local_copy.encoded_io.gets
- Engine.parse_line(line).map do |v|
- header = RemoteTable.normalize_whitespace v
- header.present? ? header : "empty_#{i+=1}"
+ begin
+ line = local_copy.encoded_io.gets.strip
+ end while line.length == 0
+ proto_headers = Engine.parse_line(line, csv_options)
+ if proto_headers
+ proto_headers.map do |v|
+ header = RemoteTable.normalize_whitespace v
+ header.present? ? header : "empty_#{i+=1}"
+ end
+ else
+ raise "No headers found in first line: #{line.inspect}"
end
when Array
@headers
else
raise "Invalid headers: #{headers.inspect}"