Class: Mandrill::WebHook::EventDecorator

Inherits:
Hash
  • Object
show all
Defined in:
lib/mandrill/web_hook/event_decorator.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

Returns an array of all the urls marked as clicked in this message. Applicable events: click, open



161
162
163
# File 'lib/mandrill/web_hook/event_decorator.rb', line 161

def all_clicked_links
  all_clicks.collect{|c| c['url'] }.uniq
end

#all_clicksObject

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



153
154
155
156
157
# File 'lib/mandrill/web_hook/event_decorator.rb', line 153

def all_clicks
  clicks = msg['clicks'] || []
  clicks << click if click and clicks.empty?
  clicks
end

#attachmentsObject

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.



118
119
120
# File 'lib/mandrill/web_hook/event_decorator.rb', line 118

def attachments
  (msg['attachments']||{}).map{|attached| Mandrill::WebHook::Attachment[attached.last] }
end

#clickObject

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



147
148
149
# File 'lib/mandrill/web_hook/event_decorator.rb', line 147

def click
  { 'ts' => self['ts'], 'url' => self['url'] } if self['ts'] && self['url']
end

#event_typeObject

Returns the event type. Applicable events: all



15
16
17
18
19
# File 'lib/mandrill/web_hook/event_decorator.rb', line 15

def event_type
  self['event'] || if sync_type.present?
    'sync'
  end
end

#headersObject

Returns the headers Hash. Applicable events: inbound



80
81
82
# File 'lib/mandrill/web_hook/event_decorator.rb', line 80

def headers
  msg['headers']||{}
end

#imagesObject

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

[ image, image, .. ]

Applicable events: inbound



125
126
127
# File 'lib/mandrill/web_hook/event_decorator.rb', line 125

def images
  (msg['images']||{}).map{|image| Mandrill::WebHook::Image[image.last] }
end

#in_reply_toObject

Returns the reply-to ID. Applicable events: inbound



68
69
70
# File 'lib/mandrill/web_hook/event_decorator.rb', line 68

def in_reply_to
  headers['In-Reply-To']
end

#message_body(format = nil) ⇒ Object

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



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/mandrill/web_hook/event_decorator.rb', line 132

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_idObject

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



50
51
52
# File 'lib/mandrill/web_hook/event_decorator.rb', line 50

def message_id
  headers['Message-Id'] || msg['_id']
end

#message_versionObject

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



57
58
59
# File 'lib/mandrill/web_hook/event_decorator.rb', line 57

def message_version
  msg['_version']
end

#metadataObject

Returns, if pre-configured, the metadata.



62
63
64
# File 'lib/mandrill/web_hook/event_decorator.rb', line 62

def 
  msg['metadata']||{}
end

#msgObject

Returns the msg Hash. Applicable events: all



43
44
45
# File 'lib/mandrill/web_hook/event_decorator.rb', line 43

def msg
  self['msg']||{}
end

#recipient_emailsObject

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

[ email, email, .. ]

Applicable events: inbound



108
109
110
# File 'lib/mandrill/web_hook/event_decorator.rb', line 108

def recipient_emails
  recipients.map(&:first)
end

#recipientsObject

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

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

Applicable events: inbound



101
102
103
# File 'lib/mandrill/web_hook/event_decorator.rb', line 101

def recipients
  (Array(msg['to']) | Array(msg['cc'])).compact
end

#referencesObject

Returns an array of reference IDs. Applicable events: inbound



74
75
76
# File 'lib/mandrill/web_hook/event_decorator.rb', line 74

def references
  (headers['References']||'').scan(/(<[^<]+?>)/).flatten
end

#rejectObject

Returns the reject Hash. Applicable events: sync



31
32
33
# File 'lib/mandrill/web_hook/event_decorator.rb', line 31

def reject
  self['reject']||{}
end

#sender_emailObject

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



87
88
89
# File 'lib/mandrill/web_hook/event_decorator.rb', line 87

def sender_email
  msg['from_email'] || msg['sender'] || reject['sender']
end

#subjectObject

Returns the message subject. Applicable events: all



37
38
39
# File 'lib/mandrill/web_hook/event_decorator.rb', line 37

def subject
  self['subject'] || msg['subject']
end

#sync_typeObject

Returns the sync type. Applicable events: sync



23
24
25
26
27
# 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_emailObject

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



94
95
96
# File 'lib/mandrill/web_hook/event_decorator.rb', line 94

def user_email
  msg['email'] || reject['email']
end