How to FTP in Ruby without first saving the text file -
since heroku not allow saving dynamic files disk, i've run dilemma hoping can me overcome. have text file can create in ram. problem cannot find gem or function allow me stream file ftp server. net/ftp gem using requires save file disk first. suggestions?
ftp = net::ftp.new(domain) ftp.passive = true ftp.login(username, password) ftp.chdir(path_on_server) ftp.puttextfile(path_to_web_file) ftp.close
the ftp.puttextfile function requiring physical file exist.
stringio.new provides object acts opened file. it's easy create method puttextfile, using stringio object instead of file.
require 'net/ftp' require 'stringio' class net::ftp def puttextcontent(content, remotefile, &block) f = stringio.new(content) begin storlines("stor " + remotefile, f, &block) ensure f.close end end end file_content = <<filecontent <html> <head><title>hello!</title></head> <body>hello.</body> </html> filecontent ftp = net::ftp.new(domain) ftp.passive = true ftp.login(username, password) ftp.chdir(path_on_server) ftp.puttextcontent(file_content, path_to_web_file) ftp.close
Comments
Post a Comment