-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
37 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class CsvImporter | ||
# load CSV allowing UTF-8 to pass through, deleting blank columns | ||
# @param [String] filename | ||
# @return [Array<ActiveSupport::HashWithIndifferentAccess>] | ||
# @raise if file missing/unreadable | ||
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) | ||
csv = CSV.parse(file_contents, :headers => true) | ||
csv.map { |row| row.to_hash.with_indifferent_access } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
RSpec.describe CsvImporter do | ||
describe '#parse_to_hash' do | ||
let(:manifest) do | ||
described_class.parse_to_hash("#{Rails.root}/spec/test_data/bundle_input_a/manifest.csv") | ||
end | ||
|
||
it "loads a CSV as a hash with indifferent access" do | ||
expect(manifest).to be_an(Array) | ||
expect(manifest.size).to eq(3) | ||
headers = %w{format sourceid filename label year inst_notes prod_notes has_more_metadata description} | ||
expect(manifest).to all(be_an(ActiveSupport::HashWithIndifferentAccess)) # accessible w/ string and symbols | ||
expect(manifest).to all(include(*headers)) | ||
expect(manifest[0][:description]).to be_nil | ||
expect(manifest[1][:description]).to eq('') | ||
expect(manifest[2][:description]).to eq('yo, this is a description') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters