Module: MandrillClient

Defined in:
lib/mandrill_client.rb

Overview

An interface to the Mandrill API Example usage: MandrillClient.send_template(template_name(string), template_content(array), message(hash))

Class Method Summary collapse

Class Method Details

.base_deliveriesObject

Store the list of mandrill emails that are pending to be sent Only used for testing E.g: expect { some_action }.to change(MandrillClient.base_deliveries,:count).by(1)



11
12
13
# File 'lib/mandrill_client.rb', line 11

def base_deliveries
  @base_deliveries ||= []
end

.clientObject

Return a mandrill client configured with the right API key



22
23
24
# File 'lib/mandrill_client.rb', line 22

def client
  @client ||= Mandrill::API.new(MnoEnterprise.mandrill_key)
end

.deliver(template, from, to, vars = {}, opts = {}) ⇒ Object

A simpler version of send_template

Take in argument:

template: name of a mandrill template
from: hash describing the sender. E.g.: { name: "John", email: "[email protected]" }
to: Array or hash describing the recipient. E.g.: { name: "Jack", email: "[email protected]" }
vars: Mandrill email variables. E.g.: { link: "https://mywebsite.com/confirm_account" }
opts: additional parameters to pass to mandrill. See: https://mandrillapp.com/api/docs/messages.ruby.html


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/mandrill_client.rb', line 45

def deliver(template,from,to,vars = {},opts = {})
  # Prepare message from args
  message = { from_name: from[:name], from_email: from[:email]}
  message[:to] = [to].flatten.map { |t| {name: t[:name], email: t[:email], type: (t[:type] || :to) } }
  message[:global_merge_vars] = vars.map { |k,v| {name: k.to_s, content: v} }
  
  # Merge additional mandrill options
  message.merge!(opts)
  
  self.send_template(template,[],message)
end

.send_template(*args) ⇒ Object

Send the provided template with options MandrillClient.send_template(template_name(string), template_content(array), message(hash))



28
29
30
31
32
33
34
# File 'lib/mandrill_client.rb', line 28

def send_template(*args)
  if self.test?
    self.base_deliveries.push(args)
  else
    self.client.messages.send_template(*args)
  end
end

.test?Boolean

Check whether mailers are in test mode or not Emails should not be sent in test mode

Returns:

  • (Boolean)


17
18
19
# File 'lib/mandrill_client.rb', line 17

def test?
  (Rails.configuration.action_mailer.delivery_method || '').to_sym == :test
end