3

I can't seem to get this to work. I want to pull a CSV file from a different webserver to read in my application. This is how I'd like to call it:

url = 'http://www.testing.com/test.csv'
records = FasterCSV.read(url, :headers => true, :header_converters => :symbol)

But that doesn't work. I tried Googling, and all I came up with was this excerpt: Practical Ruby Gems

So, I tried modifying it as follows:

require 'open-uri'
url = 'http://www.testing.com/test.csv'
csv_url = open(url)
records = FasterCSV.read(csv_url, :headers => true, :header_converters => :symbol)

... and I get a can't convert Tempfile into String error (coming from the FasterCSV gem).

Can anyone tell me how to make this work?

neezer
  • 19,720
  • 33
  • 121
  • 220

5 Answers5

5
require 'open-uri'
url = 'http://www.testing.com/test.csv'
open(url) do |f|
  f.each_line do |line|
    FasterCSV.parse(line) do |row|
      # Your code here
    end
  end
end

http://www.ruby-doc.org/core/classes/OpenURI.html http://fastercsv.rubyforge.org/

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
1

You just had a small typo. You should have used FasterCSV.parse instead of FasterCSV.read:

data = open('http://www.testing.com/test.csv')
records = FasterCSV.parse(data)
David J.
  • 31,569
  • 22
  • 122
  • 174
1

I would retrieve the file with Net::HTTP for example and feed that to FasterCSV

Extracted from ri Net::HTTP

 require 'net/http'
 require 'uri'

 url = URI.parse('http://www.example.com/index.html')
 res = Net::HTTP.start(url.host, url.port) {|http|
   http.get('/index.html')
 }
 puts res.body
Keltia
  • 14,535
  • 3
  • 29
  • 30
  • I tried that and now I'm getting an error: File name too long. After which, it lists the entire contents of my CSV file. I'm looking at the documentation now; Any suggestions? – neezer Jan 12 '09 at 15:12
  • "res.body" will be a String which you should probably split into an Array on "\n" and feed that to `FasterCSV.read` – Keltia Jan 12 '09 at 15:22
  • open-uri is higher level than Net::HTTP, I think it is better suited for neezer's example. – David J. Mar 02 '10 at 22:34
0

I would download it with rio - as easy as:

require 'rio'
require 'fastercsv'

array_of_arrays = FasterCSV.parse(rio('http://www.example.com/index.html').read)
0

I upload CSV file with Paperclip and save it to Cloudfiles and then start file processing with Delayed_job.

This worked for me:

require 'open-uri'
url = 'http://www.testing.com/test.csv'
open(url) do |file|
  FasterCSV.parse(file.read) do |row|
    # Your code here
  end
end
Voldy
  • 12,829
  • 8
  • 51
  • 67