euromail 0.3.1 → 0.4.0

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.
@@ -2,21 +2,25 @@ module Euromail
2
2
 
3
3
  module SFTPDevelopment
4
4
 
5
- def upload! pdf_data, identifier
6
- connect do
7
- $stdout.puts "Uploaded #{filename(identifier)}"
8
- end
5
+ def upload pdf_data, identifier
6
+ raise "Can only be called in a connect block" unless @sftp
7
+
8
+ $stdout.puts "Uploaded #{filename(identifier)}"
9
9
  end
10
10
 
11
- def remove! identifier
12
- connect do
13
- $stdout.puts "Removed #{filename(identifier)}"
14
- end
11
+ def remove identifier
12
+ raise "Can only be called in a connect block" unless @sftp
13
+
14
+ $stdout.puts "Removed #{filename(identifier)}"
15
15
  end
16
16
 
17
17
  def connect &block
18
18
  $stdout.puts "Connecting to #{host}"
19
- block.call if block
19
+
20
+ @sftp = 'Dummy'
21
+ block.call(self)
22
+ @sftp = nil
23
+
20
24
  $stdout.puts "Connection to #{host} closed"
21
25
  end
22
26
 
@@ -22,28 +22,61 @@ module Euromail
22
22
  self.extend(Euromail::SFTPDevelopment)
23
23
  end
24
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
34
+ end
35
+
36
+ # Attempt to remove the file for the given identifier. If the upload fails or is aborted,
37
+ # this method attempts to remove the incomplete file from the remote server.
25
38
  def upload! pdf_data, identifier
26
39
  begin
27
- connect do |sftp|
28
- sftp.file.open( filename(identifier) , "w") do |f|
29
- f.write pdf_data
30
- end
40
+ connect do |service|
41
+ service.upload(pdf_data, identifier)
31
42
  end
32
43
  rescue => e
33
44
  remove!(identifier)
34
45
  raise e
35
46
  end
36
47
  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
37
57
 
38
58
  # Attempt to remove the file for the given identifier.
39
59
  def remove! identifier
40
- connect do |sftp|
41
- sftp.remove!( filename(identifier) )
60
+ connect do |service|
61
+ service.remove( identifier )
42
62
  end
43
63
  end
44
-
64
+
65
+ # 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')
70
+ # end
45
71
  def connect &block
46
- Net::SFTP.start(host, username, :password => password, &block)
72
+ 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
79
+ end
47
80
  end
48
81
 
49
82
  # Generate a filename based on the application, customer and some unique identifier.
@@ -11,17 +11,24 @@ module Euromail
11
11
  return @removed_files || []
12
12
  end
13
13
 
14
- def upload! pdf_data, identifier
14
+ def upload pdf_data, identifier
15
+ raise "Can only be called in a connect block" unless @sftp
16
+
15
17
  @uploaded_files = [] if @uploaded_files.nil?
16
18
  @uploaded_files << filename(identifier)
17
19
  end
18
20
 
19
- def remove! identifier
21
+ def remove identifier
22
+ raise "Can only be called in a connect block" unless @sftp
23
+
20
24
  @removed_files = [] if @removed_files.nil?
21
25
  @removed_files << filename(identifier)
22
26
  end
23
27
 
24
28
  def connect &block
29
+ @sftp = 'Dummy'
30
+ block.call(self)
31
+ @sftp = nil
25
32
  end
26
33
 
27
34
  end
@@ -7,32 +7,44 @@ describe Euromail::SFTPService do
7
7
  mock_sftp
8
8
  end
9
9
 
10
- let(:service) do
10
+ let(:euromail) do
11
11
  Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
12
12
  end
13
13
 
14
14
  context "#development_mode!" do
15
15
  before(:each) do
16
- service.development_mode!
16
+ euromail.development_mode!
17
17
  end
18
18
 
19
- describe "#upload!" do
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)
22
+ end
23
+
20
24
  it "only logs uploads" do
