Class: FtpTransfer
- Inherits:
-
Object
- Object
- FtpTransfer
- Defined in:
- lib/ftp_transfer.rb,
lib/ftp_transfer/version.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Method Summary collapse
- #download(remote_directory) ⇒ Object
-
#initialize(options = {}) ⇒ FtpTransfer
constructor
A new instance of FtpTransfer.
- #upload(remote_directory) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ FtpTransfer
Returns a new instance of FtpTransfer.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ftp_transfer.rb', line 8 def initialize( = {}) @host = [:host] @user = [:user] @password = [:pass] @local_directory = File.([:local_dir]) @archive_directory = nil if [:archive_dir] @archive_directory = File.([:archive_dir]) end @pattern = [:pattern] || '*' @port = [:port] || 21 @ftp = Net::FTP.new @ftp.connect(@host, @port) @ftp.login(@user, @password) @ftp.passive = true end |
Instance Method Details
#download(remote_directory) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ftp_transfer.rb', line 37 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
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ftp_transfer.rb', line 25 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 |