euromail 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  module Euromail
2
+ require 'euromail/sftp_connection'
2
3
  require 'euromail/sftp_service'
3
4
  require 'euromail/sftp_development'
4
5
  require 'euromail/sftp_test'
@@ -0,0 +1,20 @@
1
+ module Euromail
2
+ class SFTPConnection
3
+
4
+ def initialize service, sftp
5
+ @service = service
6
+ @sftp = sftp
7
+ end
8
+
9
+ def upload pdf_data, identifier
10
+ @sftp.file.open( @service.filename(identifier) , "w") do |f|
11
+ f.write pdf_data
12
+ end
13
+ end
14
+
15
+ def remove identifier
16
+ @sftp.remove!( @service.filename(identifier) )
17
+ end
18
+
19
+ end
20
+ end
@@ -1,30 +1,27 @@
1
1
  module Euromail
2
-
3
2
  module SFTPDevelopment
4
3
 
5
- def upload pdf_data, identifier
6
- raise "Can only be called in a connect block" unless @sftp
4
+ class SFTPConnection < Euromail::SFTPConnection
5
+ def upload pdf_data, identifier
6
+ $stdout.puts "Uploaded #{@service.filename(identifier)}"
7
+ end
7
8
 
8
- $stdout.puts "Uploaded #{filename(identifier)}"
9
+ def remove identifier
10
+ $stdout.puts "Removed #{@service.filename(identifier)}"
11
+ end
9
12
  end
10
13
 
11
- def remove identifier
12
- raise "Can only be called in a connect block" unless @sftp
13
-
14
- $stdout.puts "Removed #{filename(identifier)}"
15
- end
14
+ module ServiceMethods
15
+ def connect &block
16
+ $stdout.puts "Connecting to #{host}"
16
17
 
17
- def connect &block
18
- $stdout.puts "Connecting to #{host}"
18
+ connection = Euromail::SFTPDevelopment::SFTPConnection.new(self, "SFTP dummy")
19
+ block.call(connection)
19
20
 
20
- @sftp = 'Dummy'
21
- block.call(self)
22
- @sftp = nil
23
-
24
- $stdout.puts "Connection to #{host} closed"
21
+ $stdout.puts "Connection to #{host} closed"
22
+ end
25
23
  end
26
24
 
27
25
  end
28
-
29
26
  end
30
27
 
@@ -1,7 +1,6 @@
1
1
  require 'net/sftp'
2
2
 
3
3
  module Euromail
4
-
5
4
  class SFTPService
6
5
 
7
6
  attr_reader :application, :customer, :host, :username, :password
@@ -15,67 +14,43 @@ module Euromail
15
14
  end
16
15
 
17
16
  def test_mode!
18
- self.extend(Euromail::SFTPTest)
17
+ self.extend(Euromail::SFTPTest::ServiceMethods)
19
18
  end
20
19
 
21
20
  def development_mode!
22
- self.extend(Euromail::SFTPDevelopment)
23
- end
24
-
25
- # Upload pdf data to a file on the remote sftp server. Must be called within a connect block:
26
- # euromail.connect do |service|
27
- # service.upload('some-data', '1')
28
- # end
29
- def upload pdf_data, identifier
30
- raise "Can only be called in a connect block" unless @sftp
31
- @sftp.file.open( filename(identifier) , "w") do |f|
32
- f.write pdf_data
33
- end
21
+ self.extend(Euromail::SFTPDevelopment::ServiceMethods)
34
22
  end
35
23
 
36
24
  # Attempt to remove the file for the given identifier. If the upload fails or is aborted,
37
25
  # this method attempts to remove the incomplete file from the remote server.
38
26
  def upload! pdf_data, identifier
39
27
  begin
40
- connect do |service|
41
- service.upload(pdf_data, identifier)
28
+ connect do |connection|
29
+ connection.upload(pdf_data, identifier)
42
30
  end
43
31
  rescue => e
44
32
  remove!(identifier)
45
33
  raise e
46
34
  end
47
35
  end
