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 3e9d3c7..98eb9a9 100644 --- a/app/controllers/sendgrid_postback/events_controller.rb +++ b/app/controllers/sendgrid_postback/events_controller.rb @@ -8,16 +8,23 @@ 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 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? - 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 35a8cc2..c4da858 100644 --- a/lib/sendgrid_postback.rb +++ b/lib/sendgrid_postback.rb @@ -15,10 +15,15 @@ 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 def initialize #@report_exception = proc { |exc| } + enable_interceptor = true + require_ssl = true end end diff --git a/lib/sendgrid_postback/engine.rb b/lib/sendgrid_postback/engine.rb index 0a2cd8c..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 + MailInterceptor.install if SendgridPostback.config.enable_interceptor end end 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