From 0b866ce0a9fbe307153d4853857e42bf614c92e4 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Mon, 30 Jul 2012 16:44:15 -0400 Subject: [PATCH 1/7] Allow disable interceptor via config --- lib/sendgrid_postback.rb | 1 + lib/sendgrid_postback/engine.rb | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/sendgrid_postback.rb b/lib/sendgrid_postback.rb index 35a8cc2..e179e9e 100644 --- a/lib/sendgrid_postback.rb +++ b/lib/sendgrid_postback.rb @@ -16,6 +16,7 @@ class Config attr_accessor :report_exception attr_accessor :find_receiver_by_uuid attr_accessor :request_path + attr_accessor :disable_interceptor def initialize #@report_exception = proc { |exc| } diff --git a/lib/sendgrid_postback/engine.rb b/lib/sendgrid_postback/engine.rb index 0a2cd8c..e58cb44 100644 --- a/lib/sendgrid_postback/engine.rb +++ b/lib/sendgrid_postback/engine.rb @@ -10,7 +10,7 @@ class Engine < ::Rails::Engine end config.after_initialize do - MailInterceptor.install + MailInterceptor.install unless config.disable_interceptor end end From 39a7842a8e13eae112180314ff535f77a8de4072 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Mon, 30 Jul 2012 16:57:04 -0400 Subject: [PATCH 2/7] Enable interceptor rather than disabling it --- lib/sendgrid_postback.rb | 3 ++- lib/sendgrid_postback/engine.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/sendgrid_postback.rb b/lib/sendgrid_postback.rb index e179e9e..26ee0ce 100644 --- a/lib/sendgrid_postback.rb +++ b/lib/sendgrid_postback.rb @@ -16,10 +16,11 @@ class Config attr_accessor :report_exception attr_accessor :find_receiver_by_uuid attr_accessor :request_path - attr_accessor :disable_interceptor + attr_accessor :enable_interceptor def initialize #@report_exception = proc { |exc| } + enable_interceptor = true end end diff --git a/lib/sendgrid_postback/engine.rb b/lib/sendgrid_postback/engine.rb index e58cb44..ad513f3 100644 --- a/lib/sendgrid_postback/engine.rb +++ b/lib/sendgrid_postback/engine.rb @@ -10,7 +10,7 @@ class Engine < ::Rails::Engine end config.after_initialize do - MailInterceptor.install unless config.disable_interceptor + MailInterceptor.install unless SendgridPostback.config.enable_interceptor end end From c4e907fa974673b870b27e6491d7ecb7d3ed5e27 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Mon, 30 Jul 2012 16:57:54 -0400 Subject: [PATCH 3/7] Change unless to if --- lib/sendgrid_postback/engine.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid_postback/engine.rb b/lib/sendgrid_postback/engine.rb index ad513f3..36ec84f 100644 --- a/lib/sendgrid_postback/engine.rb +++ b/lib/sendgrid_postback/engine.rb @@ -10,7 +10,7 @@ class Engine < ::Rails::Engine end config.after_initialize do - MailInterceptor.install unless SendgridPostback.config.enable_interceptor + MailInterceptor.install if SendgridPostback.config.enable_interceptor end end From 28126fdaf1952b68c2b780277ad02b2a537e0faf Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Mon, 30 Jul 2012 16:59:54 -0400 Subject: [PATCH 4/7] Version bump --- lib/sendgrid_postback/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/sendgrid_postback/version.rb b/lib/sendgrid_postback/version.rb index bd3406d..25d5915 100644 --- a/lib/sendgrid_postback/version.rb +++ b/lib/sendgrid_postback/version.rb @@ -1,3 +1,3 @@ module SendgridPostback - VERSION = "0.0.1" + VERSION = "0.0.2" end From 9356c36f9c02984fa9bf6e25904990199d325987 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Mon, 30 Jul 2012 19:18:05 -0400 Subject: [PATCH 5/7] Add a setting to toggle the SSL requirement --- app/controllers/sendgrid_postback/events_controller.rb | 2 +- lib/sendgrid_postback.rb | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/sendgrid_postback/events_controller.rb b/app/controllers/sendgrid_postback/events_controller.rb index 3e9d3c7..dcc8aa2 100644 --- a/app/controllers/sendgrid_postback/events_controller.rb +++ b/app/controllers/sendgrid_postback/events_controller.rb @@ -8,7 +8,7 @@ class SendgridPostback::EventsController < ActionController::Metal # curl -i -H "Content-Type: application/json" -X POST -d '{"email": "test@gmail.com", "event": "processed"}{"email": "test2@gmail.com", "event": "processed2"}' https://localhost:3000/sendgrid_postback/events def create SendgridPostback.logger.info "#{self.class}#create event params: #{params[:events].inspect}" - unless request.ssl? + if SendgridPostback.config.require_ssl && !request.ssl? self.response_body = '' self.status = :bad_request return diff --git a/lib/sendgrid_postback.rb b/lib/sendgrid_postback.rb index 26ee0ce..259bc98 100644 --- a/lib/sendgrid_postback.rb +++ b/lib/sendgrid_postback.rb @@ -17,10 +17,12 @@ class Config attr_accessor :find_receiver_by_uuid attr_accessor :request_path attr_accessor :enable_interceptor + attr_accessor :require_ssl def initialize #@report_exception = proc { |exc| } enable_interceptor = true + require_ssl = true end end From b3781bd98715ed02395906219c0100f375a48df2 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Tue, 7 Aug 2012 16:24:05 -0400 Subject: [PATCH 6/7] Added generic event receiver, updated read me --- README.md | 19 +++++++++++++++++-- .../sendgrid_postback/events_controller.rb | 8 +++++++- lib/sendgrid_postback.rb | 1 + 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 50a6687..c6e3762 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ SendgridPostback is a Rails Engine which integrates with the [SendGrid](http://s It includes a MailInterceptor which will attach a UUID header in all mails before they are sent. When properly configured, SendGrid will then post events for each message to your app. You'll -know when emails are delivered, bounced, delayed, clicked, etc., according to your SendGrid +know when emails are delivered, bounced, delayed, clicked, etc., according to your SendGrid account configuration. Note that for performance reasons, you'll probably want to configure your Event API to batch events. @@ -35,11 +35,26 @@ SendgridPostback.configure do |config| # proc that accepts an exception for reporting config.report_exception = proc { |exc| ... } # Optional + # If you're creating Sendgrid UUID yourself, disable the interceptor + config.enable_interceptor = false + + # Allow the controller to recieve data over non-ssl connections + config.require_ssl = false + # Required proc that returns an instance for the given uuid. # The class should mix in SendgridPostback::EventReceiver config.find_receiver_by_uuid = proc do |uuid| Notification.find_by_uuid(uuid) # for example end + + # Sendgrid will post all events to your receiver URL. If emails are sent + # via Sendgrid outside of this application their events will still post back here. + # You can specify a model here. + # Add a post_general_sendgrid_event(data) method to handle receiving the data as + # you please. + config.get_general_event_receiver = proc do + SendgridReport.new + end end ``` @@ -51,7 +66,7 @@ Add the route manually to your routes.rb at the top level, so the declaration ca ## Usage -Your app should have a class, i.e. an ActiveRecord model, that mixes in SendgridPostback::EventReceiver. +Your app should have a class, i.e. an ActiveRecord model, that mixes in SendgridPostback::EventReceiver. This module adds attributes that should be persisted, `sendgrid_events` and `sendgrid_state`. ```ruby diff --git a/app/controllers/sendgrid_postback/events_controller.rb b/app/controllers/sendgrid_postback/events_controller.rb index dcc8aa2..985e75b 100644 --- a/app/controllers/sendgrid_postback/events_controller.rb +++ b/app/controllers/sendgrid_postback/events_controller.rb @@ -17,7 +17,13 @@ def create parse_send_grid_events do |data| receiver = SendgridPostback.config.find_receiver_by_uuid.call(data[:uuid]) if receiver.blank? - SendgridPostback.config.report_exception.call("SendgridPostback postback: Notification UUID(#{data[:uuid]}) not found.") + general_receiver = SendgridPostback.config.get_general_event_receiver.call + + if general_receiver.blank? + SendgridPostback.config.report_exception.call("SendgridPostback postback: Notification UUID(#{data[:uuid]}) not found.") + else + general_receiver.send(:post_general_sendgrid_event, data) + end else receiver.send(:post_sendgrid_event, data) end diff --git a/lib/sendgrid_postback.rb b/lib/sendgrid_postback.rb index 259bc98..c4da858 100644 --- a/lib/sendgrid_postback.rb +++ b/lib/sendgrid_postback.rb @@ -15,6 +15,7 @@ class Config attr_accessor :logger attr_accessor :report_exception attr_accessor :find_receiver_by_uuid + attr_accessor :get_general_event_receiver attr_accessor :request_path attr_accessor :enable_interceptor attr_accessor :require_ssl From fbc58d38806eaa744cc32a8d09b576a829a53572 Mon Sep 17 00:00:00 2001 From: Barry Jones Date: Tue, 7 Aug 2012 17:11:24 -0400 Subject: [PATCH 7/7] Don't call receiver by uuid unless uuid is included --- app/controllers/sendgrid_postback/events_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/sendgrid_postback/events_controller.rb b/app/controllers/sendgrid_postback/events_controller.rb index 985e75b..98eb9a9 100644 --- a/app/controllers/sendgrid_postback/events_controller.rb +++ b/app/controllers/sendgrid_postback/events_controller.rb @@ -14,8 +14,9 @@ def create return end - parse_send_grid_events do |data| - receiver = SendgridPostback.config.find_receiver_by_uuid.call(data[:uuid]) + parse_send_grid_events do |data| + receiver = SendgridPostback.config.find_receiver_by_uuid.call(data[:uuid]) if data[:uuid] + if receiver.blank? general_receiver = SendgridPostback.config.get_general_event_receiver.call