Class: FileTransfer::FileTransferHandler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, options) ⇒ FileTransferHandler

Returns a new instance of FileTransferHandler.



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

def initialize(type, options)
  @type = type
  @options = options
  @client = get_client type, options
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



4
5
6
# File 'lib/file_transfer/file_transfer_handler.rb', line 4

def client
  @client
end

Instance Method Details

#closeObject



73
74
75
# File 'lib/file_transfer/file_transfer_handler.rb', line 73

def close
  client.close
end

#download(paths) ⇒ Array

Downloads data from server

Parameters:

  • paths (Array<Hash<Symbol, String>>)

    download specification

Returns:

  • (Array)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/file_transfer/file_transfer_handler.rb', line 45

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



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

def list(path)
  client.list path
end

#remove(path) ⇒ Object



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

def remove(path)
  client.remove path
end

#rename(from_path, to_path) ⇒ Object



69
70
71
# File 'lib/file_transfer/file_transfer_handler.rb', line 69

def rename(from_path, to_path)
  client.rename from_path, to_path
end

#upload(paths) ⇒ Array

Uploads data to server

Parameters:

  • paths (Array<Hash<Symbol, String>>)

    upload specification

Returns:

  • (Array)


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

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