Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Adding support for the SendGrid event notification API #30

Closed
wants to merge 2 commits into from

3 participants

@leejarvis

Hi,

I'd like to add support for the SendGrid Event Notification Webhook. This feature is probably beyond the scope of this project which is why there are yet no tests, and no dependency additions. I wanted to put the idea forward with the basic code to discuss before I continued. Otherwise I can move this into a separate gem. It seems a (fairly) natural fit here, though.

This adds an email event processing controller, which uses yajl to parse the incoming JSON body SendGrid sends (I'm using Yajl as it supports parsing streams, and the content received from SendGrid event notifications with batches enabled unfortunately is not valid JSON, it's newline separated JSON objects, using Yajl means it'll work for both batches and single JSON objects).

Would these additions be considered?

@leejarvis

On second thought, this isn't going to work. SendGrid using Content-Type application/json for the request means Rails attempts to parse this JSON at the lowest level, so if a batch event is requested Rails will fail to parse the invalid JSON body.

I'm going to come up with an alternative to get around this issue, I'll leave this open for discussion nonetheless.

@theycallmeswift
Collaborator

@injekt I can put you on the beta list for valid JSON event batches. Email me (swift@sendgrid.com) and include your username.

@theycallmeswift
Collaborator

I'm a fan of including this, but I think my opinion might be slightly bias and it's a step in a different direction than where we were heading before. The distinction we'd have to make is wether griddler is about parsing and consuming inbound emails in Rails or if it's about consuming the SendGrid webhooks (parse & event).

I'll defer to @calebthompson and @jayroh for their opinions.

@jayroh

I think this is cool stuff! But I think that this is something that would be better served in the context of another gem. We've started moving Griddler toward being a library that will handle incoming mail for multiple providers and this, I would say, falls outside of that realm.

Now, if this were "The SendGrid" gem, then I think I would be persuaded otherwise. But that's not the case.

Anyway -- @injekt I see that there are several gem names like gridhook available :).

@theycallmeswift
Collaborator

Define "The SendGrid" gem? Right now, we don't have any officially supported ruby gems. We've been relying on mail and actionmailer to be our workhorses, but I've recently been exploring the opportunity to change that.

@leejarvis

@theycallmeswift I'm assuming @jayroh is talking in theory, a "The SendGrid" gem would handle all kinds of interaction with SendGrid, where Gridder is not designed to do the same thing. That said, there is a sendgrid gem but it's limited to being an outgoing mailer addition rather than a swiss-army type of gem.

Anyway I agree with you guys that this wasn't necessarily the way this library should go and sticking to consuming inbound email is a good choice. I'll create a separate library for this and post back here for anyone interested. Thanks for the discussion.

@leejarvis leejarvis closed this
@leejarvis

https://github.com/injekt/gridhook is a starting point for this. Thanks for the tip on the name, @jayroh

@jayroh

Awesome, guys. I'm happy to see where this went, and is continuing to go. @injekt I'll keep an eye on what you're up to over with gridhook and look forward to trying it out. Thanks again :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 app/controllers/griddler/events_controller.rb
@@ -0,0 +1,6 @@
+class Griddler::EventsController < ActionController::Base
+ def create
+ Griddler::Event.process(request.raw_body)
+ head :ok
+ end
+end
View
4 lib/griddler/configuration.rb
@@ -26,6 +26,10 @@ def processor_class
@processor_class ||= EmailProcessor
end
+ def event_processor_class
+ @event_processor_class ||= EmailEventProcessor
+ end
+
def reply_delimiter
@reply_delimiter ||= 'Reply ABOVE THIS LINE'
end
View
41 lib/griddler/event.rb
@@ -0,0 +1,41 @@
+require 'yajl'
+
+class Griddler::Event
+ attr_reader :attributes
+
+ def self.process(body)
+ parser = Yajl::Parser.new
+ parser.on_parse_complete = proc do |event_attributes|
+ config.event_processor_class.process Event.new(event_attributes)
+ end
+ parser.parse(body)
+ end
+
+ def initialize(attributes)
+ @attributes = attributes
+ end
+
+ def event
+ @attributes[:event]
+ end
+
+ def email
+ @attributes[:email]
+ end
+
+ def timestamp
+ @timestamp ||= Time.at (@attribute[:timestamp] || Time.now).to_i
+ end
+
+ def [](key)
+ @attributes[key]
+ end
+
+ class << self
+ private
+
+ def config
+ Griddler.configuration
+ end
+ end
+end
Something went wrong with that request. Please try again.