Class: MultiMail::Sender::Mandrill

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/multi_mail/mandrill/sender.rb

Overview

Mandrill's outgoing mail sender.

Instance Attribute Summary collapse

Attributes included from Base

#settings, #tracking

Instance Method Summary collapse

Methods included from Base

included

Constructor Details

#initialize(options = {}) ⇒ Mandrill

Initializes a Mandrill outgoing email sender.

"content" key for the content to inject

Parameters:

  • options (Hash) (defaults to: {})

    required and optional arguments

Options Hash (options):

  • :api_key (String)

    a Mandrill API key

  • :async (Boolean)

    whether to enable a background sending mode optimized for bulk sending

  • :ip_pool (String)

    the name of the dedicated IP pool that should be used to send the message

  • :send_at (Time, String)

    when this message should be sent

  • :template_name (String)

    the slug or name of a template that exists in the user's Mandrill account

  • :template_content (Array<Hash>)

    an array of hashes, each with a "name" key for the editable region to inject into and a

See Also:



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/multi_mail/mandrill/sender.rb', line 29

def initialize(options = {})
  super
  @api_key = settings.delete(:api_key)
  @async = settings.delete(:async) || false
  @ip_pool = settings.delete(:ip_pool)
  @send_at = settings.delete(: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

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



9
10
11
# File 'lib/multi_mail/mandrill/sender.rb', line 9

def api_key
  @api_key
end

#asyncObject (readonly)

Returns the value of attribute async.



9
10
11
# File 'lib/multi_mail/mandrill/sender.rb', line 9

def async
  @async
end

#ip_poolObject (readonly)

Returns the value of attribute ip_pool.



9
10
11
# File 'lib/multi_mail/mandrill/sender.rb', line 9

def ip_pool
  @ip_pool
end

#send_atObject (readonly)

Returns the value of attribute send_at.



9
10
11
# File 'lib/multi_mail/mandrill/sender.rb', line 9

def send_at
  @send_at
end

#template_contentObject

Returns the value of attribute template_content.



11
12
13
# File 'lib/multi_mail/mandrill/sender.rb', line 11

def template_content
  @template_content
end

#template_nameObject

Returns the value of attribute template_name.



11
12
13
# File 'lib/multi_mail/mandrill/sender.rb', line 11

def template_name
  @template_name
end

Instance Method Details

#deliver!(mail) ⇒ Object

Delivers a message via the Mandrill API.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/multi_mail/mandrill/sender.rb', line 71

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

  api_params = {
    :key     => api_key,
    :message => message,
    :async   => async,
    :ip_pool => ip_pool,
    :send_at => send_at,
  }

  if template_name
    api_method = 'send-template'
    api_params[:template_name] = template_name
    api_params[:template_content] = template_content
  else
    api_method = 'send'
  end

  response = Faraday.post("https://mandrillapp.com/api/1.0/messages/#{api_method}.json", JSON.dump(api_params))

  body = JSON.load(response.body)

  unless response.status == 200
    if body['status'] == 'error'
      case body['name']
      when 'Invalid_Key'
        raise InvalidAPIKey, body['message']
      when 'Unknown_Template'
        raise InvalidTemplate, body['message']
      else
        raise "#{body['name']}: #{body['message']}"
      end
    else
      raise body['message']
    end
  end

  if settings[:return_response]
    body
  else
    self
  end
end

#parametersHash

Returns the additional parameters for the API call.

Returns:

  • (Hash)

    the additional parameters for the API call



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/multi_mail/mandrill/sender.rb', line 45

def parameters
  parameters = settings.dup
  parameters.delete(:return_response)

  [:opens, :clicks].each do |sym|
    if tracking.key?(sym)
      parameter = :"track_#{sym}"
      case tracking[sym]
      when true, false, nil
        parameters[parameter] = tracking[sym]
      when 'yes'
        parameters[parameter] = true
      when 'no'
        parameters[parameter] = false
      end # ignore "htmlonly"
    end
  end

  parameters
end