Class: MultiMail::Receiver::Mandrill

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

Instance Method Summary collapse

Methods included from Base

included, #process

Methods inherited from Service

recognized, recognizes, requirements, requires, validate_options

Constructor Details

#initialize(options = {}) ⇒ Mandrill

Returns a new instance of Mandrill.

Parameters:

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

    required and optional arguments

  • opts (Hash)

    a customizable set of options



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

def initialize(options = {})
  super
  @mandrill_api_key = options[:mandrill_api_key]
end

Instance Method Details

#spam?(message) ⇒ Boolean

Returns whether the message is spam.

Parameters:

  • message (Mail::Message)

    a message

Returns:

  • (Boolean)

    whether the message is spam



25
26
27
# File 'lib/multi_mail/mandrill/receiver.rb', line 25

def spam?(message)
  false
end

#transform(params) ⇒ Array<Mail::Message>

TODO:

parse attachments properly

Returns messages.

Parameters:

  • params (Hash)

    the content of Mandrill’s webhook

Returns:

  • (Array<Mail::Message>)

    messages



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/multi_mail/mandrill/receiver.rb', line 32

def transform(params)
  JSON.parse(params['mandrill_events']).map do |event|
    message = Mail.new do
      headers event['msg']['headers'].reject{|k,_| k=='Received'} # @todo

      # The following are redundant with `message-headers`:
      #
      # address = Mail::Address.new event['msg']['from_email']
      # address.display_name = event['msg']['from_name']
      #
      # from    address.format
      # to      event['msg']['to'].flatten.compact
      # subject event['msg']['subject']

      text_part do
        body event['msg']['text']
      end

      html_part do
        content_type 'text/html; charset=UTF-8' # unsure about charset
        body event['msg']['html']
      end
    end

    # Extra Mandrill parameters. Discard `raw_msg`.
    [ 'email',
      'tags',
      'sender',
    ].each do |key|
      if !event['msg'][key].nil? && !event['msg'][key].empty?
        message[key] = event['msg'][key]
      end
    end

    message
  end
end

#valid?(params) ⇒ Boolean

Returns whether the request originates from Mandrill.

Parameters:

  • params (Hash)

    the content of Mandrill’s webhook

Returns:

  • (Boolean)

    whether the request originates from Mandrill



17
18
19
20
21
# File 'lib/multi_mail/mandrill/receiver.rb', line 17

def valid?(params)
  JSON.parse(params['mandrill_events']).all? do |event|
    event.fetch('event') == 'inbound'
  end
end