sparkpost_rails 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 925047803a24a8f98ba5f888f81c4a1ad472f9fc
4
- data.tar.gz: 171842fb600f86f2e65143102ccdba962ac80d17
3
+ metadata.gz: 1824c8a4ed0368d9063eadf820ff0e53b8469af7
4
+ data.tar.gz: b7ba7d9806925dfcc2e6f859a484a85cd43e24f2
5
5
  SHA512:
6
- metadata.gz: a783aaf00991597cc6f3b5558db1b43f285e9196cc9cebf22e21f87ba54c5b5cc7cfcc20158cfa1ef9309c70f9ff2da7e6f0fb30f1a6f6f1ae0606bcfa8187ea
7
- data.tar.gz: 6ef6d2888ebf9d6b1864ab77df43aaae54d59abd28ab173621b173b9e3d52877931f6415b27efbda4b9454b730026b1b6804e0f665d9543a084f65caa6ee0d54
6
+ metadata.gz: de4ea6d3e24d6414ec768867816d1ddb3f17a4dbe01994b05fa5ba43a3d53663ca315b5202c0d8d0b918c3d1b0f6b12b86738536f62049f12c2fcd813a0ca8e6
7
+ data.tar.gz: c4431d3186b89221cc0f8270a04b7c698f31a1594d1de39bb8cf3068a2b4f6e999369b22c3244714fc28f2e51359f45d63adbb84a89a8638b73fe210efc3ef51
data/README.md CHANGED
@@ -89,6 +89,17 @@ data returned by the API.
89
89
  SparkPostRails will support multiple recipients, multilple CC, multiple BCC, ReplyTo address, file attachments, inline images, multi-part (HTML and plaintext) messages -
90
90
  all utilizing the standard ActionMailer methodologies.
91
91
 
92
+ Handling Errors
93
+ ---------------
94
+ If you are using `ActiveJob` and wish to do something special when the SparkPost API responds with an error condition you can do so by rescuing these exceptions via `ActionMailer::DeliveryJob`. Simply add an initializer:
95
+
96
+ `config/initializers/action_mailer.rb`
97
+
98
+ ```
99
+ ActionMailer::DeliveryJob.rescue_from(SparkPostRails::DeliveryException) do |exception|
100
+ # do something special with the error
101
+ end
102
+ ```
92
103
 
93
104
  SparkPost Specific Features
94
105
  ---------------------------
@@ -196,14 +207,31 @@ data = { substitution_data: sub_data }
196
207
  mail(to: to_email, subject: "Test", body: "test", sparkpost_data: data)
197
208
  ```
198
209
 
210
+ ### Recipient-specific Data
211
+ When sending to multiple recipients, you can pass an array of data to complement each recipient. Simply pass an array called recipients containing an array of the additional data (e.g. substitution_data).
212
+
213
+ ```
214
+ recipients = ['recipient1@email.com', 'recipient2@email.com']
215
+ sparkpost_data = {
216
+ recipients: [
217
+ { substitution_data: { name: 'Recipient1' } },
218
+ { substitution_data: { name: 'Recipient2' } }
219
+ ]
220
+ }
221
+ mail(to: recipients, sparkpost_data: sparkpost_data)
222
+ ```
223
+
224
+
199
225
  ### Using SparkPost Templates
200
226
  If you would rather leverage SparkPost's powerful templates rather than building ActionMailer views, SparkPostRails can support that as well. Simply
201
- add your template id to the sparkpost_data hash:
227
+ add your template id to the sparkpost_data hash. By default, ActionMailer finds a template to use within views, a workaround to prevent this default action is to explicitly pass a block with an empty text part:
202
228
 
203
229
  ```
204
230
  data = { template_id: "MY-TEMPLATE" }
205
231
 
