Class: IOP::FTPFileReader
- Inherits:
-
Object
- Object
- IOP::FTPFileReader
- Defined in:
- lib/iop/net/ftp.rb
Overview
Feed class to read file from FTP server.
This class an adapter for the standard Ruby Net::FTP class.
### Use case: retrieve file from FTP server and store it locally.
require 'iop/net/ftp'
( IOP::FTPFileReader.new('ftp.gnu.org', '/pub/README') | IOP::FileWriter.new('README') ).process!
Instance Attribute Summary
Attributes included from Feed
Instance Method Summary collapse
-
#initialize(ftp, file, size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE, **options) ⇒ FTPFileReader
constructor
Creates class instance.
- #process! ⇒ Object
Methods included from Feed
Constructor Details
#initialize(ftp, file, size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE, **options) ⇒ FTPFileReader
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.
Refer to Net::FTP documentation for available options.
87 88 89 90 91 92 93 94 |
# File 'lib/iop/net/ftp.rb', line 87 def initialize(ftp, file, size: nil, offset: nil, block_size: DEFAULT_BLOCK_SIZE, **) @block_size = size.nil? ? block_size : IOP.min(size, block_size) @left = @size = size @options = @offset = offset @file = file @ftp = ftp end |
Instance Method Details
#process! ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/iop/net/ftp.rb', line 96 def process! setup begin # FTP logic taken from Net::FTP#retrbinary @io = transfercmd('RETR ' << @file, @offset) begin loop do read_size = @size.nil? ? @block_size : IOP.min(@left, @block_size) break if read_size.zero? data = @io.read(read_size) if data.nil? if @size.nil? break else raise EOFError, INSUFFICIENT_DATA end else unless @left.nil? @left -= data.size raise IOError, EXTRA_DATA if @left < 0 end process(data) unless data.size.zero? end end process @io.shutdown(Socket::SHUT_WR) @io.read_timeout = 1 @io.read ensure @io.close end voidresp ensure cleanup end end |