Skip to content

Commit

Permalink
add docs and specs for mandrill send-template
Browse files Browse the repository at this point in the history
  • Loading branch information
James McKinney committed Jun 11, 2015
1 parent ed7d807 commit a1cadd0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -331,6 +331,8 @@ You may pass additional arguments to `delivery_method` to use Mandrill-specific
* `async`
* `ip_pool`
* `send_at`
* `template_name`
* `template_content`

```ruby
Mail.deliver do
Expand Down
2 changes: 2 additions & 0 deletions lib/multi_mail.rb
Expand Up @@ -22,6 +22,8 @@ class InvalidRequest < MultiMail::Error; end
class InvalidAPIKey < InvalidRequest; end
# Raise if a message is invalid.
class InvalidMessage < InvalidRequest; end
# Raise if a message template is invalid.
class InvalidTemplate < InvalidRequest; end

# Raise if a message header is invalid
class InvalidHeader < InvalidMessage; end
Expand Down
2 changes: 1 addition & 1 deletion lib/multi_mail/mailgun/sender.rb
Expand Up @@ -17,7 +17,7 @@ class Mailgun
def initialize(options = {})
super
@api_key = settings.delete(:api_key)
@domain = settings.delete(:domain)
@domain = settings.delete(:domain)
end

# Returns the additional parameters for the API call.
Expand Down
30 changes: 17 additions & 13 deletions lib/multi_mail/mandrill/sender.rb
Expand Up @@ -6,7 +6,7 @@ class Mandrill

requires :api_key

attr_reader :api_key, :async, :ip_pool, :send_at
attr_reader :api_key, :async, :ip_pool, :send_at, :template_name, :template_content

# Initializes a Mandrill outgoing email sender.
#
Expand All @@ -16,16 +16,23 @@ class Mandrill
# mode optimized for bulk sending
# @option options [String] :ip_pool the name of the dedicated IP pool that
# should be used to send the message
# @option options [String] :template_name the slug or name of a template
# that exists in the user's Mandrill account
# @option options [Array<Hash>] :template_content an array of hashes, each
# with a `"name"` key for the editable region to inject into and a
# `"content"` key for the content to inject
# @option options [Time,String] :send_at when this message should be sent
# @see https://mandrillapp.com/api/docs/index.ruby.html
# @see https://mandrillapp.com/api/docs/messages.JSON.html#method-send
def initialize(options = {})
super
@api_key = settings.delete(:api_key)
@async = settings.delete(:async) || false
@async = settings.delete(:async) || false
@ip_pool = settings.delete(:ip_pool)
@send_at = settings.delete(:send_at)
unless @send_at.nil? or String === @send_at
@template_name = settings.delete(:template_name)
@template_content = settings.delete(:template_content)
unless @send_at.nil? || String === @send_at
@send_at = @send_at.utc.strftime('%Y-%m-%d %T')
end
end
Expand Down Expand Up @@ -60,9 +67,6 @@ def parameters
# @see https://bitbucket.org/mailchimp/mandrill-api-ruby/src/d0950a6f9c4fac1dd2d5198a4f72c12c626ab149/lib/mandrill/api.rb?at=master#cl-738
# @see https://bitbucket.org/mailchimp/mandrill-api-ruby/src/d0950a6f9c4fac1dd2d5198a4f72c12c626ab149/lib/mandrill.rb?at=master#cl-32
def deliver!(mail)
template_name = settings.delete(:template_name) || false
template_content = settings.delete(:template_content)

message = MultiMail::Message::Mandrill.new(mail).to_mandrill_hash.merge(parameters)

api_params = {
Expand All @@ -74,13 +78,11 @@ def deliver!(mail)
}

if template_name
api_method = "send-template"
api_params.merge!({
template_name: template_name,
template_content: template_content
})
api_method = 'send-template'
api_params[:template_name] = template_name
api_params[:template_content] = template_content
else
api_method = "send"
api_method = 'send'
end

response = Faraday.post("https://mandrillapp.com/api/1.0/messages/#{api_method}.json", JSON.dump(api_params))
Expand All @@ -92,8 +94,10 @@ def deliver!(mail)
case body['name']
when 'Invalid_Key'
raise InvalidAPIKey, body['message']
when 'Unknown_Template'
raise InvalidTemplate, body['message']
else
raise body['message']
raise "#{body['name']}: #{body['message']}"
end
else
raise body['message']
Expand Down
44 changes: 30 additions & 14 deletions spec/mandrill/sender_spec.rb
Expand Up @@ -34,6 +34,13 @@
}.to raise_error(MultiMail::InvalidAPIKey, 'Invalid API key')
end

it 'should raise an error if :template_name is invalid' do
expect{
message.delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :template_name => 'nonexistent'
message.deliver
}.to raise_error(MultiMail::InvalidTemplate, 'No such template "nonexistent"')
end

it 'should transform send_at to a string if it is not a string' do
sender = MultiMail::Sender::Mandrill.new(:api_user => '', :api_key => '', 'send_at' => Time.at(981203696))
sender.send_at.should == '2001-02-03 12:34:56'
Expand All @@ -51,20 +58,26 @@
sender.async.should == false
sender.ip_pool.should == nil
sender.send_at.should == nil
sender.template_name.should == nil
sender.template_content.should == nil
end

it 'should assign custom settings' do
sender = MultiMail::Sender::Mandrill.new({
:api_key => 'xxx',
:async => true,
:async => true,
:ip_pool => 'Main Pool',
:send_at => 'example send_at',
:template_name => 'foo',
:template_content => [{'name' => 'bar', 'content' => 'baz'}],
})

sender.api_key.should == 'xxx'
sender.async.should == true
sender.ip_pool.should == 'Main Pool'
sender.send_at.should == 'example send_at'
sender.template_name.should == 'foo'
sender.template_content.should == [{'name' => 'bar', 'content' => 'baz'}]
end
end

Expand Down Expand Up @@ -119,17 +132,14 @@
end

describe '#deliver!' do
before do
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :return_response => true
end
end

context "when the :tempalte_name param is set" do
context 'when :template_name is set' do
before do
message.delivery_method.settings.merge!({template_name: "default"})
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :return_response => true, :template_name => 'default'
end
end
it "should send a mandrill template" do

it 'should send a mandrill template' do
results = message.deliver!
results.size.should == 1

Expand All @@ -143,7 +153,13 @@
end
end

context "when the :template_name params is not set" do
context 'when :template_name is not set' do
before do
Mail.defaults do
delivery_method MultiMail::Sender::Mandrill, :api_key => ENV['MANDRILL_API_KEY'], :return_response => true
end
end

it 'should send a message' do
results = message.deliver!
results.size.should == 1
Expand All @@ -156,10 +172,10 @@
result['email'].should == 'bit-bucket@test.smtp.org'
result['_id'].should match(/\A[0-9a-f]{32}\z/)
end
end

it 'should not send an empty message' do
empty_message.deliver!.should == [] # response not saved
it 'should not send an empty message' do
empty_message.deliver!.should == [] # response not saved
end
end
end
end

0 comments on commit a1cadd0

Please sign in to comment.