Skip to content

Instantly share code, notes, and snippets.

@seth-macpherson
Created October 2, 2012 18:40
Show Gist options
  • Save seth-macpherson/3822214 to your computer and use it in GitHub Desktop.
Save seth-macpherson/3822214 to your computer and use it in GitHub Desktop.
SendGrid Event Webhooks Controller
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