Skip to content

Commit

Permalink
Merge fb75f8a into fe7d825
Browse files Browse the repository at this point in the history
  • Loading branch information
atz committed Oct 9, 2018
2 parents fe7d825 + fb75f8a commit 33a4cf6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 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 == 65_279 # purge Byte-order-mark
csv = CSV.parse(file_contents, headers: true)
csv.map { |row| row.to_hash.with_indifferent_access }
end
Expand Down
13 changes: 13 additions & 0 deletions spec/lib/csv_importer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,18 @@
expect(manifest).to all(include(*headers))
expect(manifest.pluck(:description)).to eq([nil, '', 'yo, this is a description'])
end
context 'windows manifest.csv' do
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]
expect(manifest).to all(be_an(ActiveSupport::HashWithIndifferentAccess)) # accessible w/ string and symbols
expect(manifest).to all(include(*headers))
end
end
end
end
8 changes: 8 additions & 0 deletions spec/test_data/windows_manifest/manifest.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
druid,object
mw438sy2326,mw438sy2326
sv928qy8859,sv928qy8859
jh486mk1405,jh486mk1405
vb063xr4527,vb063xr4527
ww805gw8199,ww805gw8199
yg789dz9935,yg789dz9935
vz805mb3344,vz805mb3344

0 comments on commit 33a4cf6

Please sign in to comment.