class IOP::FTPFileWriter

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

@since 0.1

Public Class Methods

new(ftp, file, **options) click to toggle source

Creates class instance.

@param ftp [String, Net::FTP] FTP server to connect to

@param file [String] file name to process

@param options [Hash] extra keyword parameters passed to Net::FTP constructor

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.

# File lib/iop/net/ftp.rb, line 178
def initialize(ftp, file, **options)
  @options = options
  @file = file
  @ftp = ftp
end

Public Instance Methods

process(data = nil) click to toggle source
# File lib/iop/net/ftp.rb, line 200
def process(data = nil)
  @io.write(data) unless data.nil?
end
process!() click to toggle source
Calls superclass method IOP::Sink#process!
# 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