Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get FTP records in Ruby without first saving the text file and feed CSV with it

Tags:

ruby

csv

ftp

I already get records from a ftp server with the gettextfile method and work on each record in the given block to finally put it elsewhere.

This file is a CSV file and I need to proceed it with CSV to get headers and datas and put it on DB after some work. As I have many different files, I need a generic way. I don't want to load all records on memory or disk, because the files can be very big! So a stream would be good

One idea is to give a io object to the CSV, but I don't see how to do that with Net::FTP.

I already see "http://stackoverflow.com/questions/5223763/how-to-ftp-in-ruby-without-first-saving-the-text-file" but it work with PUT.

Any help?

like image 794
user1834205 Avatar asked Oct 05 '22 22:10

user1834205


1 Answers

The technique that Justin mentions creates a temporary file.

You could use retrlines:

   filedata = ''
   ftp.retrlines("RETR " + filename) do |block|
      filedata << block
   end

or retrbinary instead:

   filedata = ''
   ftp.retrbinary("RETR " + filename, Net::FTP::DEFAULT_BLOCKSIZE) do |block|
      filedata << block
   end
like image 104
gef Avatar answered Oct 12 '22 19:10

gef