Skip to content

Commit

Permalink
Merge pull request #18 from vijendra/master
Browse files Browse the repository at this point in the history
Please make a pull.
  • Loading branch information
schorsch committed Jan 31, 2012
2 parents beaf3a7 + c518bcf commit fe55376
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 5 additions & 7 deletions app/models/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,13 @@ def rows(size = 0)
# When parsing data, we expect our file to be saved as valid utf-8
def parsed_data
@parsed_data ||= begin
CSV.read(full_filename, col_sep: col_sep, quote_char: quote_char, row_sep: "rn", encoding: "ISO-8859-1:UTF-8")
rescue CSV::MalformedCSVError
CSV.read(full_filename, col_sep: col_sep, quote_char: quote_char)
rescue CSV::MalformedCSVError => er
rows = []
#one more attempt. #TODO check if this is required.
#one more attempt. If BOM is present in the file.
begin
f = File.open(full_filename, "r:ISO-8859-1")
CSV.parse(f.read.encode("UTF-8") , col_sep: col_sep, quote_char: quote_char) do |row|
rows << row
end
f = File.open(full_filename, "rb:bom|utf-8")
rows = CSV.parse(f.read.force_encoding("ISO-8859-1"))
ensure
return rows
end
Expand Down
13 changes: 12 additions & 1 deletion spec/models/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@
it "reveals specified number of rows" do
@attachment.rows(1).size.should == 1
end
end

describe "different csv formats" do
{'google_native_test_.csv' => 3, 'google_outlook_test.csv' => 3, 'test1.csv' => 2}.each do |csv_file, count|
it "should able to read #{csv_file}" do
attachment = Factory(:attachment, :uploaded_data => file_upload(csv_file))
attachment.rows.first.size.should be > 1
attachment.rows.size.should == count
end
end
end

end

0 comments on commit fe55376

Please sign in to comment.