Users can upload a CSV file to my service. The CSV needs to have two column names and nothing else. One column needs to be "email" and the other "credit" and nothing else can be in those column names, no spaces, characters, returns, nothing.
Right now I'm getting the header with this:
first_row = File.open(csv_file.tempfile, &:readline)
first_row = first_row.downcase
So the first row is downcased and then checking for the existence of "email" or "credit" with this:
first_row.include?("email") && first_row.include?("credit")
Works great, until I realized it would pass the test even if the header contained other characters or spaces.. like this email and pretty much anything else
I was thinking of counting the length or splitting the string, but nothing I could come up with that would consistently determine if the first_row
contained ONLY the words I need.
What do you all think?
This will work.
first_row = File.open(csv_file.tempfile, &:readline)
first_row = first_row.downcase.strip
header = first_row.split(',')
if header.sort == ["credit", "email"]
# The header is valid
else
# The header is invalid
end