Class: FtpTransfer

Inherits:
Object
  • Object
show all
Defined in:
lib/ftp_transfer.rb

Instance Method Summary collapse

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, options = {})
  @host = host
  @user = user
  @password = password
  @local_directory = File.expand_path(local_directory)
  @archive_directory = nil
  if options[:archive_dir]
    @archive_directory = File.expand_path(options[:archive_dir])
  end
  @pattern = options[: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.expand_path(@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