outbox 0.1.2 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7b0251b3437146e36c621521831de3f1b1f66754
4
- data.tar.gz: edf0111ebfff3b8cf1d7a6716ac3fb72c59f934a
3
+ metadata.gz: f4d0674db8e9a1b5e9ff4177d0ceb2ad37b10ae5
4
+ data.tar.gz: aed0fd01ee8e3022b381e9e5cdc6de527fc1a120
5
5
  SHA512:
6
- metadata.gz: 0fab54a11397b3fdd99407adcd41f16f10f624eba332686cf3c8c0fb3924605b5590f226da3fe2a51602a1aaad1ab8a48a5d1bb5cf2b7b9b664e0f9966fed1ed
7
- data.tar.gz: b3228876ee7f25bd9dfe9d53983fb691c7ad907c628ba227d0a58b621a4482f013cf1c50efc9578dea886ac81e3cf6ea04da91179fbb37cf0a2a60b7a00dbae2
6
+ metadata.gz: ad5f555dbe59e569c0f8a77c12d37d2847f21706f056b929fbc3b1a44a9cc22ecaf25064a5d58b02f9065072cf9e88b333756c851629563303515ced2113d4d9
7
+ data.tar.gz: 6cc7e36744f7b5a5f71ebc8f6cdbf826ff8f1e0323aafa5927534e9c7f6b3485a46720d9606c0fbd7fc8fe5f831a59954107eb03b06ea8df7ed9699b396b7ab0
data/README.md CHANGED
@@ -34,13 +34,15 @@ This gem is still in early development with plans to support email, SMS, and pus
34
34
 
35
35
  ### Email
36
36
 
