0

I have the following code to connect my rails app to my FTP. This works great. However, I want to use open-uri to open the csv file so I can parse it. Any ideas how to do this? I think it's an easy thing to do but I'm missing something.

    require 'net/ftp'
    ftp = Net::FTP.new
    ftp.connect("xxx.xxx.xx.xxx",21)
    ftp.login("xxxxx","xxxx")
    ftp.chdir("/")
    ftp.passive = true
    puts ftp.list("TEST.csv")
CodeCabin
  • 233
  • 1
  • 9
  • Check if it helps: https://stackoverflow.com/questions/8149366/how-to-read-content-of-remote-file-in-ruby-on-rails – iGian Aug 30 '18 at 08:50
  • Have you tried `open('ftp://xxxxx:xxxx@xxx.xxx.xx.xxx/TEST.csv')`? – Stefan Aug 30 '18 at 13:08

1 Answers1

0

You'll need to use #gettextfile.

A) Get the file to a local temporary file and read its content

# Creating a tmp file can be done differently as well.
# It may also be omitted, in which case `gettextfile`
# will create a file in the current directory.
Dir::Tmpname.create(['TEST', ['.csv']) do |file_name|
  ftp.gettextfile('TEST.csv', file_name)
  content = File.read(file_name)
end

B) Pass a block to gettextfile and get the content one line at a time

  content = ''
  ftp.gettextfile('TEST.csv') do |line|
    content << line
  end
koffeinfrei
  • 1,985
  • 13
  • 19