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

Constructor Details

#initialize(options = {}) ⇒ Mandrill

Initializes a Mandrill outgoing email sender.

Parameters:

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

    required and optional arguments

Options Hash (options):

  • :api_key (String)

    a Mandrill API key

See Also:



18
19
20
21
22
23
24
# File 'lib/multi_mail/mandrill/sender.rb', line 18

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)
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

#asyncObject (readonly)

Returns the value of attribute async.



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

def async
  @async
end

#ip_poolObject (readonly)

Returns the value of attribute ip_pool.



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

def ip_pool
  @ip_pool
end

#send_atObject (readonly)

Returns the value of attribute send_at.



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

def send_at
  @send_at
end

Instance Method Details

#deliver!(mail) ⇒ Object

Delivers a message via the Mandrill API.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/multi_mail/mandrill/sender.rb', line 55

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

  response = Faraday.post('https://mandrillapp.com/api/1.0/messages/send.json', JSON.dump({
    :key     => api_key,
    :message => message,
    :async   => async,
    :ip_pool => ip_pool,
    :send_at => send_at,
  }))

  body = JSON.load(response.body)

  unless response.status == 200
    if body['status'] == 'error'
      case body['name']
      when 'Invalid_Key'
        raise InvalidAPIKey, body['message']
      else
        raise 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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/multi_mail/mandrill/sender.rb', line 29

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