euromail 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/euromail.rb +5 -0
- data/lib/euromail/sftp_development.rb +26 -0
- data/lib/euromail/sftp_service.rb +58 -0
- data/lib/euromail/sftp_test.rb +29 -0
- data/spec/euromail/sftp_development_spec.rb +50 -0
- data/spec/euromail/sftp_service_spec.rb +91 -0
- data/spec/euromail/sftp_test_spec.rb +45 -0
- data/spec/sftp_mock.rb +20 -0
- data/spec/spec_helper.rb +12 -0
- metadata +53 -0
data/lib/euromail.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module Euromail
|
2
|
+
|
3
|
+
module SFTPDevelopment
|
4
|
+
|
5
|
+
def upload! pdf_data, identifier
|
6
|
+
connect do
|
7
|
+
$stdout.puts "Uploaded #{filename(identifier)}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def remove! identifier
|
12
|
+
connect do
|
13
|
+
$stdout.puts "Removed #{filename(identifier)}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def connect &block
|
18
|
+
$stdout.puts "Connecting to #{host}"
|
19
|
+
block.call if block
|
20
|
+
$stdout.puts "Connection to #{host} closed"
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'net/sftp'
|
2
|
+
|
3
|
+
module Euromail
|
4
|
+
|
5
|
+
class SFTPService
|
6
|
+
|
7
|
+
attr_reader :application, :customer, :host, :username, :password
|
8
|
+
|
9
|
+
def initialize application, customer, host, username, password
|
10
|
+
@application = application
|
11
|
+
@customer = customer
|
12
|
+
@host = host
|
13
|
+
@username = username
|
14
|
+
@password = password
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_mode!
|
18
|
+
self.extend(Euromail::SFTPTest)
|
19
|
+
end
|
20
|
+
|
21
|
+
def development_mode!
|
22
|
+
self.extend(Euromail::SFTPDevelopment)
|
23
|
+
end
|
24
|
+
|
25
|
+
def upload! pdf_data, identifier
|
26
|
+
begin
|
27
|
+
connect do |sftp|
|
28
|
+
sftp.file.open( filename(identifier) , "w") do |f|
|
29
|
+
f.write pdf_data
|
30
|
+
end
|
31
|
+
end
|
32
|
+
rescue => e
|
33
|
+
remove!(identifier)
|
34
|
+
raise e
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# Attempt to remove the file for the given identifier.
|
39
|
+
def remove! identifier
|
40
|
+
connect do |sftp|
|
41
|
+
sftp.remove!( filename(identifier) )
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def connect &block
|
46
|
+
Net::SFTP.start(host, username, :password => password, &block)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Generate a filename based on the application, customer and some unique identifier.
|
50
|
+
# The identifier is not allowed to be blank since this risks previous files from being deleted.
|
51
|
+
def filename identifier
|
52
|
+
raise "An identifier is required" if identifier.to_s == ''
|
53
|
+
"./#{application}_#{customer}_#{identifier}.pdf"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module Euromail
|
2
|
+
module SFTPTest
|
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 || []
|
12
|
+
end
|
13
|
+
|
14
|
+
def upload! pdf_data, identifier
|
15
|
+
@uploaded_files = [] if @uploaded_files.nil?
|
16
|
+
@uploaded_files << filename(identifier)
|
17
|
+
end
|
18
|
+
|
19
|
+
def remove! identifier
|
20
|
+
@removed_files = [] if @removed_files.nil?
|
21
|
+
@removed_files << filename(identifier)
|
22
|
+
end
|
23
|
+
|
24
|
+
def connect &block
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Euromail::SFTPService do
|
4
|
+
include SFTPMock
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
mock_sftp
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:service) do
|
11
|
+
Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#development_mode!" do
|
15
|
+
before(:each) do
|
16
|
+
service.development_mode!
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#upload!" do
|
20
|
+
it "only logs uploads" do
|
21
|
+
Net::SFTP.should_not receive(:start)
|
22
|
+
$stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
|
23
|
+
$stdout.should receive(:puts).with("Uploaded ./moves_nedap_3.pdf")
|
24
|
+
$stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
|
25
|
+
service.upload!('come-client-code', '3')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "remove!" do
|
30
|
+
it "only logs deletes" do
|
31
|
+
Net::SFTP.should_not receive(:start)
|
32
|
+
$stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
|
33
|
+
$stdout.should receive(:puts).with("Removed ./moves_nedap_3.pdf")
|
34
|
+
$stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
|
35
|
+
service.remove!('3')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "connect" do
|
40
|
+
it "only logs the connection" do
|
41
|
+
Net::SFTP.should_not receive(:start)
|
42
|
+
$stdout.should receive(:puts).with("Connecting to some-cheapass-domain.com")
|
43
|
+
$stdout.should receive(:puts).with("Connection to some-cheapass-domain.com closed")
|
44
|
+
service.connect
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Euromail::SFTPService do
|
4
|
+
include SFTPMock
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
mock_sftp
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:service) do
|
11
|
+
Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when creating" do
|
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')
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#connect" do
|
25
|
+
it "connects to euromail using the given username and pass" do
|
26
|
+
Net::SFTP.should receive(:start).with('some-cheapass-domain.com', 'stefan', :password => 'super_secret')
|
27
|
+
service.connect {}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#upload!" do
|
32
|
+
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')
|
35
|
+
end
|
36
|
+
|
37
|
+
it "uploads pdf data" do
|
38
|
+
@file_hander.should receive(:write).with('some-client-code')
|
39
|
+
service.upload!('some-client-code', '1')
|
40
|
+
end
|
41
|
+
|
42
|
+
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')
|
45
|
+
end
|
46
|
+
|
47
|
+
it "tries to remove the remote file after an upload fails" do
|
48
|
+
@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
|
51
|
+
end
|
52
|
+
|
53
|
+
it "returns true if it succeeds" do
|
54
|
+
expect{ service.upload!('some-client-code', '2') }.to_not raise_error
|
55
|
+
end
|
56
|
+
|
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
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "#remove!" do
|
64
|
+
it "removes the file from the sftp server" do
|
65
|
+
@net_sftp_session.should receive(:remove!).with( service.filename('2') )
|
66
|
+
@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
|
72
|
+
end
|
73
|
+
|
74
|
+
it "return false if some error occurs" do
|
75
|
+
@net_sftp_session.stub(:remove!).and_raise("Some error")
|
76
|
+
expect{ service.remove!('2') }.to raise_error
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "#filename" do
|
81
|
+
it "generates a string with application, customer and the given identifier" do
|
82
|
+
service.filename('123').should eql('./moves_nedap_123.pdf')
|
83
|
+
end
|
84
|
+
|
85
|
+
it "requires a non empty identifier" do
|
86
|
+
expect{ service.filename('') }.to raise_error
|
87
|
+
expect{ service.filename(nil) }.to raise_error
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Euromail::SFTPService do
|
4
|
+
include SFTPMock
|
5
|
+
|
6
|
+
before(:each) do
|
7
|
+
mock_sftp
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:service) do
|
11
|
+
Euromail::SFTPService.new('moves', 'nedap', 'some-cheapass-domain.com', "stefan", "super_secret")
|
12
|
+
end
|
13
|
+
|
14
|
+
context "#test_mode!" do
|
15
|
+
before(:each) do
|
16
|
+
service.test_mode!
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#upload!" do
|
20
|
+
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')]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "remove!" do
|
28
|
+
it "stores deleted filenames" do
|
29
|
+
service.removed_files.should == []
|
30
|
+
service.remove!('2')
|
31
|
+
service.removed_files.should == [service.filename('2')]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "connect" do
|
36
|
+
it "does nothing" do
|
37
|
+
Net::SFTP.should_not receive(:start)
|
38
|
+
$stdout.should_not receive(:puts)
|
39
|
+
service.connect
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
data/spec/sftp_mock.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
module SFTPMock
|
2
|
+
def mock_sftp
|
3
|
+
@net_sftp_session = double("Net::SFTP::Session")
|
4
|
+
@file_hander = double("SomeFileHandler")
|
5
|
+
|
6
|
+
@file_hander.stub(:write) do |data|
|
7
|
+
end
|
8
|
+
|
9
|
+
@net_sftp_session.stub(:remove!) do |filename|
|
10
|
+
end
|
11
|
+
|
12
|
+
@net_sftp_session.stub_chain(:file, :open) do |filename, method, &block|
|
13
|
+
block.call(@file_hander) if block
|
14
|
+
end
|
15
|
+
|
16
|
+
Net::SFTP.stub(:start) do |host, username, password_hash, &block|
|
17
|
+
block.call(@net_sftp_session) if block
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: euromail
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Stefan Teijgeler
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-26 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Euromail SFTP service
|
15
|
+
email: stefan.teijgeler@nedap.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/euromail/sftp_development.rb
|
21
|
+
- lib/euromail/sftp_service.rb
|
22
|
+
- lib/euromail/sftp_test.rb
|
23
|
+
- lib/euromail.rb
|
24
|
+
- spec/euromail/sftp_development_spec.rb
|
25
|
+
- spec/euromail/sftp_service_spec.rb
|
26
|
+
- spec/euromail/sftp_test_spec.rb
|
27
|
+
- spec/sftp_mock.rb
|
28
|
+
- spec/spec_helper.rb
|
29
|
+
homepage: https://github.com/steijgeler/euromail
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.25
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: Gem to upload pdf data to an SFTP server
|
53
|
+
test_files: []
|