emarsys-broadcast 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,14 @@
1
+ module Emarsys
2
+ module Broadcast
3
+ module Validation
4
+ def string_present? value
5
+ !value.to_s.strip.empty?
6
+ end
7
+
8
+ def within_range? value, range
9
+ raise ArgumentError, 'range is required' if range.nil? || !range.is_a?(::Range)
10
+ range.include? value
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module Emarsys
2
+ module Broadcast
3
+ class ValidationError < StandardError
4
+ attr_reader :errors
5
+ def initialize(message, errors)
6
+ super(message)
7
+ @errors = errors
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Emarsys
2
+ module Broadcast
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,35 @@
1
+ require 'nokogiri'
2
+ module Emarsys
3
+ module Broadcast
4
+ class XmlBuilder
5
+ include Validation
6
+
7
+ def import_xml(remote_path)
8
+ xml = Nokogiri::XML::Builder.new do |xml|
9
+ xml.importRequest {
10
+ xml.filePath remote_path
11
+ }
12
+ end
13
+ xml.to_xml
14
+ end
15
+
16
+ def sender_xml(sender)
17
+ xml = Nokogiri::XML::Builder.new do |xml|
18
+ xml.sender {
19
+ xml.name sender.name
20
+ xml.address sender.address
21
+ }
22
+ end
23
+ xml.to_xml
24
+ end
25
+
26
+ private
27
+
28
+ def validate_options(options)
29
+ raise ArgumentError, 'options can not be nil' unless options
30
+ raise ArgumentError, 'name is required' unless string_present? options[:name]
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ require 'active_model'
2
+ require 'emarsys/broadcast/version'
3
+ require 'emarsys/broadcast/email'
4
+ require 'emarsys/broadcast/validation'
5
+ require 'emarsys/broadcast/configuration'
6
+ require 'emarsys/broadcast/sftp'
7
+ require 'emarsys/broadcast/http'
8
+ require 'emarsys/broadcast/validation_error'
9
+ require 'emarsys/broadcast/batch'
10
+ require 'emarsys/broadcast/sender'
11
+ require 'emarsys/broadcast/batch_xml_builder'
12
+ require 'emarsys/broadcast/xml_builder'
13
+ require 'emarsys/broadcast/api'
14
+
15
+ module Emarsys
16
+ module Broadcast
17
+ end
18
+ end
data/spec/api_spec.rb ADDED
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::API do
4
+ before(:each){create_valid_config}
5
+ after(:each){restore_default_config}
6
+ let(:config){create_valid_config}
7
+ describe 'initialize' do
8
+ context 'when configured properly' do
9
+
10
+ it 'should initialize a new instance of API' do
11
+ api = Emarsys::Broadcast::API.new
12
+ expect(api).not_to be_nil
13
+ end
14
+
15
+ it 'should instantiate sftp' do
16
+ Emarsys::Broadcast::SFTP.should_receive(:new).with(config)
17
+ api = Emarsys::Broadcast::API.new
18
+ end
19
+
20
+ it 'should instantiate http' do
21
+ Emarsys::Broadcast::HTTP.should_receive(:new).with(config)
22
+ api = Emarsys::Broadcast::API.new
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#send_batch' do
28
+ let(:api){Emarsys::Broadcast::API.new}
29
+ let(:minimal_batch){create_minimal_batch}
30
+
31
+ it 'should raise ValidationError if passed invalid batch' do
32
+ expect {
33
+ invalid_batch = Emarsys::Broadcast::Batch.new
34
+ api.send_batch(invalid_batch)
35
+ }.to raise_error Emarsys::Broadcast::ValidationError
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::Batch do
4
+ it 'should initialize a new instance of batch' do
5
+ expect(Emarsys::Broadcast::Batch.new).not_to be_nil
6
+ end
7
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::BatchXmlBuilder do
4
+ let(:batch_builder){batch_builder = Emarsys::Broadcast::BatchXmlBuilder.new}
5
+ let(:minimal_batch){create_minimal_batch}
6
+ let(:minimal_html_batch){create_minimal_html_batch}
7
+ describe 'initialize' do
8
+ it 'should create a new instance of BatchXmlBuilder' do
9
+ expect(batch_builder).not_to be_nil
10
+ end
11
+ end
12
+
13
+ describe '#build' do
14
+ # these tests fail on jruby Nokogiri
15
+ # it 'should return a valid Emarsys Xml XML string' do
16
+ # actual_xml = batch_builder.build(minimal_batch).chomp
17
+ # fixture_path = File.dirname(__FILE__) + '/fixtures/minimal_batch.xml'
18
+ # expected_xml = File.read(fixture_path)
19
+ # expect(actual_xml).to eq expected_xml
20
+ # end
21
+
22
+ # it 'should properly escape the body of the Emarsys Xml XML string' do
23
+ # actual_xml = batch_builder.build(minimal_html_batch).chomp
24
+ # fixture_path = File.dirname(__FILE__) + '/fixtures/minimal_escaped_batch.xml'
25
+ # expected_xml = File.read(fixture_path)
26
+ # expect(actual_xml).to eq expected_xml
27
+ # end
28
+ end
29
+ end
@@ -0,0 +1,215 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::Configuration do
4
+
5
+ after(:each){restore_default_config}
6
+
7
+ describe 'configure' do
8
+
9
+ it 'should return instance of configuration when configured' do
10
+ configuration = Emarsys::Broadcast::configure {}
11
+ expect(configuration).not_to be_nil
12
+ expect(configuration).to be_an_instance_of Emarsys::Broadcast::Configuration
13
+ end
14
+
15
+
16
+ describe 'when sftp_host is set' do
17
+ before do
18
+ Emarsys::Broadcast::configure do |c|
19
+ c.sftp_host = 'some_host'
20
+ end
21
+ end
22
+
23
+ it 'returns sftp_host' do
24
+ expect(Emarsys::Broadcast.configuration.sftp_host).to eq 'some_host'
25
+ end
26
+ end
27
+
28
+ describe 'when sftp_host is not set' do
29
+ before do
30
+ Emarsys::Broadcast::configure {}
31
+ end
32
+
33
+ it 'defaults to e3.emarsys.net' do
34
+ expect(Emarsys::Broadcast.configuration.sftp_host).to eq 'e3.emarsys.net'
35
+ end
36
+ end
37
+
38
+ describe 'when sftp_user is set' do
39
+ before do
40
+ Emarsys::Broadcast::configure do |c|
41
+ c.sftp_user = 'some_user'
42
+ end
43
+ end
44
+
45
+ it 'returns sftp_user' do
46
+ expect(Emarsys::Broadcast.configuration.sftp_user).to eq 'some_user'
47
+ end
48
+ end
49
+
50
+ describe 'when sftp_user is not set' do
51
+ before do
52
+ Emarsys::Broadcast::configure {}
53
+ end
54
+
55
+ it 'defaults to nil' do
56
+ expect(Emarsys::Broadcast.configuration.sftp_user).to be_nil
57
+ end
58
+ end
59
+
60
+ describe 'when sftp_password is set' do
61
+ before do
62
+ Emarsys::Broadcast::configure do |c|
63
+ c.sftp_password = 'some_password'
64
+ end
65
+ end
66
+
67
+ it 'returns sftp_password' do
68
+ expect(Emarsys::Broadcast.configuration.sftp_password).to eq 'some_password'
69
+ end
70
+ end
71
+
72
+ describe 'when sftp_password is not set' do
73
+ before do
74
+ Emarsys::Broadcast::configure {}
75
+ end
76
+
77
+ it 'defaults to nil' do
78
+ expect(Emarsys::Broadcast.configuration.sftp_password).to be_nil
79
+ end
80
+ end
81
+
82
+ describe 'when sftp_port is set' do
83
+ before do
84
+ Emarsys::Broadcast::configure do |c|
85
+ c.sftp_port = 2222
86
+ end
87
+ end
88
+
89
+ it 'returns sftp_port' do
90
+ expect(Emarsys::Broadcast.configuration.sftp_port).to eq 2222
91
+ end
92
+ end
93
+
94
+ describe 'when sftp_port is not set' do
95
+ before do
96
+ Emarsys::Broadcast::configure {}
97
+ end
98
+
99
+ it 'defaults to 22' do
100
+ expect(Emarsys::Broadcast.configuration.sftp_port).to eq 22
101
+ end
102
+ end
103
+
104
+ describe 'when api_host is set' do
105
+ before do
106
+ Emarsys::Broadcast::configure do |c|
107
+ c.api_host = 'some_host'
108
+ end
109
+ end
110
+
111
+ it 'returns api_host' do
112
+ expect(Emarsys::Broadcast.configuration.api_host).to eq 'some_host'
113
+ end
114
+ end
115
+
116
+ describe 'when api_host is not set' do
117
+ before do
118
+ Emarsys::Broadcast::configure {}
119
+ end
120
+
121
+ it 'defaults to e3.emarsys.net' do
122
+ expect(Emarsys::Broadcast.configuration.api_host).to eq 'e3.emarsys.net'
123
+ end
124
+ end
125
+
126
+ describe 'when api_user is set' do
127
+ before do
128
+ Emarsys::Broadcast::configure do |c|
129
+ c.api_user = 'some_user'
130
+ end
131
+ end
132
+
133
+ it 'returns api_user' do
134
+ expect(Emarsys::Broadcast.configuration.api_user).to eq 'some_user'
135
+ end
136
+ end
137
+
138
+ describe 'when api_user is not set' do
139
+ before do
140
+ Emarsys::Broadcast::configure {}
141
+ end
142
+
143
+ it 'defaults to nil' do
144
+ expect(Emarsys::Broadcast.configuration.api_user).to be_nil
145
+ end
146
+ end
147
+
148
+ describe 'when api_password is set' do
149
+ before do
150
+ Emarsys::Broadcast::configure do |c|
151
+ c.api_password = 'some_password'
152
+ end
153
+ end
154
+
155
+ it 'returns api_password' do
156
+ expect(Emarsys::Broadcast.configuration.api_password).to eq 'some_password'
157
+ end
158
+ end
159
+
160
+ describe 'when api_password is not set' do
161
+ before do
162
+ Emarsys::Broadcast::configure {}
163
+ end
164
+
165
+ it 'defaults to nil' do
166
+ expect(Emarsys::Broadcast.configuration.api_password).to be_nil
167
+ end
168
+ end
169
+
170
+ describe 'when api_port is set' do
171
+ before do
172
+ Emarsys::Broadcast::configure do |c|
173
+ c.api_port = 8080
174
+ end
175
+ end
176
+
177
+ it 'returns api_port' do
178
+ expect(Emarsys::Broadcast.configuration.api_port).to eq 8080
179
+ end
180
+ end
181
+
182
+ describe 'when api_port is not set' do
183
+ before do
184
+ Emarsys::Broadcast::configure {}
185
+ end
186
+
187
+ it 'defaults to 80' do
188
+ expect(Emarsys::Broadcast.configuration.api_port).to eq 80
189
+ end
190
+ end
191
+
192
+ describe 'when sender is set' do
193
+ before do
194
+ Emarsys::Broadcast::configure do |c|
195
+ c.sender = 'sender'
196
+ end
197
+ end
198
+
199
+ it 'returns sender' do
200
+ expect(Emarsys::Broadcast.configuration.sender).to eq 'sender'
201
+ end
202
+ end
203
+
204
+ describe 'when sender is not set' do
205
+ before do
206
+ Emarsys::Broadcast::configure {}
207
+ end
208
+
209
+ it 'defaults to nil' do
210
+ expect(Emarsys::Broadcast.configuration.sender).to be_nil
211
+ end
212
+ end
213
+ end
214
+
215
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::Email do
4
+ it 'should validate valid email' do
5
+ expect(Emarsys::Broadcast::Email::validate 'winston.smith-1984@big.brother.ru').to be_true
6
+ expect(Emarsys::Broadcast::Email::validate 'abc@example.com').to be_true
7
+ end
8
+ it 'should not validate invalid email' do
9
+ expect(Emarsys::Broadcast::Email::validate 'some invalid@email').to be_false
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0"?>
2
+ <batch><name>batch_name</name><runDate>2013-12-31T00:00:00-0800</runDate><properties><property key="Sender">sender_id</property><property key="Language">en</property><property key="Encoding">UTF-8</property><property key="Domain">e3.emarsys.net</property></properties><subject>subject</subject><html>body</html></batch>
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0"?>
2
+ <batch><name>batch_name</name><runDate>2013-12-31T00:00:00-0800</runDate><properties><property key="Sender">sender_id</property><property key="Language">en</property><property key="Encoding">UTF-8</property><property key="Domain">e3.emarsys.net</property></properties><subject>subject</subject><html>&lt;h1&gt;hello&lt;/h1&gt;</html></batch>
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0"?>
2
+ <importRequest>
3
+ <filePath>batch_name</filePath>
4
+ </importRequest>
data/spec/http_spec.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::HTTP do
4
+ let(:config){create_valid_config}
5
+ let(:http){Emarsys::Broadcast::HTTP.new config}
6
+ end
data/spec/sftp_spec.rb ADDED
@@ -0,0 +1,99 @@
1
+ require 'net/sftp'
2
+ require 'spec_helper'
3
+
4
+ describe Emarsys::Broadcast::SFTP do
5
+
6
+ describe 'initialize' do
7
+ after(:each){restore_default_config}
8
+ it 'should create a new instance of SFTP from valid configuration' do
9
+ config = Emarsys::Broadcast::configure do |c|
10
+ c.sftp_host = 'localhost'
11
+ c.sftp_user = 'user'
12
+ c.sftp_password = 'password'
13
+ end
14
+
15
+ sftp = Emarsys::Broadcast::SFTP.new config
16
+ expect(sftp).not_to be_nil
17
+ end
18
+
19
+ context 'invalid sftp configuration' do
20
+ it 'should raise ConfigurationError when config is nil (was never configured)' do
21
+ expect{Emarsys::Broadcast::SFTP.new(nil)}.to raise_error Emarsys::Broadcast::ConfigurationError
22
+ end
23
+
24
+ it 'should raise ConfigurationError when no sftp_user is configured' do
25
+ config = Emarsys::Broadcast::configure do |c|
26
+ c.sftp_host = 'host'
27
+ c.sftp_password = 'password'
28
+ end
29
+ expect{Emarsys::Broadcast::SFTP.new(config)}.to raise_error Emarsys::Broadcast::ConfigurationError
30
+ end
31
+
32
+ it 'should raise ConfigurationError when no sftp_password is configured' do
33
+ config = Emarsys::Broadcast::configure do |c|
34
+ c.sftp_host = 'host'
35
+ c.sftp_user = 'user'
36
+ end
37
+ expect{Emarsys::Broadcast::SFTP.new(config)}.to raise_error Emarsys::Broadcast::ConfigurationError
38
+ end
39
+
40
+ it 'should raise ConfigurationError when sftp_port is nil' do
41
+ config = Emarsys::Broadcast::configure do |c|
42
+ c.sftp_host = 'host'
43
+ c.sftp_user = 'user'
44
+ c.sftp_password = 'password'
45
+ c.sftp_port = nil
46
+ end
47
+ expect{Emarsys::Broadcast::SFTP.new(config)}.to raise_error Emarsys::Broadcast::ConfigurationError
48
+ end
49
+
50
+ it 'should raise ConfigurationError when sftp_port is not Integer' do
51
+ config = Emarsys::Broadcast::configure do |c|
52
+ c.sftp_host = 'host'
53
+ c.sftp_user = 'user'
54
+ c.sftp_password = 'password'
55
+ c.sftp_port = 'blaster'
56
+ end
57
+ expect{Emarsys::Broadcast::SFTP.new(config)}.to raise_error Emarsys::Broadcast::ConfigurationError
58
+ end
59
+
60
+ it 'should raise ConfigurationError when sftp_port is outside the valid range (1..65536)' do
61
+ config = Emarsys::Broadcast::configure do |c|
62
+ c.sftp_host = 'host'
63
+ c.sftp_user = 'user'
64
+ c.sftp_password = 'password'
65
+ c.sftp_port = 999999
66
+ end
67
+ expect{Emarsys::Broadcast::SFTP.new(config)}.to raise_error Emarsys::Broadcast::ConfigurationError
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'initialized' do
73
+ let(:config) do
74
+ config = Emarsys::Broadcast::configure do |c|
75
+ c.sftp_host = 'host'
76
+ c.sftp_user = 'user'
77
+ c.sftp_password = 'password'
78
+ end
79
+ end
80
+
81
+ let(:sftp) do
82
+ Emarsys::Broadcast::SFTP.new config
83
+ end
84
+
85
+ describe '#upload_file' do
86
+ it 'should call Net::SFTP.start with sftp configuration values' do
87
+ Net::SFTP.should_receive(:start).with(config.sftp_host, config.sftp_user, password: config.sftp_password)
88
+ sftp.upload_file('local_file', 'remote_file')
89
+ end
90
+
91
+ it 'should take an instance of SFTP as a block argument and call #upload! on it with file paths' do
92
+ mock_session = mock('session')
93
+ Net::SFTP.stub(:start).and_yield mock_session
94
+ mock_session.should_receive(:upload!).with('local_file', 'remote_file')
95
+ sftp.upload_file('local_file', 'remote_file')
96
+ end
97
+ end
98
+ end
99
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: UTF-8
2
+ require 'bundler/setup'
3
+ require 'emarsys/broadcast'
4
+ require 'timecop'
5
+
6
+
7
+ def restore_default_config
8
+ Emarsys::Broadcast.configuration = nil
9
+ Emarsys::Broadcast.configure {}
10
+ end
11
+
12
+ def create_valid_config
13
+ Emarsys::Broadcast::configure do |c|
14
+ c.sftp_host = 'a'
15
+ c.sftp_user = 'a'
16
+ c.sftp_password = 'a'
17
+
18
+ c.api_user = 'a'
19
+ c.api_password = 'a'
20
+
21
+ c.sender = spec_sender
22
+ end
23
+ end
24
+
25
+ def create_full_batch
26
+ batch = Emarsys::Broadcast::Batch.new
27
+ batch.name="test_batch_#{Time.now.to_i}"
28
+ batch.subject = 'די שטאַט פון ישראל'
29
+ batch.body_html = '<h1>די שטאט ווערט שוין דערמאנט אין תנ"ך. לויט געוויסע איז אליעזר (עבד אברהם) פון דמשק. דוד המלך, האט מלחמה געהאלטן און איינגענומען דמשק. און שפעטער מלכי ישראל. דאס איז שוין 3,000 יאר צוריק.<i>!</i></h1>'
30
+ batch.sender = 'פייַןבאָכער@gmail.com'
31
+ batch.sender_domain = 'google.com'
32
+ batch.send_time = Time.now + 1000000
33
+ batch
34
+ end
35
+
36
+ def create_minimal_batch
37
+ batch = Emarsys::Broadcast::Batch.new
38
+ batch.name="batch_name"
39
+ batch.subject = 'subject'
40
+ batch.body_html = 'body'
41
+ batch.send_time = spec_time
42
+ batch.sender_id = 'sender_id'
43
+ batch.sender_domain = 'e3.emarsys.net'
44
+ batch
45
+ end
46
+
47
+ def create_minimal_html_batch
48
+ batch = Emarsys::Broadcast::Batch.new
49
+ batch.name="batch_name"
50
+ batch.subject = 'subject'
51
+ batch.body_html = '<h1>hello</h1>'
52
+ batch.send_time = spec_time
53
+ batch.sender_id = 'sender_id'
54
+ batch.sender_domain = 'e3.emarsys.net'
55
+ batch
56
+ end
57
+
58
+ def spec_time
59
+ Time.new(2013, 12, 31, 0, 0, 0, "-08:00")
60
+ end
61
+
62
+ def spec_sender
63
+ 'abc@example.com'
64
+ end
65
+
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::Validation do
4
+ class Test
5
+ include Emarsys::Broadcast::Validation
6
+ end
7
+ let(:test){Test.new}
8
+ describe '#string_present?' do
9
+
10
+ it 'should return true if string not nil or empty' do
11
+ expect(test.string_present? 'hello').to be_true
12
+ end
13
+
14
+ it 'should return false if string is nil' do
15
+ expect(test.string_present? nil).to be_false
16
+ end
17
+
18
+ it 'should return false if string is empty' do
19
+ expect(test.string_present? '').to be_false
20
+ end
21
+
22
+ it 'should return false if string is all spaces' do
23
+ expect(test.string_present? ' ').to be_false
24
+ end
25
+ end
26
+
27
+ describe '#within_range?' do
28
+ it 'should return true if integer is within range' do
29
+ expect(test.within_range? 99, 1..100).to be_true
30
+ end
31
+
32
+ it 'should return false if integer is not within range' do
33
+ expect(test.within_range? 101, 1..100).to be_false
34
+ end
35
+
36
+ it 'should return false if integer is nil' do
37
+ expect(test.within_range? nil, 1..100).to be_false
38
+ end
39
+
40
+ it 'should raise ArgumentError is range is nil' do
41
+ expect{test.within_range?(99, nil)}.to raise_error ArgumentError
42
+ end
43
+
44
+ it 'should raise ArgumentError is range is not a Range' do
45
+ expect{test.within_range?(99, [])}.to raise_error ArgumentError
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Emarsys::Broadcast::VERSION do
4
+ it 'should return version' do
5
+ expect(Emarsys::Broadcast::VERSION).to_not be_nil
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'spec_helper'
2
+ describe Emarsys::Broadcast::XmlBuilder do
3
+ let(:valid_options){options = {name: 'batch_name'}}
4
+ let(:xml_builder){Emarsys::Broadcast::XmlBuilder.new(valid_options)}
5
+ end