21
25
  Net::SFTP.should_not receive(:start)
22
26
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
23
27
  $stdout.should receive(:puts).with("Uploaded ./moves_nedap_3.pdf")
24
28
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
25
- service.upload!('come-client-code', '3')
29
+ euromail.connect do |service|
30
+ service.upload('come-client-code', '3')
31
+ end
26
32
  end
27
33
  end
28
34
 
29
- describe "remove!" do
35
+ describe "remove" do
36
+ it "must be called in a connect block" do
37
+ expect{ euromail.remove('1')}.to raise_error(RuntimeError)
38
+ end
39
+
30
40
  it "only logs deletes" do
31
41
  Net::SFTP.should_not receive(:start)
32
42
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
33
43
  $stdout.should receive(:puts).with("Removed ./moves_nedap_3.pdf")
34
44
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
35
- service.remove!('3')
45
+ euromail.connect do |service|
46
+ service.remove('3')
47
+ end
36
48
  end
37
49
  end
38
50
 
@@ -41,7 +53,7 @@ describe Euromail::SFTPService do
41
53
  Net::SFTP.should_not receive(:start)
42
54
  $stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
43
55
  $stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
44
- service.connect
56
+ euromail.connect {}
45
57
  end
46
58
  end
47
59
 
@@ -7,84 +7,134 @@ describe Euromail::SFTPService do
7
7
  mock_sftp
8
8
  end
9
9
 
10
- let(:service) do
10
+ let(:euromail) do
11
11
  Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
12
12
  end
13
13
 
14
14
  context "when creating" do
15
15
  it "has an application, customer, host, username and password" do
16
- service.application.should eql('moves')
17
- service.customer.should eql('nedap')
18
- service.host.should eql('some-cheapass-domain.com')
19
- service.username.should eql('stefan')
20
- service.password.should eql('super_secret')
16
+ euromail.application.should eql('moves')
17
+ euromail.customer.should eql('nedap')
18
+ euromail.host.should eql('some-cheapass-domain.com')
19
+ euromail.username.should eql('stefan')
20
+ euromail.password.should eql('super_secret')
21
21
  end
22
22
  end
23
23
 
24
24
  describe "#connect" do
25
25
  it "connects to euromail using the given username and pass" do
26
26
  Net::SFTP.should receive(:start).with('some-cheapass-domain.com', 'stefan', :password => 'super_secret')
27
- service.connect {}
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
+
29
41
  end
30
42
 
31
- describe "#upload!" do
43
+ describe "#upload" do
32
44
  it "use the generated filename" do
33
- @net_sftp_session.file.should receive(:open).with( service.filename('1'), 'w')
34
- service.upload!('some-client-code', '1')
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
35
78
  end
79
+ end
36
80
 
81
+ describe "#upload!" do
37
82
  it "uploads pdf data" do
38
83
  @file_hander.should receive(:write).with('some-client-code')
39
- service.upload!('some-client-code', '1')
84
+ euromail.upload!('some-client-code', '1')
40
85
  end
41
86
 
42
87
  it "does not remove the remote file after an upload succeeds" do
43
- service.should_not receive(:remove!).with('1')
44
- service.upload!('some-client-code', '1')
88
+ euromail.should_not receive(:remove!).with('1')
89
+ euromail.upload!('some-client-code', '1')
45
90
  end
46
91
 
47
92
  it "tries to remove the remote file after an upload fails" do
48
93
  @file_hander.stub(:write).and_raise("Connection dropped")
49
- service.should receive(:remove!).with('1')
50
- expect{ service.upload!('some-client-code', '1') }.to raise_error
94
+ euromail.should receive(:remove!).with('1')
95
+ expect{ euromail.upload!('some-client-code', '1') }.to raise_error
51
96
  end
52
97
 
