FasterCSV: Read Remote CSV Files

I can't get this to work. I want to pull a CSV file from another web server for reading in my application. Here is what I would like to call it:

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

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

So, I tried to change 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 an error can't convert Tempfile into String(coming from the FasterCSV gem).

Can someone tell me how to make this work?

+3
source share
5 answers
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/

+5

Net::HTTP, , FasterCSV

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
+1

You had a little typo. You should use FasterCSV.parseinstead FasterCSV.read:

data = open('http://www.testing.com/test.csv')
records = FasterCSV.parse(data)
+1
source

I would download it with rio - as simple as:

require 'rio'
require 'fastercsv'

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

I upload a CSV file using Paperclip and save it to Cloudfiles, and then start file processing using 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
0
source

All Articles