Class: IOP::FTPFileWriter
Overview
Sink class to write file to FTP server.
This class an adapter for the standard Ruby +Net::FTP+ class.
Use case: store a number of files filled with random data to an FTP server reusing connection.
require 'iop/net/ftp'
require 'iop/securerandom'
ftp = Net::FTP.open('ftp.server', username: 'user')
begin
ftp.login
(1..3).each do |i|
( IOP::SecureRandomGenerator.new(1024) | IOP::FTPFileWriter.new(ftp, "random#{i}.dat") ).process!
end
ensure
ftp.close
end
Instance Attribute Summary
Attributes included from Sink
Instance Method Summary collapse
-
#initialize(ftp, file, **options) ⇒ FTPFileWriter
constructor
Creates class instance.
- #process(data = nil) ⇒ Object
- #process! ⇒ Object
Constructor Details
#initialize(ftp, file, **options) ⇒ FTPFileWriter
Creates class instance.
ftp can be either a +String+ of +Net::FTP+ instance. If it is a string a corresponding +Net::FTP+ instance will be created with options passed to its constructor.
If ftp is a string, a created FTP connection is managed, e.g. it is closed after the process is complete, otherwise supplied object is left as is and no closing is performed. This allows to reuse FTP connection for a sequence of operations.
178 179 180 181 182 |
# File 'lib/iop/net/ftp.rb', line 178 def initialize(ftp, file, **) @options = @file = file @ftp = ftp end |
Instance Method Details
#process(data = nil) ⇒ Object
200 201 202 |
# File 'lib/iop/net/ftp.rb', line 200 def process(data = nil) @io.write(data) unless data.nil? end |
#process! ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/iop/net/ftp.rb', line 184 def process! setup begin # FTP logic taken from Net::FTP#storbinary @io = transfercmd('STOR ' << @file) begin super ensure @io.close end voidresp ensure cleanup end end |