Class: FtpTransfer

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

Constant Summary collapse

VERSION =
'0.1.0'

Instance Method Summary collapse

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(options = {})
  @host = options[:host]
  @user = options[:user]
  @password = options[:pass]
  @local_directory = File.expand_path(options[:local_dir])
  @archive_directory = nil
  if options[:archive_dir]
    @archive_directory = File.expand_path(options[:archive_dir])
  end
  @pattern = options[:pattern] || '*'
  @port = options[:port] || 21
  @ftp = Net::FTP.new
  @ftp.connect(@host, @port)
  @ftp.(@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.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



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