Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
```

Expand All @@ -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
Expand Down
15 changes: 11 additions & 4 deletions app/controllers/sendgrid_postback/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/sendgrid_postback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/sendgrid_postback/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Engine < ::Rails::Engine
end

config.after_initialize do
MailInterceptor.install
MailInterceptor.install if SendgridPostback.config.enable_interceptor
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/sendgrid_postback/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SendgridPostback
VERSION = "0.0.1"
VERSION = "0.0.2"
end