Class: FileTransfer::FileTransferHandler

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

Instance Method Summary collapse

Constructor Details

#initialize(type, options) ⇒ FileTransferHandler

Returns a new instance of FileTransferHandler.



5
6
7
8
9
# File 'lib/file_transfer/file_transfer_handler.rb', line 5

def initialize(type, options)
  @type = type
  @options = options
  @ftp_obj = get_ftp_obj type, options
end

Instance Method Details

#closeObject



66
67
68
# File 'lib/file_transfer/file_transfer_handler.rb', line 66

def close
  @ftp_obj.close
end

#download(paths) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/file_transfer/file_transfer_handler.rb', line 38

def download(paths)
  paths = normalize_paths paths
  all_file_paths = []
  # Loop over Path Specs
  paths.each do |path|
    # Make sure it's an Array of from-Paths
    path[:from] = [path[:from]] unless path[:from].kind_of?(Array) && path[:from][0].kind_of?(Array)
    # Loop over Path From Specs
    path[:from].each do |path_pattern|
      _download path_pattern, path[:to]
      all_file_paths.push path_pattern
    end
  end
  all_file_paths
end

#list(path) ⇒ Object



54
55
56
# File 'lib/file_transfer/file_transfer_handler.rb', line 54

def list(path)
  @ftp_obj.list path
end

#remove(path) ⇒ Object



58
59
60
# File 'lib/file_transfer/file_transfer_handler.rb', line 58

def remove(path)
  @ftp_obj.remove path
end

#rename(from_path, to_path) ⇒ Object



62
63
64
# File 'lib/file_transfer/file_transfer_handler.rb', line 62

def rename(from_path, to_path)
  @ftp_obj.rename from_path, to_path
end

#upload(paths) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/file_transfer/file_transfer_handler.rb', line 11

def upload(paths)
  paths = normalize_paths paths
  all_file_paths = []
  # Loop over Path Specs
  paths.each do |path|
    # Make sure it's an Array of from-Paths
    path[:from] = [path[:from]] unless path[:from].kind_of?(Array) && path[:from][0].kind_of?(Array)
    # Loop over Path From Specs
    path[:from].each do |path_pattern|
      path_pattern = [path_pattern] unless path_pattern.kind_of?(Array)
      file_paths = []
      if File.exists?(path_pattern[0]) && File.directory?(path_pattern[0])
        path_pattern[1] = "*.*" if path_pattern.length < 2
        Dir.chdir(path_pattern[0])
        file_paths = Dir.glob(path_pattern[1])
      elsif File.exists?(path_pattern[0])
        file_paths = [path_pattern[0]]
      end
      file_paths.each do |file_path|
        _upload file_path, path[:to]
        all_file_paths.push file_path
      end
    end
  end
  all_file_paths
end