mandrill_mailer 0.3.2 → 0.3.3

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.
data/README.md CHANGED
@@ -42,11 +42,20 @@ Creating a new Mandrill Mailer is similar to a normal Rails mailer:
42
42
  def invite(invitation)
43
43
  mandrill_mail template: 'Group Invite',
44
44
  subject: I18n.t('invitation_mailer.invite.subject'),
45
- to: {email: invitation.email, name: 'Honored Guest'},
45
+ to: invitation.invitees.map {|invitee| { email: invitee.email, name: invitee.name }},
46
+ # to: {email: invitation.email, name: 'Honored Guest'},
46
47
  vars: {
47
48
  'OWNER_NAME' => invitation.owner_name,
48
- 'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
49
- }
49
+ 'PROJECT_NAME' => invitation.project_name
50
+ },
51
+ recipient_vars: invitation.invitees.map do |invitee| # invitation.invitees is an Array ,
52
+ { invitee.email =>
53
+ {
54
+ 'INVITEE_NAME' => invitee.name,
55
+ 'INVITATION_URL' => new_invitation_url(invitee.email, secret: invitee.secret_code)
56
+ }
57
+ }
58
+ end
50
59
  end
51
60
  end
52
61
  ```
@@ -67,6 +76,10 @@ Creating a new Mandrill Mailer is similar to a normal Rails mailer:
67
76
  email by wrapping them in `*||*` vars: {'OWNER_NAME' => 'Suzy'} is used
68
77
  by doing: `*|OWNER_NAME|*` in the email template within Mandrill
69
78
 
79
+ * `:recipient_vars` - Similar to `:vars`, this is a Hash of merge tags specific to a particular recipient.
80
+ Use this if you are sending batch transactions and hence need to send multiple emails at one go.
81
+ ex. `[{'someone@email.com' => {'INVITEE_NAME' => 'Roger'}}, {'another@email.com' => {'INVITEE_NAME' => 'Tommy'}}]`
82
+
70
83
  * `:template_content` - A Hash of values and content for Mandrill editable content blocks.
71
84
  In MailChimp templates there are editable regions with 'mc:edit' attributes that look
72
85
  a little like: `<div mc:edit="header">My email content</div>` You can insert content directly into
@@ -9,11 +9,20 @@
9
9
  # def invite(invitation)
10
10
  # mandrill_mail template: 'Group Invite',
11
11
  # subject: I18n.t('invitation_mailer.invite.subject'),
12
- # to: {email: invitation.email, name: 'user level 1'},
12
+ # to: invitation.invitees.map {|invitee| { email: invitee.email, name: invitee.name }},
13
+ # # to: { email: invitation.email, name: invitation.recipient_name }
13
14
  # vars: {
14
15
  # 'OWNER_NAME' => invitation.owner_name,
15
- # 'INVITATION_URL' => new_invitation_url(email: invitation.email, secret: invitation.secret)
16
+ # 'PROJECT_NAME' => invitation.project_name
16
17
  # },
18
+ # recipient_vars: invitation.invitees.map do |invitee| # invitation.invitees is an Array
19
+ # { invitee.email =>
20
+ # {
21
+ # 'INVITEE_NAME' => invitee.name,
22
+ # 'INVITATION_URL' => new_invitation_url(invitee.email, secret: invitee.secret_code)
23
+ # }
24
+ # }
25
+ # end,
17
26
  # template_content: {}
18
27
  # end
19
28
  # end
@@ -32,6 +41,10 @@
32
41
  # :vars - A Hash of merge tags made available to the email. Use them in the
33
42
  # email by wrapping them in '*||*' vars: {'OWNER_NAME' => 'Suzy'} is used
34
43
  # by doing: *|OWNER_NAME|* in the email template within Mandrill
44
+ #
45
+ # :recipient_vars - Similar to :vars, this is a Hash of merge tags specific to a particular recipient.
46
+ # Use this if you are sending batch transactions and hence need to send multiple emails at one go.
47
+ # ex. [{'someone@email.com' => {'INVITEE_NAME' => 'Roger'}}, {'another@email.com' => {'INVITEE_NAME' => 'Tommy'}}]
35
48
 
36
49
  # :template_content - A Hash of values and content for Mandrill editable content blocks.
37
50
  # In MailChimp templates there are editable regions with 'mc:edit' attributes that look
@@ -163,7 +176,8 @@ module MandrillMailer
163
176
  # :template - Template name in Mandrill
164
177
  # :subject - Subject of the email
165
178
  # :to - Email to send the mandrill email to
166
- # :vars - Merge vars used in the email for dynamic data
179
+ # :vars - Global merge vars used in the email for dynamic data
180
+ # :recipient_vars - Merge vars used in the email for recipient-specific dynamic data
167
181
  # :bcc - bcc email for the mandrill email
168
182
  # :tags - Tags for the email
169
183
  # :google_analytics_domains - Google analytics domains
@@ -188,6 +202,9 @@ module MandrillMailer
188
202
  # format the :to param to what Mandrill expects if a string or array is passed
189
203
  args[:to] = format_to_params(args[:to])
190
204
 
205
+ # if not provided (nil), set to true (Mandrill default). Otherwise, set to either true or false
206
+ args[:preserve_recipients] = args[:preserve_recipients].nil? ? true : format_boolean(args[:preserve_recipients])
207
+
191
208
  # Set the template name
192
209
  self.template_name = args.delete(:template)
193
210
 
@@ -205,15 +222,10 @@ module MandrillMailer
205
222
  "track_clicks" => true,
206
223
  "auto_text" => true,
207
224
  "url_strip_qs" => true,
225
+ "preserve_recipients" => args[:preserve_recipients],
208
226
  "bcc_address" => args[:bcc],
209
227
  "global_merge_vars" => mandrill_args(args[:vars]),
210
- # "merge_vars" =>[
211
- # {
212
- # "rcpt" => "email@email.com"
213
- # "vars" => {"name" => "VARS", "content" => "vars content"}
214
- # }
215
- # ]
216
-
228
+ "merge_vars" => mandrill_rcpt_args(args[:recipient_vars]),
217
229
  "tags" => args[:tags],
218
230
  "google_analytics_domains" => args[:google_analytics_domains],
219
231
  "google_analytics_campaign" => args[:google_analytics_campaign]
@@ -285,11 +297,25 @@ module MandrillMailer
285
297
 
286
298
  # convert a normal hash into the format mandrill needs
287
299
  def mandrill_args(args)
300
+ return [] unless args
288
301
  args.map do |k,v|
289
302
  {'name' => k, 'content' => v}
290
303
  end
291
304
  end
292
305
 
306
+ def mandrill_rcpt_args(args)
307
+ return [] unless args
308
+ args.map do |item|
309
+ rcpt = item.keys[0]
310
+ {'rcpt' => rcpt, 'vars' => mandrill_args(item.fetch(rcpt))}
311
+ end
312
+ end
313
+
314
+ # ensure only true or false is returned given arg
315
+ def format_boolean(arg)
316
+ arg ? true : false
317
+ end
318
+
293
319
  # handle if to params is an array of either hashes or strings or the single string
294
320
  def format_to_params(to_params)
295
321
  if to_params.kind_of? Array
@@ -1,3 +1,3 @@
1
1
  module MandrillMailer
2
- VERSION = "0.3.2"
2
+ VERSION = "0.3.3"
3
3
  end
@@ -55,6 +55,28 @@ describe MandrillMailer::TemplateMailer do
55
55
  end
56
56
  end
57
57
 
58
+ describe '#mandrill_rcpt_args' do
59
+ let(:rcpt) { 'email@email.com' }
60
+ let(:arg_name) { 'USER_NAME' }
61
+ let(:arg_value) { 'bob' }
62
+
63
+ subject { mailer.send(:mandrill_rcpt_args, [{rcpt => {arg_name => arg_value}}]) }
64
+
65
+ it 'should convert the args to the merge_vars format' do
66
+ should eq [{'rcpt' => rcpt, 'vars' => [{'name' => arg_name, 'content' => arg_value}]}]
67
+ end
68
+ end
69
+
70
+ describe '#format_boolean' do
71
+ it 'only returns true or false' do
72
+ mailer.send(:format_boolean, 1).should eq true
73
+ mailer.send(:format_boolean, '1').should eq true
74
+ mailer.send(:format_boolean, nil).should eq false
75
+ mailer.send(:format_boolean, false).should eq false
76
+ mailer.send(:format_boolean, true).should eq true
77
+ end
78
+ end
79
+
58
80
  describe '#format_to_params' do
59
81
  let(:email) { 'bob@email.com' }
60
82
  let(:name) { 'bob' }
@@ -99,6 +121,8 @@ describe MandrillMailer::TemplateMailer do
99
121
  let(:from_name) { 'Example Name' }
100
122
  let(:var_name) { 'USER_NAME' }
101
123
  let(:var_content) { 'bobert' }
124
+ let(:var_rcpt_name) { 'USER_INFO' }
125
+ let(:var_rcpt_content) { 'boboblacksheep' }
102
126
  let(:to_email) { 'bob@email.com' }
103
127
  let(:to_name) { 'bob' }
104
128
 
@@ -107,9 +131,13 @@ describe MandrillMailer::TemplateMailer do
107
131
  template: 'Email Template',
108
132
  subject: "super secret",
109
133
  to: {'email' => to_email, 'name' => to_name},
134
+ preserve_recipients: false,
110
135
  vars: {
111
136
  var_name => var_content
112
137
  },
138
+ recipient_vars: [
139
+ { to_email => { var_rcpt_name => var_rcpt_content } }
140
+ ],
113
141
  template_content: {template_content_name => template_content_content},
114
142
  headers: {"Reply-To" => "support@email.com"},
115
143
  bcc: 'email@email.com',
@@ -148,8 +176,10 @@ describe MandrillMailer::TemplateMailer do
148
176
  "track_clicks" => true,
149
177
  "auto_text" => true,
150
178
  "url_strip_qs" => true,
179
+ "preserve_recipients" => false,
151
180
  "bcc_address" => args[:bcc],
152
181
  "global_merge_vars" => [{"name" => var_name, "content" => var_content}],
182
+ "merge_vars" => [{"rcpt" => to_email, "vars" => [{"name" => var_rcpt_name, "content" => var_rcpt_content}]}],
153
183
  "tags" => args[:tags],
154
184
  "google_analytics_domains" => args[:google_analytics_domains],
155
185
  "google_analytics_campaign" => args[:google_analytics_campaign]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandrill_mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
12
+ date: 2013-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -139,4 +139,3 @@ test_files:
139
139
  - spec/fake_rails/fake_rails.rb
140
140
  - spec/spec_helper.rb
141
141
  - spec/template_mailer_spec.rb
142
- has_rdoc: