Class: Mandrill::WebHook::EventDecorator
- Inherits:
-
Hash
- Object
- Hash
- Mandrill::WebHook::EventDecorator
- 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
-
#all_clicked_links ⇒ Object
Returns an array of all the urls marked as clicked in this message.
-
#all_clicks ⇒ Object
Returns an array of all the clicks.
-
#attachments ⇒ Object
Returns an array of Mandrill::WebHook::Attachment objects describing each attached file [ attachment, attachment, .. ] Applicable events: inbound.
-
#click ⇒ Object
Returns the primary click payload or nil if n/a.
-
#event_type ⇒ Object
Returns the event type.
-
#headers ⇒ Object
Returns the headers Hash.
-
#in_reply_to ⇒ Object
Returns the reply-to ID.
-
#message_body(format = nil) ⇒ Object
Returns the
format
(:text,:html,:raw) message body. -
#message_id ⇒ Object
Returns the message_id.
-
#message_version ⇒ Object
Returns the Mandrill message version.
-
#metadata ⇒ Object
Returns, if pre-configured, the metadata.
-
#msg ⇒ Object
Returns the msg Hash.
-
#recipient_emails ⇒ Object
Returns an array of all unique recipient emails (to/cc) [ email, email, .. ] Applicable events: inbound.
-
#recipients ⇒ Object
Returns an array of all unique recipients (to/cc) [ [email,name], [email,name], .. ] Applicable events: inbound.
-
#references ⇒ Object
Returns an array of reference IDs.
-
#sender_email ⇒ Object
Returns the email (String) of the sender.
-
#subject ⇒ Object
Returns the message subject.
-
#user_email ⇒ Object
Returns the subject user email address.
Instance Method Details
#all_clicked_links ⇒ Object
Returns an array of all the urls marked as clicked in this message. Applicable events: click, open
138 139 140 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 138 def all_clicked_links all_clicks.collect{|c| c['url'] }.uniq end |
#all_clicks ⇒ Object
Returns an array of all the clicks. Applicable events: click, open
130 131 132 133 134 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 130 def all_clicks clicks = msg['clicks'] || [] clicks << click if click and clicks.empty? clicks end |
#attachments ⇒ Object
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.
102 103 104 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 102 def (msg['attachments']||{}).map{|attached| Mandrill::WebHook::Attachment[attached.last] } end |
#click ⇒ Object
Returns the primary click payload or nil if n/a. Applicable events: click, open
124 125 126 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 124 def click { 'ts' => self['ts'], 'url' => self['url'] } if self['ts'] && self['url'] end |
#event_type ⇒ Object
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 |
#headers ⇒ Object
Returns the headers Hash. Applicable events: inbound
64 65 66 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 64 def headers msg['headers']||{} end |
#in_reply_to ⇒ Object
Returns the reply-to ID. Applicable events: inbound
52 53 54 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 52 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
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 109 def (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 ⇒ Object
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 headers['Message-Id'] || msg['_id'] end |
#message_version ⇒ Object
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 msg['_version'] end |
#metadata ⇒ Object
Returns, if pre-configured, the metadata.
46 47 48 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 46 def msg['metadata']||{} end |
#msg ⇒ Object
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_emails ⇒ Object
Returns an array of all unique recipient emails (to/cc)
[ email, email, .. ]
Applicable events: inbound
92 93 94 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 92 def recipient_emails recipients.map(&:first) end |
#recipients ⇒ Object
Returns an array of all unique recipients (to/cc)
[ [email,name], [email,name], .. ]
Applicable events: inbound
85 86 87 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 85 def recipients (Array(msg['to']) | Array(msg['cc'])).compact end |
#references ⇒ Object
Returns an array of reference IDs. Applicable events: inbound
58 59 60 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 58 def references (headers['References']||'').scan(/(<[^<]+?>)/).flatten end |
#sender_email ⇒ Object
Returns the email (String) of the sender. Inbound messages: references ‘from_email’ message attribute. Send/Open/Click messages: references ‘sender’ message attribute.
71 72 73 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 71 def sender_email msg['from_email']||msg['sender'] end |
#subject ⇒ Object
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_email ⇒ Object
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).
78 79 80 |
# File 'lib/mandrill/web_hook/event_decorator.rb', line 78 def user_email msg['email'] end |