53
- it "returns true if it succeeds" do
54
- expect{ service.upload!('some-client-code', '2') }.to_not raise_error
98
+ it "raises if some error occurs" do
99
+ @net_sftp_session.stub_chain(:file, :open).and_raise("Some error")
100
+ expect{ euromail.upload!('some-client-code', '2') }.to raise_error
55
101
  end
102
+ end
56
103
 
57
- it "return false if some error occurs" do
58
- @net_sftp_session.stub_chain(:file, :open).and_raise("Some error")
59
- expect{ service.upload!('some-client-code', '2') }.to raise_error
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)
60
114
  end
61
115
  end
62
116
 
63
117
  describe "#remove!" do
64
118
  it "removes the file from the sftp server" do
65
- @net_sftp_session.should receive(:remove!).with( service.filename('2') )
119
+ @net_sftp_session.should receive(:remove!).with( euromail.filename('2') )
66
120
  @net_sftp_session.file.should_not receive(:open)
67
- service.remove!('2')
68
- end
69
-
70
- it "returns true if it succeeds" do
71
- expect{ service.remove!('2') }.to_not raise_error
121
+ euromail.remove!('2')
72
122
  end
73
123
 
74
- it "return false if some error occurs" do
124
+ it "raises if some error occurs" do
75
125
  @net_sftp_session.stub(:remove!).and_raise("Some error")
76
- expect{ service.remove!('2') }.to raise_error
126
+ expect{ euromail.remove!('2') }.to raise_error
77
127
  end
78
128
  end
79
129
 
80
130
  describe "#filename" do
81
131
  it "generates a string with application, customer and the given identifier" do
82
- service.filename('123').should eql('./moves_nedap_123.pdf')
132
+ euromail.filename('123').should eql('./moves_nedap_123.pdf')
83
133
  end
84
134
 
85
135
  it "requires a non empty identifier" do
86
- expect{ service.filename('') }.to raise_error
87
- expect{ service.filename(nil) }.to raise_error
136
+ expect{ euromail.filename('') }.to raise_error
137
+ expect{ euromail.filename(nil) }.to raise_error
88
138
  end
89
139
  end
90
140
 
@@ -7,36 +7,44 @@ describe Euromail::SFTPService do
7
7
  mock_sftp
8
8
  end
9
9
 
10
- let(:service) do
10
+ let(:euromail) do
11
11
  Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
12
12
  end
13
13
 
14
14
  context "#test_mode!" do
15
15
  before(:each) do
16
- service.test_mode!
16
+ euromail.test_mode!
17
17
  end
18
18
 
19
- describe "#upload!" do
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)
22
+ end
23
+
20
24
  it "stores uploaded filenames" do
21
- service.uploaded_files.should == []
22
- service.upload!('some-client-code', '1')
23
- service.uploaded_files.should == [service.filename('1')]
25
+ euromail.uploaded_files.should == []
26
+ euromail.upload!('some-client-code', '1')
27
+ euromail.uploaded_files.should == [euromail.filename('1')]
24
28
  end
25
29
  end
26
30
 
27
- describe "remove!" do
31
+ describe "remove" do
32
+ it "must be called in a connect block" do
33
+ expect{ euromail.remove('1')}.to raise_error(RuntimeError)
34
+ end
35
+
28
36
  it "stores deleted filenames" do
29
- service.removed_files.should == []
30
- service.remove!('2')
31
- service.removed_files.should == [service.filename('2')]
37
+ euromail.removed_files.should == []
38
+ euromail.remove!('2')
39
+ euromail.removed_files.should == [euromail.filename('2')]
32
40
  end
33
41
  end
34
42
 
35
43
  describe "connect" do
36
- it "does nothing" do
44
+ it "only calls the block" do
37
45
  Net::SFTP.should_not receive(:start)
38
46
  $stdout.should_not receive(:puts)
39
- service.connect
47
+ euromail.connect {}
40
48
  end
41
49
  end
42
50
 
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.3.1
4
+ version: 0.4.0
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: 2013-07-26 00:00:00.000000000 Z
12
+ date: 2013-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: net-sftp