Sha256: 351792a20f29363c9fb0182bbf6e94960444a8a257c4958077d1aca3d560ce1f

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

# 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
#
class Mandrill::WebHook::EventDecorator < Hash

  # Returns the event type
  def event_type
    self['event']
  end

  # Returns the subject
  def subject
    self['subject'] || msg['subject']
  end

  # Returns the msg Hash (as used for inbound messages )
  def msg
    self['msg']||{}
  end

  # Returns the message_id (as used for inbound messages )
  def message_id
    headers['Message-Id']
  end

  # Returns the reply-to ID (as used for inbound messages )
  def in_reply_to
    headers['In-Reply-To']
  end

  # Returns an array of reference IDs (as used for inbound messages )
  def references
    (headers['References']||'').scan(/(<[^<]+?>)/).flatten
  end

  # Returns the headers Hash (as used for inbound messages )
  def headers
    msg['headers']||{}
  end

  # Returns the email (String) of the sender
  def sender_email
    msg['from_email']
  end

  # Returns an array of all unique recipient emails (to/cc)
  #   [ email, email, .. ]
  def recipients
    (Array(msg['to']) | Array(msg['cc'])).compact
  end

  # Returns an array of all unique recipients (to/cc)
  #   [ [email,name], [email,name], .. ]
  def recipient_emails
    recipients.map(&:first)
  end

  # Returns the +format+ (:text,:html,:raw) message body
  def message_body(format=:text)
    case format
    when :text
      msg['text']
    when :html
      msg['html']
    when :raw
      msg['raw_msg']
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mandrill-rails-0.0.1 lib/mandrill/web_hook/event_decorator.rb