file_transfer 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,8 +5,6 @@ module FileTransfer
5
5
 
6
6
  def initialize(options = {})
7
7
  super(options)
8
- @ftp = Net::FTP.new
9
- @ftp.passive = true
10
8
  end
11
9
 
12
10
  def list(dir, options = {})
@@ -24,7 +22,7 @@ module FileTransfer
24
22
  def upload(from_path, to_path)
25
23
  to_path = split_path(to_path)
26
24
  connect if @ftp.closed?
27
- timeout(300) do
25
+ timeout(timeout_seconds) do
28
26
  @ftp.chdir to_path[:file_path]
29
27
  @ftp.putbinaryfile(from_path)
30
28
  end
@@ -33,7 +31,7 @@ module FileTransfer
33
31
  def download(from_path, to_path)
34
32
  from_path = split_path(from_path)
35
33
  connect if @ftp.closed?
36
- timeout(300) do
34
+ timeout(timeout_seconds) do
37
35
  @ftp.chdir from_path[:file_path]
38
36
  @ftp.getbinaryfile(to_path, from_path[:file_name])
39
37
  "#{from_path[:file_path]}/#{from_path[:file_name]}"
@@ -44,7 +42,7 @@ module FileTransfer
44
42
  from_path = split_path(from_path)
45
43
  to_path = split_path(to_path)
46
44
  connect if @ftp.closed?
47
- timeout(60) do
45
+ timeout(timeout_seconds) do
48
46
  @ftp.chdir from_path[:file_path]
49
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]}")
50
48
  end
@@ -59,25 +57,27 @@ module FileTransfer
59
57
  end
60
58
 
61
59
  def close
62
- unless @ftp.closed?
60
+ if @ftp && !@ftp.closed?
63
61
  timeout(30) do
64
62
  @ftp.close
65
63
  end
66
64
  end
67
65
  end
68
66
 
69
- def to_s
70
- "FtpClient {@host => #{@host}, @username => #{@username}, @password => ***}"
71
- end
67
+ protected
72
68
 
73
- private
74
- def connect
75
- timeout(30) do
76
- @ftp.connect(@host, @port)
77
- @ftp.login(@username, @password) if @username && @password
69
+ def closed?
70
+ !@ftp || @ftp.closed?
78
71
  end
79
- end
80
72
 
73
+ def connect
74
+ timeout(30) do
75
+ @ftp ||= Net::FTP.new
76
+ @ftp.passive = true
77
+ @ftp.connect(@host, @port)
78
+ @ftp.login(@username, @password) if @username && @password
79
+ end
80
+ end
81
81
 
82
82
  end
83
83
  end
@@ -2,13 +2,14 @@ module FileTransfer
2
2
 
3
3
  class Generic
4
4
 
5
- attr_accessor :host, :username, :password, :port
5
+ attr_accessor :host, :username, :password, :port, :timeout_seconds
6
6
 
7
7
  def initialize(options = {})
8
8
  @host = options[:host] || "localhost"
9
9
  @username = options[:username] || "anonymous"
10
10
  @password = options[:password] || ""
11
11
  @port = options[:port] || 21
12
+ @timeout_seconds = options[:timeout] || 300
12
13
  end
13
14
 
14
15
  def list(dir, filter="")
@@ -29,6 +30,10 @@ module FileTransfer
29
30
  def close
30
31
  end
31
32
 
33
+ def to_s
34
+ "FtpClient {@host => #{host}, @username => #{username}, @protocol => #{type}, @port => #{port}, @password => ***}"
35
+ end
36
+
32
37
  protected
33
38
 
34
39
  def split_path(path)
@@ -44,6 +49,9 @@ module FileTransfer
44
49
  def connect
45
50
  end
46
51
 
52
+ def closed?
53
+ end
54
+
47
55
  end
48
56
 
49
57
  end
@@ -9,41 +9,33 @@ module FileTransfer
9
9
 
10
10
  def download(from_path, to_path)
11
11
  connect if closed?
12
- timeout(300) do
12
+ timeout(timeout_seconds) do
13
13
  @sftp.download! from_path, to_path
14
14
  end
15
15
  end
16
16
 
17
17
  def upload(from_path, to_path)
18
18
  connect if closed?
19
- timeout(300) do
19
+ timeout(timeout_seconds) do
20
20
  @sftp.upload! from_path, to_path
21
21
  end
22
22
  end
23
23
 
