Class: FtpTransfer
- Inherits:
-
Object
- Object
- FtpTransfer
- Defined in:
- lib/ftp_transfer.rb
Instance Method Summary collapse
- #download(remote_directory) ⇒ Object
-
#initialize(host, user, password, local_directory, options = {}) ⇒ FtpTransfer
constructor
A new instance of FtpTransfer.
- #upload(remote_directory) ⇒ Object
Constructor Details
#initialize(host, user, password, local_directory, options = {}) ⇒ FtpTransfer
Returns a new instance of FtpTransfer.
7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/ftp_transfer.rb', line 7 def initialize(host, user, password, local_directory, = {}) @host = host @user = user @password = password @local_directory = File.(local_directory) @archive_directory = nil if [:archive_dir] @archive_directory = File.([:archive_dir]) end @pattern = [:pattern] || '*' @ftp = Net::FTP.new(@host, @user, @password) @ftp.passive = true end |
Instance Method Details
#download(remote_directory) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ftp_transfer.rb', line 33 def download(remote_directory) @ftp.chdir(remote_directory) Dir.chdir(File.(@local_directory)) @ftp.nlst .select { |file| StringGlob.regexp(@pattern) =~ file } .each do |file| begin @ftp.getbinaryfile(file) rescue raise Net::FTPError, 'files did not transfer successfully' end @ftp.delete(file) if transfer_success?(file) end end |
#upload(remote_directory) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/ftp_transfer.rb', line 21 def upload(remote_directory) @ftp.chdir(remote_directory) Dir["#{@local_directory}/#{@pattern}"].each do |file| begin @ftp.putbinaryfile(file) rescue raise Net::FTPError, 'files did not transfer successfully' end archive_locally(file) if transfer_success?(file) && archive? end end |