Class: IOP::FTPFileWriter

Inherits:
Object
  • Object
show all
Includes:
FTPFile, Sink
Defined in:
lib/iop/net/ftp.rb

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.
  (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

Instance Attribute Summary

Attributes included from Sink

#upstream

Instance Method Summary collapse

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.

Parameters:

  • ftp (String, Net::FTP)

    FTP server to connect to

  • file (String)

    file name to process

  • options (Hash)

    extra keyword parameters passed to Net::FTP constructor

Since:

  • 0.1



177
178
179
180
181
# File 'lib/iop/net/ftp.rb', line 177

def initialize(ftp, file, **options)
  @options = options
  @file = file
  @ftp = ftp
end

Instance Method Details

#process(data = nil) ⇒ Object

Since:

  • 0.1



199
200
201
# File 'lib/iop/net/ftp.rb', line 199

def process(data = nil)
  @io.write(data) unless data.nil?
end

#process!Object

Since:

  • 0.1



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/iop/net/ftp.rb', line 183

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