file_transfer 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/file_transfer.rb +6 -12
- data/lib/file_transfer/file_transfer_handler.rb +19 -10
- data/lib/file_transfer/ftp.rb +55 -27
- data/lib/file_transfer/sftp.rb +38 -16
- data/lib/file_transfer/version.rb +1 -1
- data/spec/file_transfer/file_transfer_handler_spec.rb +72 -141
- data/spec/file_transfer/ftp_spec.rb +133 -0
- data/spec/file_transfer/sftp_spec.rb +70 -0
- metadata +108 -128
- data/lib/file_transfer/ftps.rb +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: d6e7704b3a187bdb4150db073a6d11f3e88faade
|
4
|
+
data.tar.gz: 87bbb29f0feba5e47dbf7cf52158a90838118b46
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b7592105dc8ccfbf3f165afa6b1be537fb23bab19db97f31f6a95e35bbbd715a2397e167a9fb94e9d33476313724275999931b70e8e7aae2d02bb79ac455fb0a
|
7
|
+
data.tar.gz: b2d501534bc726cc44d8ed8dac713d3d759af1f1039dfaca0c9cfdf3266dd1cf82cf6e0c4cb9f7c1ed7dd3c7990b8a2c86cc0d785225813112263737d7c927c9
|
data/lib/file_transfer.rb
CHANGED
@@ -1,26 +1,20 @@
|
|
1
|
-
require
|
2
|
-
require
|
3
|
-
require
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require "file_transfer/ftps"
|
7
|
-
require "net/ftp"
|
8
|
-
require "net/sftp"
|
1
|
+
require 'file_transfer/version'
|
2
|
+
require 'file_transfer/file_transfer_handler'
|
3
|
+
require 'file_transfer/generic'
|
4
|
+
require 'file_transfer/ftp'
|
5
|
+
require 'file_transfer/sftp'
|
9
6
|
|
10
|
-
##
|
11
7
|
# Module for file exchange from a locale directory to a remote directory and vice versa. Supported protocols are:
|
12
8
|
#
|
13
9
|
# * FTP
|
14
10
|
# * SFTP
|
15
|
-
# *
|
16
|
-
|
11
|
+
# * REST
|
17
12
|
module FileTransfer
|
18
13
|
|
19
14
|
##
|
20
15
|
# Uploads one or more files to a remote server. To upload files use the locale file paths, not the ruby class File.
|
21
16
|
# Types are:
|
22
17
|
# * <tt>:ftp</tt> - for FTP file upload
|
23
|
-
# * <tt>:ftps</tt> - for FTPS file upload
|
24
18
|
# * <tt>:sftp</tt> - for SFTP file upload
|
25
19
|
#
|
26
20
|
# Options are:
|
@@ -1,13 +1,17 @@
|
|
1
1
|
module FileTransfer
|
2
2
|
|
3
3
|
class FileTransferHandler
|
4
|
+
attr_reader :client
|
4
5
|
|
5
6
|
def initialize(type, options)
|
6
7
|
@type = type
|
7
8
|
@options = options
|
8
|
-
@
|
9
|
+
@client = get_client type, options
|
9
10
|
end
|
10
11
|
|
12
|
+
# Uploads data to server
|
13
|
+
# @param paths [Array<Hash<Symbol, String>>] upload specification
|
14
|
+
# @return [Array]
|
11
15
|
def upload(paths)
|
12
16
|
paths = normalize_paths paths
|
13
17
|
all_file_paths = []
|
@@ -20,7 +24,7 @@ module FileTransfer
|
|
20
24
|
path_pattern = [path_pattern] unless path_pattern.kind_of?(Array)
|
21
25
|
file_paths = []
|
22
26
|
if File.exists?(path_pattern[0]) && File.directory?(path_pattern[0])
|
23
|
-
path_pattern[1] =
|
27
|
+
path_pattern[1] = '*.*' if path_pattern.length < 2
|
24
28
|
Dir.chdir(path_pattern[0])
|
25
29
|
file_paths = Dir.glob(path_pattern[1])
|
26
30
|
elsif File.exists?(path_pattern[0])
|
@@ -35,6 +39,9 @@ module FileTransfer
|
|
35
39
|
all_file_paths
|
36
40
|
end
|
37
41
|
|
42
|
+
# Downloads data from server
|
43
|
+
# @param paths [Array<Hash<Symbol, String>>] download specification
|
44
|
+
# @return [Array]
|
38
45
|
def download(paths)
|
39
46
|
paths = normalize_paths paths
|
40
47
|
all_file_paths = []
|
@@ -52,29 +59,31 @@ module FileTransfer
|
|
52
59
|
end
|
53
60
|
|
54
61
|
def list(path)
|
55
|
-
|
62
|
+
client.list path
|
56
63
|
end
|
57
64
|
|
58
65
|
def remove(path)
|
59
|
-
|
66
|
+
client.remove path
|
60
67
|
end
|
61
68
|
|
62
69
|
def rename(from_path, to_path)
|
63
|
-
|
70
|
+
client.rename from_path, to_path
|
64
71
|
end
|
65
72
|
|
66
73
|
def close
|
67
|
-
|
74
|
+
client.close
|
68
75
|
end
|
69
76
|
|
70
77
|
private
|
71
|
-
|
78
|
+
# @return [Array]
|
72
79
|
def normalize_paths(paths)
|
73
80
|
response = paths.kind_of?(Array) ? paths : [paths]
|
74
81
|
Marshal.load(Marshal.dump(response))
|
75
82
|
end
|
76
83
|
|
77
|
-
|
84
|
+
# Gets instance of client
|
85
|
+
# @param type [Symbol] type of client, possible values are: :ftp, :sftp or :ftps
|
86
|
+
def get_client(type, options)
|
78
87
|
if type == :ftp
|
79
88
|
Ftp.new options
|
80
89
|
elsif type == :sftp
|
@@ -85,11 +94,11 @@ module FileTransfer
|
|
85
94
|
end
|
86
95
|
|
87
96
|
def _upload(from_path, to_path)
|
88
|
-
|
97
|
+
client.upload from_path, to_path
|
89
98
|
end
|
90
99
|
|
91
100
|
def _download(from_path, to_path)
|
92
|
-
|
101
|
+
client.download from_path, to_path
|
93
102
|
end
|
94
103
|
|
95
104
|
end
|
data/lib/file_transfer/ftp.rb
CHANGED
@@ -1,17 +1,21 @@
|
|
1
|
-
require
|
1
|
+
require 'net/ftp'
|
2
2
|
|
3
3
|
module FileTransfer
|
4
4
|
class Ftp < Generic
|
5
|
+
attr_reader :ftp
|
5
6
|
|
6
7
|
def initialize(options = {})
|
7
8
|
super(options)
|
8
9
|
end
|
9
10
|
|
11
|
+
# Gets directory listing from SFTP server
|
12
|
+
# @param dir [String] remote directory
|
13
|
+
# @param options [Hash<Symbol,String>] additional options
|
14
|
+
# @return [Array<String>] listing with all file paths
|
10
15
|
def list(dir, options = {})
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
result = @ftp.nlst
|
16
|
+
open_connection(60) do
|
17
|
+
ftp.chdir dir
|
18
|
+
result = ftp.nlst
|
15
19
|
if options.has_key? :file_type
|
16
20
|
result = result.select { |file_name| file_name.end_with? options[:file_type] }
|
17
21
|
end
|
@@ -19,65 +23,89 @@ module FileTransfer
|
|
19
23
|
end
|
20
24
|
end
|
21
25
|
|
26
|
+
# Uploads file to FTP server
|
27
|
+
# @param from_path [String] locale directory
|
28
|
+
# @param to_path [String] remote directory
|
22
29
|
def upload(from_path, to_path)
|
23
30
|
to_path = split_path(to_path)
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@ftp.putbinaryfile(from_path)
|
31
|
+
open_connection do
|
32
|
+
ftp.chdir to_path[:file_path]
|
33
|
+
ftp.putbinaryfile(from_path)
|
28
34
|
end
|
29
35
|
end
|
30
36
|
|
37
|
+
# Downloads data from FTP server
|
38
|
+
# @param from_path [String] remote path
|
39
|
+
# @param to_path [String] locale path
|
40
|
+
# @return [String] remote path of downloaded file
|
31
41
|
def download(from_path, to_path)
|
32
42
|
from_path = split_path(from_path)
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
@ftp.getbinaryfile(to_path, from_path[:file_name])
|
43
|
+
open_connection do
|
44
|
+
ftp.chdir from_path[:file_path]
|
45
|
+
ftp.getbinaryfile(to_path, from_path[:file_name])
|
37
46
|
"#{from_path[:file_path]}/#{from_path[:file_name]}"
|
38
47
|
end
|
39
48
|
end
|
40
49
|
|
50
|
+
# Moves file from source to destination on FTP server
|
51
|
+
# @param from_path [String] remote source path
|
52
|
+
# @param to_path [String] remote destination path
|
41
53
|
def move(from_path, to_path)
|
42
54
|
from_path = split_path(from_path)
|
43
55
|
to_path = split_path(to_path)
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
@ftp.rename from_path[:file_name], "#{to_path[:file_path]}/#{to_path[:file_name]}" if exist?("#{from_path[:file_name]}/#{from_path[:file_path]}")
|
56
|
+
open_connection do
|
57
|
+
ftp.chdir from_path[:file_path]
|
58
|
+
ftp.rename from_path[:file_name], "#{to_path[:file_path]}/#{to_path[:file_name]}" if exist?("#{from_path[:file_name]}/#{from_path[:file_path]}")
|
48
59
|
end
|
49
60
|
end
|
61
|
+
alias_method :rename, :move
|
50
62
|
|
63
|
+
# Checks if file exists on FTP server
|
64
|
+
# @param file_path [String] path to check
|
65
|
+
# @return [Boolean] true if file exists, otherwise false
|
51
66
|
def exist?(file_path)
|
52
|
-
|
53
|
-
|
54
|
-
result = @ftp.list "#{file_path}"
|
67
|
+
open_connection(60) do
|
68
|
+
result = ftp.list "#{file_path}"
|
55
69
|
result && result.size > 0
|
56
70
|
end
|
57
71
|
end
|
58
72
|
|
73
|
+
def remove(file_path)
|
74
|
+
raise 'not yet implemented'
|
75
|
+
end
|
76
|
+
|
77
|
+
# Closes FTP connection
|
59
78
|
def close
|
60
|
-
if
|
79
|
+
if ftp && !ftp.closed?
|
61
80
|
timeout(30) do
|
62
|
-
|
81
|
+
ftp.close
|
63
82
|
end
|
64
83
|
end
|
65
84
|
end
|
66
85
|
|
86
|
+
# Opens new connection, if not already open
|
87
|
+
# @param conn_timeout [Integer] IDLE timeout in seconds
|
88
|
+
# @yield executes operation within open connection on server
|
89
|
+
def open_connection(conn_timeout = timeout_seconds)
|
90
|
+
connect if closed?
|
91
|
+
timeout(conn_timeout) do
|
92
|
+
yield if block_given?
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
67
96
|
protected
|
68
97
|
|
69
98
|
def closed?
|
70
|
-
|
99
|
+
!ftp || ftp.closed?
|
71
100
|
end
|
72
101
|
|
73
102
|
def connect
|
74
103
|
timeout(30) do
|
75
104
|
@ftp ||= Net::FTP.new
|
76
|
-
|
77
|
-
|
78
|
-
|
105
|
+
ftp.passive = true
|
106
|
+
ftp.connect(host, port)
|
107
|
+
ftp.login(username, password) if username && password
|
79
108
|
end
|
80
109
|
end
|
81
|
-
|
82
110
|
end
|
83
111
|
end
|
data/lib/file_transfer/sftp.rb
CHANGED
@@ -1,44 +1,56 @@
|
|
1
|
+
require 'net/sftp'
|
2
|
+
|
1
3
|
module FileTransfer
|
2
4
|
|
3
5
|
class Sftp < Generic
|
6
|
+
attr_reader :sftp
|
4
7
|
|
5
8
|
def initialize(options = {})
|
6
9
|
options[:port] ||= 22
|
7
10
|
super(options)
|
8
11
|
end
|
9
12
|
|
13
|
+
# Downloads data from SFTP server
|
14
|
+
# @param from_path [String] locale directory
|
15
|
+
# @param to_path [String] remote directory
|
10
16
|
def download(from_path, to_path)
|
11
|
-
|
12
|
-
|
13
|
-
@sftp.download! from_path, to_path
|
17
|
+
open_connection do
|
18
|
+
sftp.download! from_path, to_path
|
14
19
|
end
|
15
20
|
end
|
16
21
|
|
22
|
+
# Uploads data to SFTP server
|
23
|
+
# @param from_path [String] locale directory
|
24
|
+
# @param to_path [String] remote directory
|
17
25
|
def upload(from_path, to_path)
|
18
|
-
|
19
|
-
|
20
|
-
@sftp.upload! from_path, to_path
|
26
|
+
open_connection do
|
27
|
+
sftp.upload! from_path, to_path
|
21
28
|
end
|
22
29
|
end
|
23
30
|
|
31
|
+
# Gets directory listing from SFTP server
|
32
|
+
# @param path [String] remote directory
|
33
|
+
# @return [Array<String>] listing with all file paths
|
24
34
|
def list(path)
|
25
|
-
|
26
|
-
|
27
|
-
@sftp.dir.entries path
|
35
|
+
open_connection(60) do
|
36
|
+
sftp.dir.entries path
|
28
37
|
end
|
29
38
|
end
|
30
39
|
|
40
|
+
# Deletes file / folder from SFTP server
|
41
|
+
# @param path [String] remote directory
|
31
42
|
def remove(path)
|
32
|
-
|
33
|
-
|
34
|
-
@sftp.remove! path
|
43
|
+
open_connection(60) do
|
44
|
+
sftp.remove! path
|
35
45
|
end
|
36
46
|
end
|
37
47
|
|
48
|
+
# Moves file / folder from SFTP server
|
49
|
+
# @param from_path [String] remote source directory
|
50
|
+
# @param to_path [String] remote destination directory
|
38
51
|
def rename(from_path, to_path)
|
39
|
-
|
40
|
-
|
41
|
-
@sftp.rename! from_path, to_path
|
52
|
+
open_connection(60) do
|
53
|
+
sftp.rename! from_path, to_path
|
42
54
|
end
|
43
55
|
end
|
44
56
|
|
@@ -46,6 +58,16 @@ module FileTransfer
|
|
46
58
|
# do nothing
|
47
59
|
end
|
48
60
|
|
61
|
+
# Opens new connection, if not already open
|
62
|
+
# @param conn_timeout [Integer] IDLE timeout in seconds
|
63
|
+
# @yield executes operation within open connection on server
|
64
|
+
def open_connection(conn_timeout = timeout_seconds)
|
65
|
+
connect if closed?
|
66
|
+
timeout(conn_timeout) do
|
67
|
+
yield if block_given?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
49
71
|
protected
|
50
72
|
|
51
73
|
def connect
|
@@ -59,7 +81,7 @@ module FileTransfer
|
|
59
81
|
end
|
60
82
|
|
61
83
|
def closed?
|
62
|
-
|
84
|
+
!sftp || sftp.closed?
|
63
85
|
end
|
64
86
|
|
65
87
|
end
|
@@ -1,152 +1,83 @@
|
|
1
|
-
require
|
1
|
+
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.configure do |c|
|
4
4
|
c.filter_run_excluding :transaction => true
|
5
5
|
end
|
6
6
|
|
7
7
|
module FileTransfer
|
8
|
-
|
9
8
|
describe FileTransferHandler do
|
10
|
-
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
9
|
+
[:ftp, :sftp].each do |protocol|
|
10
|
+
context "#{protocol.to_s.upcase} client" do
|
11
|
+
let(:handler) { FileTransferHandler.new(protocol, {}) }
|
12
|
+
|
13
|
+
it "client is of type #{protocol.to_s.capitalize}" do
|
14
|
+
clazz = "FileTransfer::#{protocol.to_s.capitalize}".split('::').inject(Object) {|o,c| o.const_get c}
|
15
|
+
expect(handler.client).to a_kind_of(clazz)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#list' do
|
19
|
+
it 'executes list on client' do
|
20
|
+
expect(handler.client).to receive(:list).with('path')
|
21
|
+
handler.list('path')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '#remove' do
|
26
|
+
it 'executes remove on client' do
|
27
|
+
expect(handler.client).to receive(:remove).with('path')
|
28
|
+
handler.remove('path')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '#rename' do
|
33
|
+
it 'executes rename on client' do
|
34
|
+
expect(handler.client).to receive(:rename).with('from', 'to')
|
35
|
+
handler.rename('from', 'to')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '#close' do
|
40
|
+
it 'executes close on client' do
|
41
|
+
expect(handler.client).to receive(:close)
|
42
|
+
handler.close
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#download' do
|
47
|
+
it 'executes download on client' do
|
48
|
+
expect(handler.client).to receive(:download).with('from', 'to')
|
49
|
+
handler.download([{:from => 'from', :to => 'to'}])
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#upload' do
|
54
|
+
context 'when destination is folder' do
|
55
|
+
before do
|
56
|
+
allow(File).to receive(:exists?) { true }
|
57
|
+
allow(File).to receive(:directory?) { true }
|
58
|
+
allow(Dir).to receive(:chdir)
|
59
|
+
allow(Dir).to receive(:glob) { %w(from/test.txt) }
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'executes upload on client' do
|
63
|
+
expect(handler.client).to receive(:upload).with('from/test.txt', 'to')
|
64
|
+
handler.upload([{:from => 'from', :to => 'to'}])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when destination is file' do
|
69
|
+
before do
|
70
|
+
allow(File).to receive(:exists?) { true }
|
71
|
+
allow(File).to receive(:directory?) { false }
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'executes upload on client' do
|
75
|
+
expect(handler.client).to receive(:upload).with('from', 'to')
|
76
|
+
handler.upload([{:from => 'from', :to => 'to'}])
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
78
80
|
end
|
79
|
-
|
80
|
-
it "should download the test-file from OTTO SFTP" do
|
81
|
-
instance = FileTransferHandler.new :sftp, {
|
82
|
-
:host => "sftp-dmz.otto.de",
|
83
|
-
:username => "eosch",
|
84
|
-
:password => "hot}Orm1",
|
85
|
-
:port => 22
|
86
|
-
}
|
87
|
-
this_path = File.dirname(__FILE__)
|
88
|
-
paths = {:from => "/eosch/in/test/empty_file_1.csv", :to => "#{this_path}/../test_files/empty_file_from_sftp.csv"}
|
89
|
-
result = instance.download(paths)
|
90
|
-
Dir.chdir("#{this_path}/../test_files/")
|
91
|
-
result_array = Dir["*.csv"]
|
92
|
-
|
93
|
-
result.should be_kind_of(Array)
|
94
|
-
#result.should match_array(result_array)
|
95
|
-
|
96
|
-
puts "--BEGIN OF TEST"
|
97
|
-
puts result
|
98
|
-
puts "--END OF TEST"
|
99
|
-
end
|
100
|
-
|
101
|
-
it "should rename the test-file from OTTO SFTP" do
|
102
|
-
instance = FileTransferHandler.new :sftp, {
|
103
|
-
:host => "sftp-dmz.otto.de",
|
104
|
-
:username => "eosch",
|
105
|
-
:password => "hot}Orm1",
|
106
|
-
:port => 22
|
107
|
-
}
|
108
|
-
result = instance.rename "/eosch/in/test/empty_file_1.csv", "/eosch/in/test/empty_file_1.csv_old"
|
109
|
-
end
|
110
|
-
|
111
|
-
#it "should return an Array of various Files" do
|
112
|
-
# instance = FileTransferHandler.new :ftp, {}
|
113
|
-
# this_path = File.dirname(__FILE__)
|
114
|
-
# paths = [
|
115
|
-
# {:from => [
|
116
|
-
# ["#{this_path}/../test_files/", "**/*.txt"],
|
117
|
-
# "#{this_path}/../test_files/folder1/"
|
118
|
-
# ],
|
119
|
-
# :to => "/remote_path/"
|
120
|
-
# },
|
121
|
-
# {:from => ["#{this_path}/../test_files/folder2/", "[jt]*.jpg"], :to => "/remote_path/"}
|
122
|
-
# ]
|
123
|
-
#
|
124
|
-
# instance.stub(:_upload).and_return(true)
|
125
|
-
# result = instance.upload(paths)
|
126
|
-
#
|
127
|
-
# Dir.chdir(paths[0][:from][0][0])
|
128
|
-
# result_array = Dir[paths[0][:from][0][1]]
|
129
|
-
#
|
130
|
-
# Dir.chdir(paths[0][:from][1])
|
131
|
-
# Dir["*.*"].each do |f|
|
132
|
-
# result_array.push f
|
133
|
-
# end
|
134
|
-
#
|
135
|
-
# Dir.chdir(paths[1][:from][0])
|
136
|
-
# Dir[paths[1][:from][1]].each do |f|
|
137
|
-
# result_array.push f
|
138
|
-
# end
|
139
|
-
#
|
140
|
-
# result.should be_kind_of(Array)
|
141
|
-
# result.should match_array(result_array)
|
142
|
-
#
|
143
|
-
# puts "--BEGIN OF TEST"
|
144
|
-
# puts result
|
145
|
-
# puts "--END OF TEST"
|
146
|
-
#end
|
147
|
-
|
148
81
|
end
|
149
|
-
|
150
82
|
end
|
151
|
-
|
152
83
|
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module FileTransfer
|
4
|
+
describe Ftp do
|
5
|
+
let(:ftp_client) { Ftp.new }
|
6
|
+
let(:ftp) { double('ftp').as_null_object }
|
7
|
+
|
8
|
+
before do
|
9
|
+
allow(ftp_client).to receive(:ftp) { ftp }
|
10
|
+
end
|
11
|
+
|
12
|
+
context 'within open connection' do
|
13
|
+
before do
|
14
|
+
allow(ftp_client).to receive(:open_connection).and_yield
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#list' do
|
18
|
+
it 'changes directory to destination' do
|
19
|
+
expect(ftp).to receive(:chdir).with('dir')
|
20
|
+
ftp_client.list('dir')
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'gets directory listing' do
|
24
|
+
expect(ftp).to receive(:nlst)
|
25
|
+
ftp_client.list('dir')
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'returns directory listing' do
|
29
|
+
allow(ftp).to receive(:nlst) { %w(file1 file2) }
|
30
|
+
expect(ftp_client.list('dir')).to eq(%w(file1 file2))
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'when option file_type is defined' do
|
34
|
+
it 'filters files with given file_type' do
|
35
|
+
allow(ftp).to receive(:nlst) { %w(file1 file2.txt) }
|
36
|
+
expect(ftp_client.list('dir', :file_type => '.txt')).to eq(%w(file2.txt))
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe '#upload' do
|
42
|
+
it 'changes directory to destination' do
|
43
|
+
expect(ftp).to receive(:chdir).with('to')
|
44
|
+
ftp_client.upload('from/test.txt', 'to/test.txt')
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'uploads to FTP server' do
|
48
|
+
expect(ftp).to receive(:putbinaryfile).with('from/test.txt')
|
49
|
+
ftp_client.upload('from/test.txt', 'to/test.txt')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe '#download' do
|
54
|
+
it 'changes directory to destination' do
|
55
|
+
expect(ftp).to receive(:chdir).with('from')
|
56
|
+
ftp_client.download('from/test.txt', 'to/test.txt')
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'downloads from FTP server' do
|
60
|
+
expect(ftp).to receive(:getbinaryfile).with('to/test.txt', 'test.txt')
|
61
|
+
ftp_client.download('from/test.txt', 'to/test.txt')
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'returns file path' do
|
65
|
+
expect(ftp_client.download('from/test.txt', 'to/test.txt')).to eq('from/test.txt')
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#move' do
|
70
|
+
it 'changes directory to destination' do
|
71
|
+
expect(ftp).to receive(:chdir).with('dir1')
|
72
|
+
ftp_client.move('dir1/test.txt', 'dir2/test.txt')
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'moves file from current to new destination' do
|
76
|
+
expect(ftp).to receive(:rename).with('test.txt', 'dir2/test.txt')
|
77
|
+
ftp_client.move('dir1/test.txt', 'dir2/test.txt')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe '#exist?' do
|
82
|
+
it 'gets list' do
|
83
|
+
expect(ftp).to receive(:list).with('dir1/test.txt')
|
84
|
+
ftp_client.exist?('dir1/test.txt')
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'when file exists' do
|
88
|
+
before do
|
89
|
+
allow(ftp).to receive(:list) { %w(dir1/test.txt) }
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'gets true' do
|
93
|
+
expect(ftp_client.exist?('dir1/test.txt')).to be_truthy
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'when file does not exist' do
|
98
|
+
before do
|
99
|
+
allow(ftp).to receive(:list) { [] }
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'gets false' do
|
103
|
+
expect(ftp_client.exist?('dir1/test.txt')).to be_falsey
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#close' do
|
110
|
+
context 'when connection is open' do
|
111
|
+
before do
|
112
|
+
allow(ftp).to receive(:closed?) { false }
|
113
|
+
end
|
114
|
+
|
115
|
+
it 'closes connection' do
|
116
|
+
expect(ftp).to receive(:close)
|
117
|
+
ftp_client.close
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'when connection is closed' do
|
122
|
+
before do
|
123
|
+
allow(ftp).to receive(:closed?) { true }
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'do nothing' do
|
127
|
+
expect(ftp).to_not receive(:close)
|
128
|
+
ftp_client.close
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
module FileTransfer
|
4
4
|
describe Sftp do
|
5
|
+
let(:sftp_client) { Sftp.new }
|
5
6
|
|
6
7
|
describe '.new' do
|
7
8
|
context 'when port is defined in options parameter' do
|
@@ -18,5 +19,74 @@ module FileTransfer
|
|
18
19
|
end
|
19
20
|
end
|
20
21
|
end
|
22
|
+
|
23
|
+
describe '#open_connection' do
|
24
|
+
context 'when connection is closed' do
|
25
|
+
before do
|
26
|
+
allow(sftp_client).to receive(:closed?) { true }
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'connects to SFTP server' do
|
30
|
+
expect(sftp_client).to receive(:connect)
|
31
|
+
sftp_client.open_connection
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'when connection is open' do
|
36
|
+
before do
|
37
|
+
allow(sftp_client).to receive(:closed?) { false }
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'use current open connection' do
|
41
|
+
expect(sftp_client).to_not receive(:connect)
|
42
|
+
sftp_client.open_connection
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'within open connection' do
|
48
|
+
let(:sftp) { double('sftp') }
|
49
|
+
before do
|
50
|
+
allow(sftp_client).to receive(:open_connection).and_yield
|
51
|
+
allow(sftp_client).to receive(:sftp) { sftp }
|
52
|
+
end
|
53
|
+
|
54
|
+
describe '#download' do
|
55
|
+
it 'downloads from SFTP server' do
|
56
|
+
expect(sftp).to receive(:download!).with('from', 'to')
|
57
|
+
sftp_client.download('from', 'to')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#upload' do
|
62
|
+
it 'uploads to SFTP server' do
|
63
|
+
expect(sftp).to receive(:upload!).with('from', 'to')
|
64
|
+
sftp_client.upload('from', 'to')
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#list' do
|
69
|
+
let(:dir) { double('dir') }
|
70
|
+
it 'gets directory listing from SFTP server' do
|
71
|
+
expect(sftp).to receive(:dir) { dir }
|
72
|
+
expect(dir).to receive(:entries).with('path')
|
73
|
+
sftp_client.list('path')
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '#remove' do
|
78
|
+
it 'removes file / folder from SFTP server' do
|
79
|
+
expect(sftp).to receive(:remove!).with('path')
|
80
|
+
sftp_client.remove('path')
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
describe '#rename' do
|
85
|
+
it 'moves file / folder from SFTP server' do
|
86
|
+
expect(sftp).to receive(:rename!).with('from', 'to')
|
87
|
+
sftp_client.rename('from', 'to')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
21
91
|
end
|
22
92
|
end
|
metadata
CHANGED
@@ -1,139 +1,119 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: file_transfer
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
version: 0.0.9
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.10
|
6
5
|
platform: ruby
|
7
|
-
authors:
|
8
|
-
|
9
|
-
autorequire:
|
6
|
+
authors:
|
7
|
+
- impac AG
|
8
|
+
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
version_requirements: *id005
|
71
|
-
- !ruby/object:Gem::Dependency
|
72
|
-
name: net-ssh
|
73
|
-
prerelease: false
|
74
|
-
requirement: &id006 !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
|
-
requirements:
|
77
|
-
- - ~>
|
78
|
-
- !ruby/object:Gem::Version
|
79
|
-
version: 2.5.2
|
80
|
-
type: :runtime
|
81
|
-
version_requirements: *id006
|
11
|
+
date: 2016-04-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
15
|
+
requirements:
|
16
|
+
- - ">="
|
17
|
+
- !ruby/object:Gem::Version
|
18
|
+
version: '0'
|
19
|
+
name: rspec
|
20
|
+
prerelease: false
|
21
|
+
type: :development
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
name: rake
|
34
|
+
prerelease: false
|
35
|
+
type: :development
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
requirement: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
name: net-sftp
|
48
|
+
prerelease: false
|
49
|
+
type: :runtime
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.0.0
|
61
|
+
name: net-ssh
|
62
|
+
prerelease: false
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "<"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 3.0.0
|
82
69
|
description: File Transfer Handler for multiple Protocols
|
83
|
-
email:
|
84
|
-
|
70
|
+
email:
|
71
|
+
- md@impac.ch
|
85
72
|
executables: []
|
86
|
-
|
87
73
|
extensions: []
|
88
|
-
|
89
74
|
extra_rdoc_files: []
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
has_rdoc: true
|
106
|
-
homepage: http://www.impac.ch
|
75
|
+
files:
|
76
|
+
- lib/file_transfer.rb
|
77
|
+
- lib/file_transfer/file_transfer_handler.rb
|
78
|
+
- lib/file_transfer/ftp.rb
|
79
|
+
- lib/file_transfer/generic.rb
|
80
|
+
- lib/file_transfer/sftp.rb
|
81
|
+
- lib/file_transfer/version.rb
|
82
|
+
- spec/file_transfer/file_transfer_handler_spec.rb
|
83
|
+
- spec/file_transfer/ftp_spec.rb
|
84
|
+
- spec/file_transfer/generic_spec.rb
|
85
|
+
- spec/file_transfer/sftp_spec.rb
|
86
|
+
- spec/file_transfer_spec.rb
|
87
|
+
- spec/spec_helper.rb
|
88
|
+
- spec/test_files/empty_file.csv
|
89
|
+
homepage:
|
107
90
|
licenses: []
|
108
|
-
|
109
|
-
post_install_message:
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
110
93
|
rdoc_options: []
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
- - ">="
|
124
|
-
- !ruby/object:Gem::Version
|
125
|
-
version: "0"
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
126
106
|
requirements: []
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.8
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Transfer Files from and to FTP/SFTP
|
112
|
+
test_files:
|
113
|
+
- spec/file_transfer_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/file_transfer/file_transfer_handler_spec.rb
|
116
|
+
- spec/file_transfer/ftp_spec.rb
|
117
|
+
- spec/file_transfer/generic_spec.rb
|
118
|
+
- spec/file_transfer/sftp_spec.rb
|
119
|
+
- spec/test_files/empty_file.csv
|