48
-
49
- # Removes a pdf file on the remote sftp server. Must be called within a connect block:
50
- # euromail.connect do |service|
51
- # service.remove('1')
52
- # end
53
- def remove identifier
54
- raise "Can only be called in a connect block" unless @sftp
55
- @sftp.remove!( filename(identifier) )
56
- end
57
36
 
58
37
  # Attempt to remove the file for the given identifier.
59
38
  def remove! identifier
60
- connect do |service|
61
- service.remove( identifier )
39
+ connect do |connection|
40
+ connection.remove( identifier )
62
41
  end
63
42
  end
64
43
 
65
44
  # Setup a connection to the sftp server. Operations can be defined in the block passed to this method:
66
- # euromail.connect do |service|
67
- # service.upload('some data', '1')
68
- # service.upload('more data', '2')
69
- # service.remove('3')
45
+ # euromail.connect do |connection|
46
+ # connection.upload('some data', '1')
47
+ # connection.upload('more data', '2')
48
+ # connection.remove('3')
70
49
  # end
71
50
  def connect &block
72
51
  Net::SFTP.start(host, username, :password => password) do |sftp|
73
- begin
74
- @sftp = sftp
75
- block.call(self)
76
- ensure
77
- @sftp = nil
78
- end
52
+ connection = Euromail::SFTPConnection.new(self, sftp)
53
+ block.call(connection)
79
54
  end
80
55
  end
81
56
 
@@ -87,5 +62,4 @@ module Euromail
87
62
  end
88
63
 
89
64
  end
90
-
91
65
  end
@@ -1,36 +1,34 @@
1
1
  module Euromail
2
2
  module SFTPTest
3
3
 
4
- attr_writer :uploaded_files, :removed_files
5
-
6
- def uploaded_files
7
- return @uploaded_files || []
8
- end
9
-
10
- def removed_files
11
- return @removed_files || []
4
+ class SFTPConnection < Euromail::SFTPConnection
5
+ def upload pdf_data, identifier
6
+ @service.uploaded_files = [] if @service.uploaded_files.empty?
7
+ @service.uploaded_files << @service.filename(identifier)
8
+ end
9
+
10
+ def remove identifier
11
+ @service.removed_files = [] if @service.removed_files.empty?
12
+ @service.removed_files << @service.filename(identifier)
13
+ end
12
14
  end
13
15
 
14
- def upload pdf_data, identifier
15
- raise "Can only be called in a connect block" unless @sftp
16
-
17
- @uploaded_files = [] if @uploaded_files.nil?
18
- @uploaded_files << filename(identifier)
19
- end
16
+ module ServiceMethods
17
+ attr_writer :uploaded_files, :removed_files
20
18
 
21
- def remove identifier
22
- raise "Can only be called in a connect block" unless @sftp
19
+ def uploaded_files
20
+ return @uploaded_files || []
21
+ end
23
22
 
24
- @removed_files = [] if @removed_files.nil?
25
- @removed_files << filename(identifier)
26
- end
23
+ def removed_files
24
+ return @removed_files || []
25
+ end
27
26
 
28
- def connect &block
29
- @sftp = 'Dummy'
30
- block.call(self)
31
- @sftp = nil
27
+ def connect &block
28
+ connection = Euromail::SFTPTest::SFTPConnection.new(self, "SFTP dummy")
29
+ block.call(connection)
30
+ end
32
31
  end
33
32
 
34
33
  end
35
-
36
34
  end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ describe Euromail::SFTPConnection do