24
24
  def close
25
- if @sftp && !@sftp.closed?
26
- timeout(30) do
27
- @sftp.close
28
- end
29
- end
25
+ # do nothing
30
26
  end
31
27
 
32
- private
28
+ protected
33
29
 
34
30
  def connect
35
- @sftp = Net::SFTP.start(@host, @username, :password => @password, :port => @port)
31
+ timeout(60) do
32
+ @sftp = Net::SFTP.start(host, username, :password => password, :port => port)
33
+ end
36
34
  end
37
35
 
38
36
  def closed?
39
37
  !@sftp || @sftp.closed?
40
38
  end
41
39
 
42
- def timeout(seconds, &block)
43
- Timeout.timeout(seconds.to_i) do
44
- block.call
45
- end
46
- end
47
-
48
40
  end
49
41
  end
@@ -1,3 +1,3 @@
1
1
  module FileTransfer # :nodoc:
2
- VERSION = "0.0.2" # :nodoc:
2
+ VERSION = "0.0.4" # :nodoc:
3
3
  end
@@ -14,37 +14,37 @@ module FileTransfer
14
14
 
15
15
  describe "#upload" do
16
16
 
17
- it "should return an Array of JPG Files from one Folder" do
18
- instance = FileTransferHandler.new :ftp, {}
19
- this_path = File.dirname(__FILE__)
20
- paths = {:from => ["#{this_path}/../test_files/folder2", "*.jpg"], :to => "/remote_path/"}
21
- instance.stub(:_upload).and_return(true)
22
- result = instance.upload(paths)
23
- Dir.chdir(paths[:from][0])
24
- result_array = Dir[paths[:from][1]]
25
- result.should be_kind_of(Array)
26
- result.should match_array(result_array)
27
-
28
- puts "--BEGIN OF TEST"
29
- puts result
30
- puts "--END OF TEST"
31
- end
32
-
33
- it "should return an Array of JPG Files from all Subfolders incl. Root-Folder" do
34
- instance = FileTransferHandler.new :ftp, {}
35
- this_path = File.dirname(__FILE__)
36
- paths = {:from => ["#{this_path}/../test_files/", "**/*.jpg"], :to => "/remote_path/"}
37
- instance.stub(:_upload).and_return(true)
38
- result = instance.upload(paths)
39
- Dir.chdir(paths[:from][0])
40
- result_array = Dir[paths[:from][1]]
41
- result.should be_kind_of(Array)
42
- result.should match_array(result_array)
43
-
44
- puts "--BEGIN OF TEST"
45
- puts result
46
- puts "--END OF TEST"
47
- end
17
+ #it "should return an Array of JPG Files from one Folder" do
18
+ # instance = FileTransferHandler.new :ftp, {}
19
+ # this_path = File.dirname(__FILE__)
20
+ # paths = {:from => ["#{this_path}/../test_files/folder2", "*.jpg"], :to => "/remote_path/"}
21
+ # instance.stub(:_upload).and_return(true)
22
+ # result = instance.upload(paths)
23
+ # Dir.chdir(paths[:from][0])
24
+ # result_array = Dir[paths[:from][1]]
25
+ # result.should be_kind_of(Array)
26
+ # result.should match_array(result_array)
27
+ #
28
+ # puts "--BEGIN OF TEST"
29
+ # puts result
30
+ # puts "--END OF TEST"
31
+ #end
32
+ #
33
+ #it "should return an Array of JPG Files from all Subfolders incl. Root-Folder" do
34
+ # instance = FileTransferHandler.new :ftp, {}
35
+ # this_path = File.dirname(__FILE__)
36
+ # paths = {:from => ["#{this_path}/../test_files/", "**/*.jpg"], :to => "/remote_path/"}
37
+ # instance.stub(:_upload).and_return(true)
38
+ # result = instance.upload(paths)
39
+ # Dir.chdir(paths[:from][0])
40
+ # result_array = Dir[paths[:from][1]]
41
+ # result.should be_kind_of(Array)
42
+ # result.should match_array(result_array)
43
+ #
44
+ # puts "--BEGIN OF TEST"
45
+ # puts result
46
+ # puts "--END OF TEST"
47
+ #end
48
48
 
49
49
  it "should upload a test-file to OTTO SFTP" do
50
50
  instance = FileTransferHandler.new :sftp, {
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_transfer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-29 00:00:00.000000000 Z
12
+ date: 2012-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec