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



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

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



111
112
113
114
115
# File 'lib/mandrill/web_hook/event_decorator.rb', line 111

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

#clickObject

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



105
106
107
# File 'lib/mandrill/web_hook/event_decorator.rb', line 105

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
# File 'lib/mandrill/web_hook/event_decorator.rb', line 15

def event_type
  self['event']
end

#headersObject

Returns the headers Hash. Applicable events: inbound



59
60
61
# File 'lib/mandrill/web_hook/event_decorator.rb', line 59

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

#in_reply_toObject

Returns the reply-to ID. Applicable events: inbound



47
48
49
# File 'lib/mandrill/web_hook/event_decorator.rb', line 47

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

#message_body(format = :text) ⇒ Object

Returns the format (:text,:html,:raw) message body. Applicable events: inbound



92
93
94
95
96
97
98
99
100
101
# File 'lib/mandrill/web_hook/event_decorator.rb', line 92

def message_body(format=:text)
  case format
  when :text
    msg['text']
  when :html
    msg['html']
  when :raw
    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.



34
35
36
# File 'lib/mandrill/web_hook/event_decorator.rb', line 34

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.



41
42
43
# File 'lib/mandrill/web_hook/event_decorator.rb', line 41

def message_version
  msg['_version']
end

#msgObject

Returns the msg Hash. Applicable events: all



27
28
29
# File 'lib/mandrill/web_hook/event_decorator.rb', line 27

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

#recipient_emailsObject

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

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

Applicable events: inbound



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

def recipient_emails
  recipients.map(&:first)
end

#recipientsObject

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

[ email, email, .. ]

Applicable events: inbound



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

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

#referencesObject

Returns an array of reference IDs. Applicable events: inbound



53
54
55
# File 'lib/mandrill/web_hook/event_decorator.rb', line 53

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

#sender_emailObject

Returns the email (String) of the sender. Applicable events: inbound



65
66
67
# File 'lib/mandrill/web_hook/event_decorator.rb', line 65

def sender_email
  msg['from_email']
end

#subjectObject

Returns the message subject. Applicable events: all



21
22
23
# File 'lib/mandrill/web_hook/event_decorator.rb', line 21

def subject
  self['subject'] || msg['subject']
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).



72
73
74
# File 'lib/mandrill/web_hook/event_decorator.rb', line 72

def user_email
  msg['email']
end