Class: MandrillAPIDeliveryMethod::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/mandrill_api_delivery_method/delivery_method.rb

Defined Under Namespace

Classes: InvalidOption

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DeliveryMethod

Returns a new instance of DeliveryMethod.

Raises:



9
10
11
12
# File 'lib/mandrill_api_delivery_method/delivery_method.rb', line 9

def initialize(options = {})
  raise InvalidOption, "An API key is required when using the Mandrill API delivery method" if options[:api_key].nil?
  self.settings = options
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.



7
8
9
# File 'lib/mandrill_api_delivery_method/delivery_method.rb', line 7

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mandrill_api_delivery_method/delivery_method.rb', line 14

def deliver!(mail)
  begin
    mandrill = Mandrill::API.new self.settings[:api_key]
    
    message = {
      subject: mail.subject,
      from_email: mail.from.first,
      to: [{
        email: mail.to.first
      }]
    }
    
    if not mail.text_part.nil?
      message[:text] = mail.text_part.body.to_s
    end
    
    if not mail.html_part.nil?
      message[:html] = mail.html_part.body.to_s
    end
  
    async = false
    send_at = mail.deliver_at.nil? ? Time.now.utc.to_s : mail.deliver_at.uts.to_s
  
    result = mandrill.messages.send message, async, nil, send_at
  rescue Mandrill::Error => e
    puts "Error delivering mail to Mandrill API: #{e.class} - #{e.message}"
    raise
  end
end