37
- | Service | Alias | Client |
38
- |-------------------------------------------|---------|------------|
39
- | [Mail gem](https://github.com/mikel/mail) | `:mail` | MailClient |
37
+ | Service | Alias | Client | Gem |
38
+ |-------------------------------------------|---------|-------------------------------|----------|
39
+ | [Mail gem](https://github.com/mikel/mail) | `:mail` | `Outbox::Clients::MailClient` | Included |
40
40
 
41
41
  ### SMS
42
42
 
43
- TODO…
43
+ | Service | Alias | Client | Gem |
44
+ |-------------------------------------------------|-----------|--------------------------|------------------------------------------------------------|
45
+ | [Twilio](https://github.com/twilio/twilio-ruby) | `:twilio` | `Outbox::Twilio::Client` | [outbox-twilio](https://github.com/localmed/outbox-twilio) |
44
46
 
45
47
  ### Push
46
48
 
@@ -67,7 +69,7 @@ message = Outbox::Message.new do
67
69
  end
68
70
 
69
71
  ios_push do
70
- badget '+1'
72
+ badge '+1'
71
73
  sound 'default'
72
74
  end
73
75
 
@@ -75,7 +77,11 @@ message = Outbox::Message.new do
75
77
  end
76
78
 
77
79
  # This will deliver the message to User's given contact points.
78
- message.deliver email: 'user@gmail.com', sms: '+15551234567', ios_push: 'FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660'
80
+ message.deliver(
81
+ email: 'user@gmail.com',
82
+ sms: '+15551234567',
83
+ ios_push: 'FE66489F304DC75B8D6E8200DFF8A456E8DAEACEC428B427E9518741C92C6660'
84
+ )
79
85
  ```
80
86
 
81
87
  ### Making an email
@@ -108,7 +114,55 @@ email.deliver
108
114
  Configuration
109
115
  -------------
110
116
 
111
- TODO...
117
+ Configuration can be done in two ways:
118
+
119
+ ### 1. Configure clients as you need them
120
+
121
+ ``` ruby
122
+ mail_client = Outbox::Clients::MailClient.new(
123
+ delivery_method: :smtp,
124
+ smtp_settings: {}
125
+ )
126
+ sms_client = Outbox::Twilio::Client.new(
127
+ account_sid: 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
128
+ auth_token: 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
129
+ )
130
+ message = Outbox::Message.new do
131
+ email do
132
+ client(mail_client)
133
+ # content...
134
+ end
135
+
136
+ sms do
137
+ client(sms_client)
138
+ # content...
139
+ end
140
+ end
141
+ ```
142
+
143
+ ### 2. Configure a default client for each message type
144
+
145
+ ``` ruby
146
+ Outbox::Messages::Email.default_client(
147
+ :mail,
148
+ delivery_method: :smtp,
149
+ smtp_settings: {}
150
+ )
151
+ Outbox::Messages::SMS.default_client(
152
+ :twilio,
153
+ account_sid: 'ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
154
+ auth_token: 'yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy'
155
+ )
156
+ message = Outbox::Message.new do
157
+ email do
158
+ # content...
159
+ end
160
+
161
+ sms do
162
+ # content...
163
+ end
164
+ end
165
+ ```
112
166
 
113
167
  Contributing
114
168
  ------------
data/lib/outbox.rb CHANGED
@@ -18,5 +18,6 @@ module Outbox
18
18
  module Messages
19
19
  autoload 'Base', 'outbox/messages/base'
20
20
  autoload 'Email', 'outbox/messages/email'
21
+ autoload 'SMS', 'outbox/messages/sms'
21
22
  end
22
23
  end
@@ -11,17 +11,23 @@ module Outbox
11
11
  # Returns the configured delivery method settings. This will also check
12
12
  # the Rails-style #{delivery_method}_settings key as well.
13
13
  #
14
- # client = Outbox::Clients::MailClient.new delivery_method: :sendmail, delivery_method_settings: {
15
- # location: '/usr/bin/sendmail'
16
- # }
14
+ # client = Outbox::Clients::MailClient.new(
15
+ # delivery_method: :sendmail,
16
+ # delivery_method_settings: { location: '/usr/bin/sendmail' }
17
+ # )
17
18
  # client.delivery_method_settings #=> { location: '/usr/bin/sendmail' }
18
19
  #
19
- # client = Outbox::Clients::MailClient.new delivery_method: :sendmail, sendmail_settings: {
20
- # location: '/usr/bin/sendmail'
21
- # }
20
+ # client = Outbox::Clients::MailClient.new(
21
+ # delivery_method: :sendmail,
22
+ # sendmail_settings: { location: '/usr/bin/sendmail' }
23
+ # )
22
24
  # client.delivery_method_settings #=> { location: '/usr/bin/sendmail' }
23
25
  def delivery_method_settings
24
- settings[:delivery_method_settings] || settings[:"#{delivery_method}_settings"] || {}
26
+ (
27
+ settings[:delivery_method_settings] ||
28
+ settings[:"#{delivery_method}_settings"] ||
29
+ {}
30
+ )
25
31
  end
26
32
 
27
33
  def deliver(email)
@@ -14,11 +14,10 @@ module Outbox
14
14
  protected
15
15
 
16
16
  def get_inheritable_module(mod_name)
17
- if const_defined?(mod_name, _search_ancestors = false)
18
- mod = const_get(mod_name)
17
+ if const_defined?(mod_name, false)
18
+ const_get(mod_name)
19
19
  else
20
- mod = const_set(mod_name, Module.new)
21
- include mod
20
+ include const_set(mod_name, Module.new)
22
21
  end
23
22
  end
24
23
  end
@@ -3,6 +3,7 @@ module Outbox
3
3
  include MessageTypes
4
4
 
5
5
  register_message_type :email, Outbox::Messages::Email
6
+ register_message_type :sms, Outbox::Messages::SMS
6
7
 
7
8
  # Use the Outbox::Clients::TestClient for all message types. This is
8
9
  # useful for testing or working an a development environment.
@@ -26,23 +27,24 @@ module Outbox
26
27
  def initialize(message_type_values = nil, &block)
27
28
  if block_given?
28
29
  instance_eval(&block)
29
- else
30
- assign_message_type_values(message_type_values) unless message_type_values.nil?
30
+ elsif message_type_values
31
+ assign_message_type_values(message_type_values)
31
32
  end
32
33
  end
33
34
 
34
35
  # Loops through each registered message type and sets the content body.
35
36
  def body(value)
36
- each_message_type do |message_type, message|
37
+ each_message_type do |_, message|
37
38
  next if message.nil?
38
39
  message.body = value
39
40
  end
40
41
  end
41
- alias :body= :body
42
+ alias_method :body=, :body
42
43
 
43
- # Delivers all of the messages to the given 'audience'. An 'audience' object
44
- # can be a hash or an object that responds to the current message types. Only
45
- # the message types specified in the 'audience' object will be sent to.
44
+ # Delivers all of the messages to the given 'audience'. An 'audience'
45
+ # object can be a hash or an object that responds to the current message
46
+ # types. Only the message types specified in the 'audience' object will
47
+ # be sent to.
46
48
  #
47
49
  # message.deliver email: 'hello@example.com', sms: '+15555555555'
48
50
  # audience = OpenStruct.new
@@ -32,7 +32,7 @@ module Outbox
32
32
  default_client.settings.merge!(settings) if settings
33
33
  default_client.settings
34
34
  end
35
- alias :default_client_settings= :default_client_settings
35
+ alias_method :default_client_settings=, :default_client_settings
36
36
 
37
37
  # Registers a client class with an alias.
38
38
  #
@@ -18,7 +18,7 @@ module Outbox
18
18
  @defaults.merge!(defaults) if defaults
19
19
  @defaults
20
20
  end
21
- alias :defaults= :defaults
21
+ alias_method :defaults=, :defaults
22
22
 
23
23
  # Returns the defined fields for this message type.
24
24
  #
@@ -118,7 +118,7 @@ module Outbox
118
118
  fields(*names)
119
119
  end
120
120
  end
121
- alias :required_field :required_fields
121
+ alias_method :required_field, :required_fields
122
122
 
123
123
  protected
124
124
 
@@ -176,7 +176,7 @@ module Outbox
176
176
  if new_fields.nil?
177
177
  fields = {}
178
178
  self.class.fields.each do |field|
179
- fields[field] = self.public_send(field)
179
+ fields[field] = public_send(field)
180
180
  end
181
181
  fields
182
182
  else
@@ -191,7 +191,7 @@ module Outbox
191
191
  # message.fields #=> { to: 'Bob', from: 'Sally' }
192
192
  def fields=(new_fields)
193
193
  new_fields.each do |field, value|
194
- self.public_send(field, value) if self.respond_to?(field)
194
+ public_send(field, value) if respond_to?(field)
195
195
  end
196
196
  end
197
197
 
@@ -199,9 +199,9 @@ module Outbox
199
199
  # validation issues.
200
200
  def validate_fields
201
201
  self.class.required_fields.each do |field|
202
- value = self.public_send(field)
202
+ value = public_send(field)
203
203
  if value.nil? || value.respond_to?(:empty?) && value.empty?
204
- raise Outbox::MissingRequiredFieldError.new("Missing required field: #{field}")
204
+ raise Outbox::MissingRequiredFieldError, "Missing required field: #{field}"
205
205
  end
206
206
  end
207
207
  end
@@ -31,8 +31,8 @@ module Outbox
31
31
  define_message_type_defaults_accessors(name, message_type)
32
32
  end
33
33
 
34
- # Returns a hash of the registred message types, where the key is the name
35
- # of the message type and the value is the message type class.
34
+ # Returns a hash of the registred message types, where the key is the
35
+ # name of the message type and the value is the message type class.
36
36
  def message_types
37
37
  @message_types ||= {}
38
38
  end
@@ -100,7 +100,7 @@ module Outbox
100
100
  # the value is a hash of options for that message type.
101
101
  def assign_message_type_values(values)
102
102
  values.each do |key, value|
103
- self.public_send(key, value) if self.respond_to?(key)
103
+ public_send(key, value) if respond_to?(key)
104
104
  end
105
105
  end
106
106
 
@@ -108,7 +108,7 @@ module Outbox
108
108
  # of that type on this message.
109
109
  def each_message_type
110
110
  self.class.message_types.each_key do |message_type|
111
- yield message_type, self.public_send(message_type)
111
+ yield message_type, public_send(message_type)
112
112
  end
113
113
  end
114
114
  end
@@ -7,7 +7,10 @@ module Outbox
7
7
  # Make a new message. Every message type can be created using a hash,
8
8
  # block, or direct assignment.
9
9
  #
10
- # message = Email.new to: 'someone@example.com', from: 'company@example.com'
10
+ # message = Email.new(
11
+ # to: 'someone@example.com',
12
+ # from: 'company@example.com'
13
+ # )
11
14
  # message = Email.new do
12
15
  # to 'someone@example.com'
13
16
  # from 'company@example.com'
@@ -17,7 +20,11 @@ module Outbox
17
20
  # message.from = 'company@example.com'
18
21
  def initialize(fields = nil, &block)
19
22
  @fields = {}
20
- @client = self.class.default_client and self.class.default_client.dup
23
+ if self.class.default_client
24
+ @client = self.class.default_client.dup
25
+ else
26
+ @client = nil
27
+ end
21
28
 
22
29
  self.fields = self.class.defaults
23
30
 
@@ -0,0 +1,29 @@
1
+ module Outbox
2
+ module Messages
3
+ # SMS tend to support very different types of parameters, so only
4
+ # the most common ones will be directly applied to the SMS message class.
5
+ # All of the other ones can be set using the arbitrary field access with
6
+ # [] and []=.
7
+ #
8
+ # sms = Outbox::Messages::SMS.new do
9
+ # to '+14155551212'
10
+ # from 'Company Name'
11
+ # body 'Hellow world'
12
+ # end
13
+ # sms.client :twilio, api_key: '...'
14
+ # sms.deliver
15
+ class SMS < Base
16
+ required_fields :to, :from, :body
17
+
18
+ def audience=(audience) # :nodoc:
19
+ case audience
20
+ when String
21
+ self.to = audience
22
+ else
23
+ audience = Outbox::Accessor.new(audience)
24
+ self.to = audience[:to]
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Outbox
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Outbox::Messages::SMS do
4
+ it 'sets common sms fields' do
5
+ sms = Outbox::Messages::SMS.new do
6
+ to '+14155551212'
7
+ from 'Company Name'
8
+ body 'Hello world.'
9
+ end
10
+ expect(sms.to).to eq('+14155551212')
11
+ expect(sms.from).to eq('Company Name')
12
+ expect(sms.body).to eq('Hello world.')
13
+ end
14
+
15
+ describe '#audience=' do
16
+ context 'with a string' do
17
+ it 'sets the To: field' do
18
+ email = Outbox::Messages::SMS.new
19
+ email.audience = '+14155551212'
20
+ expect(email.to).to eq('+14155551212')
21
+ end
22
+ end
23
+
24
+ context 'with a hash' do
25
+ it 'sets multiple fields' do
26
+ email = Outbox::Messages::SMS.new
27
+ email.audience = { to: '+14155551212' }
28
+ expect(email.to).to eq('+14155551212')
29
+ end
30
+ end
31
+
32
+ context 'with an object' do
33
+ it 'sets multiple fields' do
34
+ email = Outbox::Messages::SMS.new
35
+ email.audience = OpenStruct.new to: '+14155551212'
36
+ expect(email.to).to eq('+14155551212')
37
+ end
38
+ end
39
+ end
40
+
41
+ describe '#validate_fields' do
42
+ before do
43
+ @valid_sms = Outbox::Messages::SMS.new do
44
+ to '+14155551212'
45
+ from 'john@gmail.com'
46
+ body 'Hello world.'
47
+ end
48
+ end
49
+
50
+ it 'requires a To: field' do
51
+ @valid_sms.to = nil
52
+ expect {
53
+ @valid_sms.validate_fields
54
+ }.to raise_error(Outbox::MissingRequiredFieldError)
55
+ end
56
+
57
+ it 'requires a From: field' do
58
+ @valid_sms.from = nil
59
+ expect {
60
+ @valid_sms.validate_fields
61
+ }.to raise_error(Outbox::MissingRequiredFieldError)
62
+ end
63
+
64
+ it 'requires a Body: field' do
65
+ @valid_sms.body = nil
66
+ expect {
67
+ @valid_sms.validate_fields
68
+ }.to raise_error(Outbox::MissingRequiredFieldError)
69
+ end
70
+ end
71
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pete Browne
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-01 00:00:00.000000000 Z
11
+ date: 2014-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -106,6 +106,7 @@ files:
106
106
  - lib/outbox/message_types.rb
107
107
  - lib/outbox/messages/base.rb
108
108
  - lib/outbox/messages/email.rb
109
+ - lib/outbox/messages/sms.rb
109
110
  - lib/outbox/version.rb
110
111
  - outbox.gemspec
111
112
  - spec/outbox/accessor_spec.rb
@@ -115,6 +116,7 @@ files:
115
116
  - spec/outbox/message_spec.rb
116
117
  - spec/outbox/messages/base_spec.rb
117
118
  - spec/outbox/messages/email_spec.rb
119
+ - spec/outbox/messages/sms_spec.rb
118
120
  - spec/outbox_spec.rb
119
121
  - spec/spec_helper.rb
120
122
  homepage: https://github.com/localmed/outbox
@@ -137,7 +139,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
137
139
  version: '0'
138
140
  requirements: []
139
141
  rubyforge_project:
140
- rubygems_version: 2.0.3
142
+ rubygems_version: 2.2.2
141
143
  signing_key:
142
144
  specification_version: 4
143
145
  summary: Outbox is a generic interface for sending notifications using a variety of
@@ -150,5 +152,6 @@ test_files:
150
152
  - spec/outbox/message_spec.rb
151
153
  - spec/outbox/messages/base_spec.rb
152
154
  - spec/outbox/messages/email_spec.rb
155
+ - spec/outbox/messages/sms_spec.rb
153
156
  - spec/outbox_spec.rb
154
157
  - spec/spec_helper.rb