4
+ include SFTPMock
5
+
6
+ before(:each) do
7
+ mock_sftp
8
+ end
9
+
10
+ let(:euromail) do
11
+ Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
12
+ end
13
+
14
+ describe "#upload" do
15
+ it "use the generated filename" do
16
+ @net_sftp_session.file.should receive(:open).with( euromail.filename('1'), 'w')
17
+ euromail.connect do |connection|
18
+ connection.upload('some-client-code', '1')
19
+ end
20
+ end
21
+
22
+ it "does not reestablish a connection for each upload" do
23
+ expect(Net::SFTP).to receive(:start).once
24
+ euromail.connect do |connection|
25
+ connection.upload('some-client-code', '1')
26
+ connection.upload('another-client-code', '2')
27
+ end
28
+ end
29
+
30
+ it "can upload several pdf files within the same connection" do
31
+ expect(@net_sftp_session.file).to receive(:open).exactly(2).times
32
+ @file_hander.should receive(:write).with('some-client-code-1')
33
+ @file_hander.should receive(:write).with('some-client-code-2')
34
+
35
+ euromail.connect do |connection|
36
+ connection.upload("some-client-code-1", '1')
37
+ connection.upload("some-client-code-2", '2')
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "#remove" do
43
+ it "removes the file with the generated filename" do
44
+ @net_sftp_session.should receive(:remove!).with( euromail.filename('1') )
45
+ euromail.connect do |connection|
46
+ connection.remove('1')
47
+ end
48
+ end
49
+
50
+ it "does not reestablish a connection for each remove" do
51
+ expect(Net::SFTP).to receive(:start).once
52
+ euromail.connect do |connection|
53
+ connection.remove('1')
54
+ connection.remove('2')
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+
61
+ end
@@ -17,39 +17,39 @@ describe Euromail::SFTPService do
17
17
  end
18
18
 
19
19
  describe "#upload" do
20
- it "must be called in a connect block" do
21
- expect{ euromail.upload('some-client-code', '1')}.to raise_error(RuntimeError)
20
+ it "does not upload anything" do
21
+ @file_hander.should_not receive(:write)
22
22
  end
23
23
 
24
- it "only logs uploads" do
24
+ it "logs uploads" do
25
25
  Net::SFTP.should_not receive(:start)
26
26
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
27
27
  $stdout.should receive(:puts).with("Uploaded ./moves_nedap_3.pdf")
28
28
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
29
- euromail.connect do |service|
30
- service.upload('come-client-code', '3')
29
+ euromail.connect do |connection|
30
+ connection.upload('come-client-code', '3')
31
31
  end
32
32
  end
33
33
  end
34
34
 
35
- describe "remove" do
36
- it "must be called in a connect block" do
37
- expect{ euromail.remove('1')}.to raise_error(RuntimeError)
35
+ describe "#remove" do
36
+ it "does not remove anything" do
37
+ @net_sftp_session.should_not receive(:remove!)
38
38
  end
39
39
 
40
- it "only logs deletes" do
40
+ it "logs deletes" do
41
41
  Net::SFTP.should_not receive(:start)
42
42
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
43
43
  $stdout.should receive(:puts).with("Removed ./moves_nedap_3.pdf")
44
44
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
45
- euromail.connect do |service|
46
- service.remove('3')
45
+ euromail.connect do |connection|
46
+ connection.remove('3')
47
47
  end
48
48
  end
49
49
  end
50
50
 
51
- describe "connect" do
52
- it "only logs the connection" do
51
+ describe "#connect" do
52
+ it "logs the connection" do
53
53
  Net::SFTP.should_not receive(:start)
54
54
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
55
55
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
@@ -26,56 +26,6 @@ describe Euromail::SFTPService do
26
26
  Net::SFTP.should receive(:start).with('some-cheapass-domain.com', 'stefan', :password => 'super_secret')
27
27
  euromail.connect {}
28
28
  end
29
-
30
- it "unsets the connection if some error occurs" do
31
- expect {
32
- euromail.connect do |service|
33
- raise 'boem'
34
- end
35
- }.to raise_error
36
-
37
- # This should raise an error if the internal sftp connection is cleared
38
- expect{ euromail.remove('1') }.to raise_error(RuntimeError)
39
- end
40
-
41
- end
42
-
43
- describe "#upload" do
44
- it "use the generated filename" do
45
- @net_sftp_session.file.should receive(:open).with( euromail.filename('1'), 'w')
46
- euromail.connect do |service|
47
- service.upload('some-client-code', '1')
48
- end
49
- end
50
-
51
- it "must be called within a connect block" do
52
- expect{ euromail.upload('some-client-code', '1')}.to raise_error(RuntimeError)
53
- expect{
54
- euromail.connect do |service|
55
- service.upload('some-client-code', '1')
56
- service.upload('another-client-code', '2')
57
- end
58
- }.to_not raise_error
59
- end
60
-
61
- it "does not reestablish a connection for each upload" do
62
- expect(Net::SFTP).to receive(:start).once
63
- euromail.connect do |service|
64
- service.upload('some-client-code', '1')
65
- service.upload('another-client-code', '2')
66
- end
67
- end
68
-
69
- it "can upload several pdf files within the same connection" do
70
- expect(@net_sftp_session.file).to receive(:open).exactly(2).times
71
- @file_hander.should receive(:write).with('some-client-code-1')
72
- @file_hander.should receive(:write).with('some-client-code-2')
73
-
74
- euromail.connect do |service|
75
- service.upload("some-client-code-1", '1')
76
- service.upload("some-client-code-2", '2')
77
- end
78
- end
79
29
  end
