Skip to content

Commit

Permalink
Coerce Win32 line endings and strip BOM to support windows CSVs
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Oct 8, 2018
1 parent 2ddd0c6 commit 2678319
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 2 additions & 0 deletions app/lib/csv_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ def self.parse_to_hash(filename)
raise ArgumentError, 'CSV filename required' unless filename.present?
raise ArgumentError, "Required file not found: #{filename}." unless File.readable?(filename)
file_contents = IO.read(filename).encode('utf-8', replace: nil)
file_contents.gsub(/\r\n?/, "\n") # coerce Windows line-endings
file_contents = file_contents[1..-1] if file_contents[0].ord == 65279 # purge Byte-order-mark
csv = CSV.parse(file_contents, headers: true)
csv.map { |row| row.to_hash.with_indifferent_access }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
let(:manifest) do
described_class.parse_to_hash("#{Rails.root}/spec/test_data/windows_manifest/manifest.csv")
end

it 'loads a CSV as a hash and provides values' do
expect(manifest.size).to eq(7)
expect(manifest).to be_an(Array)
headers = %w[object druid]
headers = %w[druid object]
expect(manifest).to all(be_an(ActiveSupport::HashWithIndifferentAccess)) # accessible w/ string and symbols
expect(manifest).to all(include(*headers))
end
Expand Down

0 comments on commit 2678319

Please sign in to comment.