206
- mail(to: to_email, sparkpost_data: data)
232
+ mail(to: to_email, sparkpost_data: data) do |format|
233
+ format.text { render text: "" }
234
+ end
207
235
  ```
208
236
 
209
237
  **NOTE**: All inline-content that may exist in your mail message will be ignored, as the SparkPost API does not accept that data when a template id is
@@ -14,6 +14,7 @@ module SparkPostRails
14
14
  sparkpost_data = find_sparkpost_data_from mail
15
15
 
16
16
  prepare_recipients_from mail, sparkpost_data
17
+ prepare_recipients_data_from sparkpost_data
17
18
 
18
19
  if sparkpost_data.has_key?(:template_id)
19
20
  prepare_template_content_from sparkpost_data
@@ -95,6 +96,17 @@ module SparkPostRails
95
96
  end
96
97
  end
97
98
 
99
+ # See https://developers.sparkpost.com/api/#/introduction/substitutions-reference/links-and-substitution-expressions-within-substitution-values
100
+ def prepare_recipients_data_from sparkpost_data
101
+ if (recipients_data = sparkpost_data[:recipients])
102
+ @data[:recipients].each_with_index do |recipient, index|
103
+ if (recipient_data = recipients_data[index])
104
+ recipient.merge!(recipient_data)
105
+ end
106
+ end
107
+ end
108
+ end
109
+
98
110
  def prepare_template_content_from sparkpost_data
99
111
  @data[:content][:template_id] = sparkpost_data[:template_id]
100
112
 
@@ -1,4 +1,17 @@
1
1
  module SparkPostRails
2
2
  class DeliveryException < StandardError
3
+ attr_reader :service_message, :service_description, :service_code
4
+
5
+ def initialize(message)
6
+ errors = [*message].first
7
+
8
+ if errors.is_a?(Hash)
9
+ @service_message = errors['message']
10
+ @service_description = errors['description']
11
+ @service_code = errors['code']
12
+ end
13
+
14
+ super(message)
15
+ end
3
16
  end
4
17
  end
@@ -1,4 +1,4 @@
1
1
  module SparkPostRails
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
4
4
 
@@ -0,0 +1,65 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryException do
4
+ subject { SparkPostRails::DeliveryException.new(error) }
5
+
6
+ describe 'string' do
7
+ let(:error) { 'Some delivery error' }
8
+
9
+ it 'preserves original message' do
10
+ begin
11
+ raise subject
12
+ rescue SparkPostRails::DeliveryException => err
13
+ expect(error).to eq(err.message)
14
+ end
15
+ end
16
+ end
17
+
18
+ describe 'array with error details' do
19
+ let(:error) { [{ 'message' => 'Message generation rejected', 'description' => 'recipient address suppressed due to customer policy', 'code' => '1902' }] }
20
+
21
+ it 'assigns message' do
22
+ expect(subject.service_message).to eq('Message generation rejected')
23
+ end
24
+
25
+ it 'assigns description' do
26
+ expect(subject.service_description).to eq('recipient address suppressed due to customer policy')
27
+ end
28
+
29
+ it 'assigns code' do
30
+ expect(subject.service_code).to eq('1902')
31
+ end
32
+
33
+ it 'preserves original message' do
34
+ begin
35
+ raise subject
36
+ rescue SparkPostRails::DeliveryException => err
37
+ expect(error.to_s).to eq(err.message)
38
+ end
39
+ end
40
+ end
41
+
42
+ describe 'array with partial details' do
43
+ let(:error) { [{ 'message' => 'end of world' }] }
44
+
45
+ it 'assigns message' do
46
+ expect(subject.service_message).to eq('end of world')
47
+ end
48
+
49
+ it 'assigns description' do
50
+ expect(subject.service_description).to be nil
51
+ end
52
+
53
+ it 'assigns code' do
54
+ expect(subject.service_code).to be nil
55
+ end
56
+
57
+ it 'preserves original message' do
58
+ begin
59
+ raise subject
60
+ rescue SparkPostRails::DeliveryException => err
61
+ expect(error.to_s).to eq(err.message)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe SparkPostRails::DeliveryMethod do
4
+
5
+ before(:each) do
6
+ @delivery_method = SparkPostRails::DeliveryMethod.new
7
+ end
8
+
9
+ context "Recipient Data" do
10
+
11
+ it "accepts data matching all recipients" do
12
+ recipients = ['recipient1@email.com', 'recipient2@email.com']
13
+ recipients_data = [
14
+ {substitution_data: {name: "Recipient1"}},
15
+ {substitution_data: {name: "Recipient2"}}
16
+ ]
17
+
18
+ test_email = Mailer.test_email to: recipients, sparkpost_data: {recipients: recipients_data}
19
+
20
+ @delivery_method.deliver!(test_email)
21
+
22
+ actual_recipients = @delivery_method.data[:recipients]
23
+ expect(actual_recipients.length).to eq(recipients.length)
24
+ expect(actual_recipients).to eq(recipients.each_with_index.map { |recipient, index| recipients_data[index].merge(address: {email: recipient}) })
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
@@ -23,7 +23,7 @@ class Mailer < ActionMailer::Base
23
23
  def test_email options = {}
24
24
  data = {
25
25
  from: "from@example.com",
26
- to: "to@example.com",
26
+ to: options[:to] || "to@example.com",
27
27
  subject: "Test Email",
28
28
  text_part: "Hello, Testing!"
29
29
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparkpost_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Kimball
@@ -9,22 +9,28 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-04-25 00:00:00.000000000 Z
12
+ date: 2016-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  requirements:
18
- - - "~>"
18
+ - - ">="
19
19
  - !ruby/object:Gem::Version
20
20
  version: '4.0'
21
+ - - "<"
22
+ - !ruby/object:Gem::Version
23
+ version: '5.1'
21
24
  type: :runtime
22
25
  prerelease: false
23
26
  version_requirements: !ruby/object:Gem::Requirement
24
27
  requirements:
25
- - - "~>"
28
+ - - ">="
26
29
  - !ruby/object:Gem::Version
27
30
  version: '4.0'
31
+ - - "<"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
28
34
  - !ruby/object:Gem::Dependency
29
35
  name: rspec
30
36
  requirement: !ruby/object:Gem::Requirement
@@ -76,12 +82,14 @@ files:
76
82
  - spec/click_tracking_spec.rb
77
83
  - spec/delivery_schedule_spec.rb
78
84
  - spec/description_spec.rb
85
+ - spec/exceptions_spec.rb
79
86
  - spec/from_spec.rb
80
87
  - spec/headers_spec.rb
81
88
  - spec/inline_content_spec.rb
82
89
  - spec/inline_css_spec.rb
83
90
  - spec/ip_pool_spec.rb
84
91
  - spec/open_tracking_spec.rb
92
+ - spec/recipient_specific_data_spec.rb
85
93
  - spec/recipients_list_spec.rb
86
94
  - spec/recipients_spec.rb
87
95
  - spec/reply_to_spec.rb
@@ -126,6 +134,7 @@ test_files:
126
134
  - spec/headers_spec.rb
127
135
  - spec/transactional_spec.rb
128
136
  - spec/bcc_recipients_spec.rb
137
+ - spec/exceptions_spec.rb
129
138
  - spec/ip_pool_spec.rb
130
139
  - spec/attachments_spec.rb
131
140
  - spec/cc_recipients_spec.rb
@@ -142,5 +151,6 @@ test_files:
142
151
  - spec/response_spec.rb
143
152
  - spec/open_tracking_spec.rb
144
153
  - spec/sandbox_mode_spec.rb
154
+ - spec/recipient_specific_data_spec.rb
145
155
  - spec/return_path_spec.rb
146
156
  - spec/delivery_schedule_spec.rb