I'm trying to parse the first 5 lines of a remote CSV file. However, when I do, it raises Errno::ENOENT
exception, and says:
No such file or directory - [file contents]
(with [file contents] being a dump of the CSV contents
Here's my code:
def preview
@csv = []
open('http://example.com/spreadsheet.csv') do |file|
CSV.foreach(file.read, :headers => true) do |row|
n += 1
@csv << row
if n == 5
return @csv
end
end
end
end
The above code is built from what I've seen others use on Stack Overflow, but I can't get it to work.
If I remove the read
method from the file, it raises a TypeError
exception, saying:
can't convert StringIO into String
Is there something I'm missing?