Browse Source

change handle line endings mechanism

- we now strip all windows line endings for unix based line endings
- this normalises things making it simpler
pull/1208/head
Phil Lee 3 years ago
parent
commit
dd7ddce3db
  1. 22
      app/services/bulk_upload/lettings/csv_parser.rb

22
app/services/bulk_upload/lettings/csv_parser.rb

@ -24,7 +24,6 @@ class BulkUpload::Lettings::CsvParser
stripped_row = row[1..] stripped_row = row[1..]
headers = ("field_1".."field_134").to_a headers = ("field_1".."field_134").to_a
hash = Hash[headers.zip(stripped_row)] hash = Hash[headers.zip(stripped_row)]
# hash[:bulk_upload] = bulk_upload
BulkUpload::Lettings::RowParser.new(hash) BulkUpload::Lettings::RowParser.new(hash)
end end
@ -35,24 +34,21 @@ class BulkUpload::Lettings::CsvParser
end end
def rows def rows
@rows ||= CSV.read(path, row_sep:) @rows ||= CSV.parse(normalised_string, row_sep:)
end end
private private
# determine the row seperator from CSV
# Windows will use \r\n
def row_sep def row_sep
contents = "" "\n"
end
File.open(path, "r") do |f| def normalised_string
contents = f.read return @normalised_string if @normalised_string
end
if contents[-2..] == "\r\n" @normalised_string = File.read(path)
"\r\n" @normalised_string.gsub!("\r\n", "\n")
else
"\n" @normalised_string
end
end end
end end

Loading…
Cancel
Save