class Mandrill::WebHook::EventDecorator

Wraps an individual Mandrill web hook event payload, providing some convenience methods handling the payload.

Given a raw event payload Hash, wrap it thus:

JSON.parse(params['mandrill_events']).each do |raw_event|
  event = Mandrill::WebHook::EventDecorator[raw_event]
  ..
end

Public Instance Methods

all_clicks() click to toggle source

Returns an array of all the clicks. Applicable events: click, open

# File lib/mandrill/web_hook/event_decorator.rb, line 159
def all_clicks
  clicks = msg['clicks'] || []
  clicks << click if click and clicks.empty?
  clicks
end
attachments() click to toggle source

Returns an array of Mandrill::WebHook::Attachment objects describing each attached file

[ attachment, attachment, .. ]

Applicable events: inbound

NB: we are throwing away the Mandrill attachments hash keynames at this point, since in practice they are usually the same as the filename. Does this matter? May need to review if other cases are identified.

# File lib/mandrill/web_hook/event_decorator.rb, line 124
def attachments
  (msg['attachments']||{}).map{|attached| Mandrill::WebHook::Attachment[attached.last] }
end
click() click to toggle source

Returns the primary click payload or nil if n/a. Applicable events: click, open

# File lib/mandrill/web_hook/event_decorator.rb, line 153
def click
  { 'ts' => self['ts'], 'url' => self['url'] } if self['ts'] && self['url']
end
entry() click to toggle source

Returns the entry Hash. Applicable events: sync

# File lib/mandrill/web_hook/event_decorator.rb, line 31
def entry
  self['entry']||{}
end
event_type() click to toggle source

Returns the event type. Applicable events: all

# File lib/mandrill/web_hook/event_decorator.rb, line 15
def event_type
  self['event'] || if sync_type.present?
    'sync'
  end
end
headers() click to toggle source

Returns the headers Hash. Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 86
def headers
  msg['headers']||{}
end
images() click to toggle source

Returns an array of Mandrill::WebHook::Image objects describing each attached image

[ image, image, .. ]

Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 131
def images
  (msg['images']||{}).map{|image| Mandrill::WebHook::Image[image.last] }
end
in_reply_to() click to toggle source

Returns the reply-to ID. Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 74
def in_reply_to
  headers['In-Reply-To']
end
message_body(format=nil) click to toggle source

Returns the format (:text,:html,:raw) message body. If format is not specified, it will return the first available message content in order: text, html, raw. Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 138
def message_body(format=nil)
  case format
  when :text
    msg['text']
  when :html
    msg['html']
  when :raw
    msg['raw_msg']
  else
    msg['text'] || msg['html'] || msg['raw_msg']
  end
end
message_id() click to toggle source

Returns the message_id. Inbound events: references 'Message-Id' header. Send/Open/Click events: references '_id' message attribute.

# File lib/mandrill/web_hook/event_decorator.rb, line 56
def message_id
  headers['Message-Id'] || msg['_id'] || self['_id']
end
message_version() click to toggle source

Returns the Mandrill message version. Send/Click events: references '_version' message attribute. Inbound/Open events: n/a.

# File lib/mandrill/web_hook/event_decorator.rb, line 63
def message_version
  msg['_version']
end
metadata() click to toggle source

Returns, if pre-configured, the metadata.

# File lib/mandrill/web_hook/event_decorator.rb, line 68
def metadata
  msg['metadata']||{}
end
msg() click to toggle source

Returns the msg Hash. Applicable events: all

# File lib/mandrill/web_hook/event_decorator.rb, line 49
def msg
  self['msg']||{}
end
recipient_emails() click to toggle source

Returns an array of all unique recipient emails (to/cc)

[ email, email, .. ]

Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 114
def recipient_emails
  recipients.map(&:first)
end
recipients() click to toggle source

Returns an array of all unique recipients (to/cc)

[ [email,name], [email,name], .. ]

Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 107
def recipients
  (Array(msg['to']) | Array(msg['cc'])).compact
end
references() click to toggle source

Returns an array of reference IDs. Applicable events: inbound

# File lib/mandrill/web_hook/event_decorator.rb, line 80
def references
  (headers['References']||'').scan(/(<[^<]+?>)/).flatten
end
reject() click to toggle source

Returns the reject Hash. Applicable events: sync

# File lib/mandrill/web_hook/event_decorator.rb, line 37
def reject
  self['reject']||{}
end
sender_email() click to toggle source

Returns the email (String) of the sender. Inbound messages: references 'from_email' message attribute. Send/Open/Click messages: references 'sender' message attribute.

# File lib/mandrill/web_hook/event_decorator.rb, line 93
def sender_email
  msg['from_email'] || msg['sender'] || entry['sender'] || reject['sender']
end
subject() click to toggle source

Returns the message subject. Applicable events: all

# File lib/mandrill/web_hook/event_decorator.rb, line 43
def subject
  self['subject'] || msg['subject']
end
sync_type() click to toggle source

Returns the sync type. Applicable events: sync

# File lib/mandrill/web_hook/event_decorator.rb, line 23
def sync_type
  if %w(blacklist whitelist).include?(type = self['type'])
    type
  end
end
user_email() click to toggle source

Returns the subject user email address. Inbound messages: references 'email' message attribute (represents the sender). Send/Open/Click messages: references 'email' message attribute (represents the recipient).

# File lib/mandrill/web_hook/event_decorator.rb, line 100
def user_email
  msg['email'] || entry['email'] || reject['email']
end