Created
May 7, 2014 19:50
-
-
Save dogweather/1479664d12d0c7c27492 to your computer and use it in GitHub Desktop.
A first pass at a CSV API which provides header-based access plus error handling
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
describe BetterCsv do | |
it 'has a nice API' do | |
parser = BetterCsv.parse('info.csv', headers: :first_row) | |
parser.each_row do |parse_result| | |
next if parse_result.error? | |
puts "Found the name and phone #: #{parse_result.name}, #{parse_result.telephone}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This could have a second way to check for errors, throwing an exception instead of returning a boolean. Similar to ActiveRecord#save vs. ActiveRecord#save!
E.g., instead of using
parse_result.error?
the user of the library could choose to callparse_result.validate!
which would throw the CSV parsing error, else take no action.