Module: EasyDownloader::Ftp

Included in:
AbstractLoader
Defined in:
lib/easy_downloader/ftp.rb

Instance Method Summary collapse

Instance Method Details

#change_remote_dir(ftp) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/easy_downloader/ftp.rb', line 54

def change_remote_dir(ftp)
  if options.remote_path.present?
    begin
      ftp.chdir(options.remote_path)
    rescue InvalidRemoteDirectory
    end
  end
end

#ftp_download(options) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/easy_downloader/ftp.rb', line 4

def ftp_download(options)
  open_ftp do |ftp|

    change_remote_dir(ftp)

    files = ftp.nlst.select {|file_name| options.remote_pattern == '*' || file_name =~ Regexp.new(options.remote_pattern) }
    total = files.size
    options.result.found(files.size, files)

    files.each do |path|
      options.result.starting_path(path)
      ftp.get(path, "#{options.local_path}#{path}")
      options.load_count = options.load_count + 1
      options.result.finished_path(path)
      options.result.files_loaded << "#{options.local_path}#{path}"
    end
  end
end

#ftp_password_option(password) ⇒ Object



63
64
65
# File 'lib/easy_downloader/ftp.rb', line 63

def ftp_password_option(password)
  password ? password : nil
end

#ftp_upload(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/easy_downloader/ftp.rb', line 23

def ftp_upload(options)
  open_ftp do |ftp|

    change_remote_dir(ftp)

    files = local_files_list(options)
    options.result.found(files.size, files)

    files.each do |path|
      options.result.starting_path(path)
      if options.remote_file
        ftp.put(path, options.remote_file)
        options.result.files_loaded << options.remote_file
      else
        ftp.put(path)
        options.result.files_loaded << File.basename(path)
      end
      options.load_count= options.load_count + 1
      options.result.finished_path(path)
    end
  end
end

#open_ftp(&block) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/easy_downloader/ftp.rb', line 46

def open_ftp(&block)
  Net::FTP.open(options.host,
                options.user,
                ftp_password_option(options.password)) do |ftp|
    yield(ftp)
  end
end

#remote_files_list(options) ⇒ Object



68
69
70
71
72
73
74
# File 'lib/easy_downloader/ftp.rb', line 68

def remote_files_list(options)
  if options.remote_file
    [options.remote_file]
  else
    Dir[options.remote_path, options.remote_pattern]
  end
end