outbox 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +83 -2
- data/lib/outbox.rb +19 -2
- data/lib/outbox/accessor.rb +71 -0
- data/lib/outbox/clients/base.rb +39 -0
- data/lib/outbox/clients/mail_client.rb +40 -0
- data/lib/outbox/clients/test_client.rb +20 -0
- data/lib/outbox/errors.rb +5 -0
- data/lib/outbox/message.rb +47 -0
- data/lib/outbox/message_clients.rb +70 -0
- data/lib/outbox/message_fields.rb +209 -0
- data/lib/outbox/message_types.rb +62 -0
- data/lib/outbox/messages/base.rb +46 -0
- data/lib/outbox/messages/email.rb +77 -0
- data/lib/outbox/version.rb +1 -1
- data/outbox.gemspec +3 -2
- data/spec/outbox/accessor_spec.rb +58 -0
- data/spec/outbox/clients/base_spec.rb +29 -0
- data/spec/outbox/clients/mail_client_spec.rb +43 -0
- data/spec/outbox/clients/test_client_spec.rb +26 -0
- data/spec/outbox/message_spec.rb +103 -0
- data/spec/outbox/messages/base_spec.rb +212 -0
- data/spec/outbox/messages/email_spec.rb +135 -0
- data/spec/outbox_spec.rb +0 -4
- data/spec/spec_helper.rb +9 -0
- metadata +48 -8
@@ -0,0 +1,212 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Outbox::Messages::Base do
|
4
|
+
class Message < Outbox::Messages::Base
|
5
|
+
default_client :test
|
6
|
+
fields :to, 'from'
|
7
|
+
field :foo
|
8
|
+
end
|
9
|
+
|
10
|
+
class MessageWithRequiredField < Outbox::Messages::Base
|
11
|
+
default_client :test
|
12
|
+
field :foo, required: true
|
13
|
+
end
|
14
|
+
|
15
|
+
class MessageWithRequiredFields < Outbox::Messages::Base
|
16
|
+
default_client :test
|
17
|
+
required_fields :to, :from
|
18
|
+
fields :foo
|
19
|
+
end
|
20
|
+
|
21
|
+
class MessageWithoutFieldAccessors < Outbox::Messages::Base
|
22
|
+
attr_reader :to_reader_called, :to_writer_called,
|
23
|
+
:from_reader_called, :from_writer_called,
|
24
|
+
:body_reader_called, :body_writer_called
|
25
|
+
|
26
|
+
def to(value = nil)
|
27
|
+
@to_reader_called = true
|
28
|
+
value ? @to = value : @to
|
29
|
+
end
|
30
|
+
|
31
|
+
def to=(value)
|
32
|
+
@to_writer_called = true
|
33
|
+
@to = value
|
34
|
+
end
|
35
|
+
|
36
|
+
def from(value = nil)
|
37
|
+
@from_reader_called = true
|
38
|
+
value ? @fields[:from] = value : @fields[:from]
|
39
|
+
end
|
40
|
+
|
41
|
+
def from=(value)
|
42
|
+
@from_writer_called = true
|
43
|
+
@fields[:from] = value
|
44
|
+
end
|
45
|
+
|
46
|
+
def body
|
47
|
+
@body_reader_called = true
|
48
|
+
@fields[:body]
|
49
|
+
end
|
50
|
+
|
51
|
+
def body=(value)
|
52
|
+
@body_writer_called = true
|
53
|
+
@fields[:body] = value
|
54
|
+
end
|
55
|
+
|
56
|
+
field :to, accessor: false
|
57
|
+
field :from, reader: false
|
58
|
+
field :body, writer: false
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '.default_client' do
|
62
|
+
after { Message.default_client :test }
|
63
|
+
|
64
|
+
it 'sets the default client' do
|
65
|
+
client = double :client
|
66
|
+
allow(client).to receive(:dup) { client }
|
67
|
+
Message.default_client(client)
|
68
|
+
message = Message.new
|
69
|
+
expect(message.client).to be(client)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe '.register_client' do
|
74
|
+
after { Message.default_client :test }
|
75
|
+
|
76
|
+
it 'registers a client alias' do
|
77
|
+
Client = Class.new
|
78
|
+
Message.register_client_alias :foo, Client
|
79
|
+
client = double :client
|
80
|
+
options = { option_1: 1, option_2: 2 }
|
81
|
+
expect(Client).to receive(:new).with(options) { client}
|
82
|
+
Message.default_client :foo, options
|
83
|
+
expect(Message.default_client).to be(client)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe '.field' do
|
88
|
+
it 'creates accessors for defined fields' do
|
89
|
+
message = Message.new
|
90
|
+
message.foo = :foo
|
91
|
+
expect(message.foo).to eq(:foo)
|
92
|
+
message.foo :bar
|
93
|
+
expect(message.foo).to eq(:bar)
|
94
|
+
end
|
95
|
+
|
96
|
+
it 'allows hash accessors for defined fields' do
|
97
|
+
message = Message.new
|
98
|
+
message['foo'] = :foo
|
99
|
+
expect(message[:foo]).to eq(:foo)
|
100
|
+
expect(message.foo).to eq(:foo)
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'does not create accessors if specified' do
|
104
|
+
message = MessageWithoutFieldAccessors.new
|
105
|
+
message.to = 'Bob'
|
106
|
+
message.from = 'John'
|
107
|
+
message.body = 'Hi'
|
108
|
+
expect(message.to).to eq('Bob')
|
109
|
+
expect(message.from).to eq('John')
|
110
|
+
expect(message.body).to eq('Hi')
|
111
|
+
expect(message.to_reader_called).to be_true
|
112
|
+
expect(message.to_writer_called).to be_true
|
113
|
+
expect(message.from_reader_called).to be_true
|
114
|
+
expect(message.from_writer_called).to be_false
|
115
|
+
expect(message.body_reader_called).to be_false
|
116
|
+
expect(message.body_writer_called).to be_true
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe '.defaults' do
|
121
|
+
it 'sets the fields defaults' do
|
122
|
+
Message.defaults from: 'John'
|
123
|
+
message = Message.new
|
124
|
+
expect(message.from).to eql('John')
|
125
|
+
end
|
126
|
+
|
127
|
+
context 'without field accessors' do
|
128
|
+
after { MessageWithoutFieldAccessors.instance_variable_set :@defaults, nil }
|
129
|
+
|
130
|
+
it 'still sets the defaults' do
|
131
|
+
MessageWithoutFieldAccessors.defaults to: 'Bob'
|
132
|
+
message = MessageWithoutFieldAccessors.new
|
133
|
+
expect(message.to).to eq('Bob')
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe '.new' do
|
139
|
+
it 'initializes with a hash' do
|
140
|
+
message = Message.new to: 'Bob', oops: 'John'
|
141
|
+
expect(message.to).to eq('Bob')
|
142
|
+
expect(message).to_not respond_to(:oops)
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'initializes with a block' do
|
146
|
+
message = Message.new do
|
147
|
+
to 'Bob'
|
148
|
+
from 'John'
|
149
|
+
end
|
150
|
+
expect(message.to).to eq('Bob')
|
151
|
+
expect(message.from).to eq('John')
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
describe '#fields' do
|
156
|
+
it 'returns a hash of the fields' do
|
157
|
+
message = Message.new
|
158
|
+
message.to = 'Bob'
|
159
|
+
message.from = 'John'
|
160
|
+
expect(message.fields).to eq(to: 'Bob', from: 'John', body: nil, foo: nil)
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'without field accessors' do
|
164
|
+
it 'returns a hash of the fields' do
|
165
|
+
message = MessageWithoutFieldAccessors.new to: 'Bob', from: 'John'
|
166
|
+
expect(message.fields).to eq(to: 'Bob', from: 'John', body: nil)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe '#audience=' do
|
172
|
+
it 'sets the to field by default' do
|
173
|
+
message = Message.new
|
174
|
+
message.audience = 'Bob'
|
175
|
+
expect(message.to).to eq('Bob')
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe '#deliver' do
|
180
|
+
context 'when valid' do
|
181
|
+
it 'delivers the message using a client' do
|
182
|
+
client = double :client
|
183
|
+
message = Message.new
|
184
|
+
message.client(client)
|
185
|
+
expect(client).to receive(:deliver).with(message)
|
186
|
+
message.deliver
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
context 'with an audience' do
|
191
|
+
it 'delivers to that audience' do
|
192
|
+
message = Message.new
|
193
|
+
message.deliver 'Bob'
|
194
|
+
expect(message.to).to eq('Bob')
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
context 'with a required field' do
|
199
|
+
it 'raises an error' do
|
200
|
+
message = MessageWithRequiredField.new
|
201
|
+
expect{message.deliver}.to raise_error(Outbox::MissingRequiredFieldError)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
context 'with required fields' do
|
206
|
+
it 'raises an error' do
|
207
|
+
message = MessageWithRequiredFields.new to: 'Bob'
|
208
|
+
expect{message.deliver}.to raise_error(Outbox::MissingRequiredFieldError)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Outbox::Messages::Email do
|
4
|
+
it 'sets common email fields' do
|
5
|
+
email = Outbox::Messages::Email.new do
|
6
|
+
to 'bob@gmail.com'
|
7
|
+
from 'john@gmail.com'
|
8
|
+
subject 'Simple email subject'
|
9
|
+
body 'Hello world.'
|
10
|
+
end
|
11
|
+
expect(email.to).to eq(['bob@gmail.com'])
|
12
|
+
expect(email.from).to eq(['john@gmail.com'])
|
13
|
+
expect(email.subject).to eq('Simple email subject')
|
14
|
+
expect(email.body).to eq('Hello world.')
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'sets a multipart body' do
|
18
|
+
email = Outbox::Messages::Email.new do
|
19
|
+
text_part do
|
20
|
+
body 'Hello world.'
|
21
|
+
end
|
22
|
+
html_part do
|
23
|
+
body '<h1>Hello world.</h1>'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
expect(email.parts.length).to eq(2)
|
27
|
+
end
|
28
|
+
|
29
|
+
describe '.registered_client_aliases' do
|
30
|
+
it 'includes TestClient' do
|
31
|
+
expect(Outbox::Messages::Email.registered_client_aliases[:test]).to eq(Outbox::Clients::TestClient)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'includes MailClient' do
|
35
|
+
expect(Outbox::Messages::Email.registered_client_aliases[:mail]).to eq(Outbox::Clients::MailClient)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe '.default_client' do
|
40
|
+
it 'defaults to MailClient' do
|
41
|
+
expect(Outbox::Messages::Email.default_client).to be_an_instance_of(Outbox::Clients::MailClient)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe '#audience=' do
|
46
|
+
context 'with a string' do
|
47
|
+
it 'sets the To: field' do
|
48
|
+
email = Outbox::Messages::Email.new
|
49
|
+
email.audience = 'bob@gmail.com'
|
50
|
+
expect(email.to).to eq(['bob@gmail.com'])
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context 'with an array' do
|
55
|
+
it 'sets the To: field' do
|
56
|
+
email = Outbox::Messages::Email.new
|
57
|
+
email.audience = ['bob@gmail.com', 'john@gmail.com']
|
58
|
+
expect(email.to).to eq(['bob@gmail.com', 'john@gmail.com'])
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'with a hash' do
|
63
|
+
it 'sets multiple fields' do
|
64
|
+
email = Outbox::Messages::Email.new
|
65
|
+
email.audience = {
|
66
|
+
:to => 'bob@gmail.com',
|
67
|
+
:cc => 'john@gmail.com',
|
68
|
+
'bcc' => 'sally@gmail.com',
|
69
|
+
:from => 'someone@gmail.com'
|
70
|
+
}
|
71
|
+
expect(email.to).to eq(['bob@gmail.com'])
|
72
|
+
expect(email.cc).to eq(['john@gmail.com'])
|
73
|
+
expect(email.bcc).to eq(['sally@gmail.com'])
|
74
|
+
expect(email.from).to be_nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'with an object' do
|
79
|
+
it 'sets multiple fields' do
|
80
|
+
email = Outbox::Messages::Email.new
|
81
|
+
email.audience = OpenStruct.new to: 'bob@gmail.com', cc: 'john@gmail.com'
|
82
|
+
expect(email.to).to eq(['bob@gmail.com'])
|
83
|
+
expect(email.cc).to eq(['john@gmail.com'])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe '#validate_fields' do
|
89
|
+
before do
|
90
|
+
@valid_email = Outbox::Messages::Email.new do
|
91
|
+
to 'bob@gmail.com'
|
92
|
+
from 'john@gmail.com'
|
93
|
+
body 'Hello world.'
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
it 'does not raise an error with a valid email' do
|
98
|
+
expect{@valid_email.validate_fields}.not_to raise_error()
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'raises an error without a recipient' do
|
102
|
+
@valid_email.to = nil
|
103
|
+
expect{@valid_email.validate_fields}.to raise_error(Outbox::MissingRequiredFieldError)
|
104
|
+
end
|
105
|
+
|
106
|
+
it 'does not raise an error with a Cc: field' do
|
107
|
+
@valid_email.to = nil
|
108
|
+
@valid_email.cc = 'bob@gmail.com'
|
109
|
+
expect{@valid_email.validate_fields}.not_to raise_error()
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'does not raise an error with a Bcc: field' do
|
113
|
+
@valid_email.to = nil
|
114
|
+
@valid_email.bcc = 'bob@gmail.com'
|
115
|
+
expect{@valid_email.validate_fields}.not_to raise_error()
|
116
|
+
end
|
117
|
+
|
118
|
+
it 'raises an error without a receiver' do
|
119
|
+
@valid_email.from = nil
|
120
|
+
expect{@valid_email.validate_fields}.to raise_error(Outbox::MissingRequiredFieldError)
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'does not raise an error with a Return-path: field' do
|
124
|
+
@valid_email.from = nil
|
125
|
+
@valid_email.return_path = 'bob@gmail.com'
|
126
|
+
expect{@valid_email.validate_fields}.not_to raise_error()
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'does not raise an error with a Sender: field' do
|
130
|
+
@valid_email.from = nil
|
131
|
+
@valid_email.sender = 'bob@gmail.com'
|
132
|
+
expect{@valid_email.validate_fields}.not_to raise_error()
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/spec/outbox_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: outbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,24 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mail
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.5.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.5.3
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: bundler
|
16
32
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,7 +91,7 @@ dependencies:
|
|
75
91
|
- - ! '>='
|
76
92
|
- !ruby/object:Gem::Version
|
77
93
|
version: '0'
|
78
|
-
description: A
|
94
|
+
description: A generic interface for sending email, SMS, & push notifications.
|
79
95
|
email:
|
80
96
|
- pete.browne@localmed.com
|
81
97
|
executables: []
|
@@ -89,8 +105,26 @@ files:
|
|
89
105
|
- README.md
|
90
106
|
- Rakefile
|
91
107
|
- lib/outbox.rb
|
108
|
+
- lib/outbox/accessor.rb
|
109
|
+
- lib/outbox/clients/base.rb
|
110
|
+
- lib/outbox/clients/mail_client.rb
|
111
|
+
- lib/outbox/clients/test_client.rb
|
112
|
+
- lib/outbox/errors.rb
|
113
|
+
- lib/outbox/message.rb
|
114
|
+
- lib/outbox/message_clients.rb
|
115
|
+
- lib/outbox/message_fields.rb
|
116
|
+
- lib/outbox/message_types.rb
|
117
|
+
- lib/outbox/messages/base.rb
|
118
|
+
- lib/outbox/messages/email.rb
|
92
119
|
- lib/outbox/version.rb
|
93
120
|
- outbox.gemspec
|
121
|
+
- spec/outbox/accessor_spec.rb
|
122
|
+
- spec/outbox/clients/base_spec.rb
|
123
|
+
- spec/outbox/clients/mail_client_spec.rb
|
124
|
+
- spec/outbox/clients/test_client_spec.rb
|
125
|
+
- spec/outbox/message_spec.rb
|
126
|
+
- spec/outbox/messages/base_spec.rb
|
127
|
+
- spec/outbox/messages/email_spec.rb
|
94
128
|
- spec/outbox_spec.rb
|
95
129
|
- spec/spec_helper.rb
|
96
130
|
homepage: https://github.com/localmed/outbox
|
@@ -108,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
108
142
|
version: '0'
|
109
143
|
segments:
|
110
144
|
- 0
|
111
|
-
hash:
|
145
|
+
hash: -1152741713307649410
|
112
146
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
147
|
none: false
|
114
148
|
requirements:
|
@@ -117,15 +151,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
117
151
|
version: '0'
|
118
152
|
segments:
|
119
153
|
- 0
|
120
|
-
hash:
|
154
|
+
hash: -1152741713307649410
|
121
155
|
requirements: []
|
122
156
|
rubyforge_project:
|
123
157
|
rubygems_version: 1.8.23
|
124
158
|
signing_key:
|
125
159
|
specification_version: 3
|
126
|
-
summary: Outbox is a
|
127
|
-
|
128
|
-
SaaS solutions.
|
160
|
+
summary: Outbox is a generic interface for sending notifications using a variety of
|
161
|
+
protocols, with built-in support for the most popular SaaS solutions.
|
129
162
|
test_files:
|
163
|
+
- spec/outbox/accessor_spec.rb
|
164
|
+
- spec/outbox/clients/base_spec.rb
|
165
|
+
- spec/outbox/clients/mail_client_spec.rb
|
166
|
+
- spec/outbox/clients/test_client_spec.rb
|
167
|
+
- spec/outbox/message_spec.rb
|
168
|
+
- spec/outbox/messages/base_spec.rb
|
169
|
+
- spec/outbox/messages/email_spec.rb
|
130
170
|
- spec/outbox_spec.rb
|
131
171
|
- spec/spec_helper.rb
|