80
30
 
81
31
  describe "#upload!" do
@@ -101,19 +51,6 @@ describe Euromail::SFTPService do
101
51
  end
102
52
  end
103
53
 
104
- describe "#remove" do
105
- it "removes the file with the generated filename" do
106
- @net_sftp_session.should receive(:remove!).with( euromail.filename('1') )
107
- euromail.connect do |service|
108
- service.remove('1')
109
- end
110
- end
111
-
112
- it "must be called within a connect block" do
113
- expect{ euromail.upload('some-client-code', '1')}.to raise_error(RuntimeError)
114
- end
115
- end
116
-
117
54
  describe "#remove!" do
118
55
  it "removes the file from the sftp server" do
119
56
  @net_sftp_session.should receive(:remove!).with( euromail.filename('2') )
@@ -17,10 +17,10 @@ describe Euromail::SFTPService do
17
17
  end
18
18
 
19
19
  describe "#upload" do
20
- it "must be called in a connect block" do
21
- expect{ euromail.upload('some-client-code', '1')}.to raise_error(RuntimeError)
20
+ it "does not upload anything" do
21
+ @file_hander.should_not receive(:write)
22
22
  end
23
-
23
+
24
24
  it "stores uploaded filenames" do
25
25
  euromail.uploaded_files.should == []
26
26
  euromail.upload!('some-client-code', '1')
@@ -28,11 +28,11 @@ describe Euromail::SFTPService do
28
28
  end
29
29
  end
30
30
 
31
- describe "remove" do
32
- it "must be called in a connect block" do
33
- expect{ euromail.remove('1')}.to raise_error(RuntimeError)
31
+ describe "#remove" do
32
+ it "does not remove anything" do
33
+ @net_sftp_session.should_not receive(:remove!)
34
34
  end
35
-
35
+
36
36
  it "stores deleted filenames" do
37
37
  euromail.removed_files.should == []
38
38
  euromail.remove!('2')
@@ -40,7 +40,7 @@ describe Euromail::SFTPService do
40
40
  end
41
41
  end
42
42
 
43
- describe "connect" do
43
+ describe "#connect" do
44
44
  it "only calls the block" do
45
45
  Net::SFTP.should_not receive(:start)
46
46
  $stdout.should_not receive(:puts)
@@ -1,7 +1,7 @@
1
1
  module SFTPMock
2
2
  def mock_sftp
3
3
  @net_sftp_session = double("Net::SFTP::Session")
4
- @file_hander = double("SomeFileHandler")
4
+ @file_hander = double("Net::SFTP::Operations::File")
5
5
 
6
6
  @file_hander.stub(:write) do |data|
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: euromail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -33,10 +33,12 @@ executables: []
33
33
  extensions: []
34
34
  extra_rdoc_files: []
35
35
  files:
36
+ - lib/euromail/sftp_connection.rb
36
37
  - lib/euromail/sftp_development.rb
37
38
  - lib/euromail/sftp_service.rb
38
39
  - lib/euromail/sftp_test.rb
39
40
  - lib/euromail.rb
41
+ - spec/euromail/sftp_connection_spec.rb
40
42
  - spec/euromail/sftp_development_spec.rb
41
43
  - spec/euromail/sftp_service_spec.rb
42
44
  - spec/euromail/sftp_test_spec.rb