Created
October 2, 2012 18:40
-
-
Save seth-macpherson/3822214 to your computer and use it in GitHub Desktop.
SendGrid Event Webhooks Controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SendGridExampleController < ApplicationController | |
# code excerpt ... | |
def parse_event | |
unless params[:event].present? | |
result = {:msg => ABORTED, :success => false} | |
end | |
result = {:msg => "Parse #{params[:event]}", :success => true} | |
begin | |
case params[:event] | |
when "bounce" | |
if Set.new(["status","reason","type","email"]).subset? params.keys.to_set | |
# update the corresponding User record as bounced | |
else | |
result = {:msg => INVALID, :keys => params.keys} | |
end | |
when "spamreport", "blocked" | |
if Set.new(["timestamp","event","email"]).subset? params.keys.to_set | |
# update the corresponding User record as unsubscribed | |
else | |
result = {:msg => INVALID, :keys => params.keys} | |
end | |
else | |
result = {:msg => "Ignored #{params[:event]}"} | |
end | |
rescue Exception => e | |
# log the incoming params to a yaml file so we can add another test case if necessary | |
# notify us of the error, then tell SendGrid not to retry | |
ensure | |
# result[:success] = false unless result.is_a?(Hash) && result.has_key?(:success) | |
render :text => result.to_json, :layout => false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment