sendgrid_events_pbxx 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/.gitignore +6 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +4 -0
  4. data/Gemfile +6 -0
  5. data/Gemfile.lock +154 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +37 -0
  8. data/Rakefile +13 -0
  9. data/app/controllers/sendgrid_events/sendgrid_events_controller.rb +8 -0
  10. data/app/models/sendgrid_events/sendgrid_email_record.rb +26 -0
  11. data/lib/generators/sendgrid_events/install/templates/create_sendgrid_email_records.rb +16 -0
  12. data/lib/generators/sendgrid_events/install/templates/initializer.rb +8 -0
  13. data/lib/generators/sendgrid_events/install/templates/routes.rb +2 -0
  14. data/lib/generators/sendgrid_events/install_generator.rb +55 -0
  15. data/lib/sendgrid_events/action_mailer_override.rb +29 -0
  16. data/lib/sendgrid_events/configure.rb +27 -0
  17. data/lib/sendgrid_events/engine.rb +12 -0
  18. data/lib/sendgrid_events/handlers/base.rb +50 -0
  19. data/lib/sendgrid_events/handlers/bounced.rb +7 -0
  20. data/lib/sendgrid_events/handlers/clicked.rb +7 -0
  21. data/lib/sendgrid_events/handlers/deferred.rb +7 -0
  22. data/lib/sendgrid_events/handlers/delivered.rb +10 -0
  23. data/lib/sendgrid_events/handlers/dispatch.rb +34 -0
  24. data/lib/sendgrid_events/handlers/dropped.rb +7 -0
  25. data/lib/sendgrid_events/handlers/opened.rb +7 -0
  26. data/lib/sendgrid_events/handlers/processed.rb +7 -0
  27. data/lib/sendgrid_events/handlers/spam_reported.rb +7 -0
  28. data/lib/sendgrid_events/handlers/unsubscribed.rb +7 -0
  29. data/lib/sendgrid_events/handlers.rb +3 -0
  30. data/lib/sendgrid_events/middleware.rb +22 -0
  31. data/lib/sendgrid_events/receiver.rb +18 -0
  32. data/lib/sendgrid_events/version.rb +3 -0
  33. data/lib/sendgrid_events.rb +9 -0
  34. data/script/rails +8 -0
  35. data/sendgrid_events.gemspec +28 -0
  36. data/spec/sendgrid_events/action_mailer_override_spec.rb +42 -0
  37. data/spec/sendgrid_events/handlers/base_spec.rb +47 -0
  38. data/spec/sendgrid_events/handlers/bounced_spec.rb +31 -0
  39. data/spec/sendgrid_events/handlers/clicked_spec.rb +31 -0
  40. data/spec/sendgrid_events/handlers/deferred_spec.rb +31 -0
  41. data/spec/sendgrid_events/handlers/delivered_spec.rb +31 -0
  42. data/spec/sendgrid_events/handlers/dispatch_spec.rb +8 -0
  43. data/spec/sendgrid_events/handlers/dropped_spec.rb +31 -0
  44. data/spec/sendgrid_events/handlers/opened_spec.rb +31 -0
  45. data/spec/sendgrid_events/handlers/processed_spec.rb +31 -0
  46. data/spec/sendgrid_events/handlers/spam_reported_spec.rb +31 -0
  47. data/spec/sendgrid_events/handlers/unsubscribed_spec.rb +31 -0
  48. data/spec/sendgrid_events/receiver_spec.rb +24 -0
  49. data/spec/spec_helper.rb +42 -0
  50. metadata +276 -0
data/script/rails ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
5
+ ENGINE_PATH = File.expand_path('../../lib/sendgrid_events/engine', __FILE__)
6
+
7
+ require 'rails/all'
8
+ require 'rails/engine/commands'
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sendgrid_events/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jon Phenow", "Russen Guggemos"]
6
+ gem.email = ["jon.phenow@tstmedia.com","russen@promoboxx.com"]
7
+ gem.description = %q{}
8
+ gem.summary = %q{}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "sendgrid_events_pbxx"
15
+ gem.version = SendgridEvents::VERSION
16
+
17
+ gem.add_development_dependency 'rake'
18
+ gem.add_development_dependency 'awesome_print'
19
+ gem.add_development_dependency 'rspec-rails', '~> 2'
20
+ gem.add_development_dependency 'capybara'
21
+ gem.add_development_dependency 'rails', '~> 3.2'
22
+ gem.add_development_dependency 'factory_girl_rails', '~> 1.7'
23
+ gem.add_development_dependency 'sqlite3'
24
+
25
+ gem.add_dependency 'rails', '~> 3.2'
26
+ gem.add_dependency 'sendgrid-rails', '~> 2.0'
27
+ gem.add_dependency 'enumerated_field'
28
+ end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ class TestMailer < ActionMailer::Base
4
+ def test_mail
5
+ sendgrid_email_record_attributes user_id: 42
6
+ mail(to: "to@example.com", from: "from@example.com", subject: "Test Mail")
7
+ end
8
+ end
9
+
10
+ module SendgridEvents
11
+ describe ActionMailerOverride do
12
+ let(:recorded_email) { SendgridEvents::SendgridEmailRecord.last }
13
+
14
+
15
+ describe "records the sent email" do
16
+ before { @mail = TestMailer.test_mail }
17
+ specify { recorded_email.to.should == "to@example.com" }
18
+ specify { recorded_email.from.should == "from@example.com" }
19
+ specify { recorded_email.subject.should == "Test Mail" }
20
+ specify { recorded_email.status.should == 'processing' }
21
+ end
22
+
23
+ describe "stores new event record's id in the uniq args" do
24
+ before { @mail = TestMailer.test_mail }
25
+ let(:sendgrid_header) { @mail.instance_variable_get(:@sendgrid_header) }
26
+
27
+ specify { sendgrid_header.data[:unique_args].should == { sendgrid_events_id: recorded_email.id} }
28
+ end
29
+
30
+ describe "passes additional attributes to SendgridEmailRecord" do
31
+ let(:email_record) { stub.as_null_object }
32
+
33
+ specify do
34
+ SendgridEmailRecord.should_receive(:create_from_headers!).with do |_, additional_attributes|
35
+ additional_attributes.should == { user_id: 42 }
36
+ end.and_return(email_record)
37
+
38
+ TestMailer.test_mail
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Base do
5
+ subject { Base }
6
+ it { should respond_to(:handle) }
7
+ it { should_not respond_to(:handlee) }
8
+ it { should respond_to(:registered_handlers) }
9
+ it { should respond_to(:selected_handlers) }
10
+ it { should respond_to(:choose_and_handle) }
11
+ it { should respond_to(:register_as_handler_for) }
12
+ let(:event) { {
13
+ :email => "foo@bar.com",
14
+ :timestamp => 1322000095,
15
+ :sendgrid_events_id => '7',
16
+ :event => "delivered"
17
+ } }
18
+
19
+ it "returns the list of handlers" do
20
+ subject.registered_handlers.keys.sort.should == Dispatch.acceptable_handlers.keys.sort
21
+ end
22
+
23
+ it "chose a handler if handler is registered" do
24
+ subject.stub(:selected_handlers).and_return({ "delivered" => true })
25
+ Delivered.should_receive(:handle).with(event)
26
+ subject.choose_and_handle(event)
27
+ end
28
+
29
+ it "doesn't handle a non-selected handler" do
30
+ subject.stub(:selected_handlers).and_return({})
31
+ Delivered.should_not_receive(:handle).with(event)
32
+ subject.choose_and_handle(event)
33
+ end
34
+
35
+ it "doesn't handle a non-registered handler" do
36
+ subject.stub(:selected_handlers).and_return({ "delivered" => true })
37
+ subject.stub(:registered_handlers).and_return({})
38
+ Delivered.should_not_receive(:handle).with(event)
39
+ subject.choose_and_handle(event)
40
+ end
41
+
42
+ it "doesn't regitser a new handle with unacceptable handle" do
43
+ expect { subject.register_as_handler_for(:winning) }.to raise_error(ArgumentError)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Bounced do
5
+ subject { Bounced }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'bounced' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "bounce"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Clicked do
5
+ subject { Clicked }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'clicked' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "click"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Deferred do
5
+ subject { Deferred }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'deferred' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "deferred"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Delivered do
5
+ subject { Delivered }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'delivered' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "delivered"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Dispatch do
5
+ subject { Dispatch }
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Dropped do
5
+ subject { Dropped }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'dropped' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "dropped"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Opened do
5
+ subject { Opened }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'opened' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "open"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Processed do
5
+ subject { Processed }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'processed' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "processed"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe SpamReported do
5
+ subject { SpamReported }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'spamreported' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "spamreport"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ module Handlers
4
+ describe Unsubscribed do
5
+ subject { Unsubscribed }
6
+ it { should respond_to(:handle) }
7
+ its(:handlee) { should == 'unsubscribed' }
8
+ let(:event) { {
9
+ :email => "foo@bar.com",
10
+ :timestamp => 1322000095,
11
+ :sendgrid_events_id => '7',
12
+ :event => "unsubscribe"
13
+ } }
14
+
15
+ it "updates event, but doesn't call handler without one set" do
16
+ subject.should_receive(:find_and_update_event).with(event).once
17
+ subject.should_receive(:handler).never
18
+ subject.handle(event)
19
+ end
20
+
21
+ it "updates event, but doesn't call handler without one set" do
22
+ subject.handler do
23
+ true
24
+ end
25
+ subject.stub(:find_and_update_event => true)
26
+ subject.should_receive(:handler).once
27
+ subject.handle(event)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+ module SendgridEvents
3
+ describe Receiver do
4
+ before do
5
+ Configure.config do
6
+ foreign_key 'sendgrid_events_id'
7
+ end
8
+ end
9
+ subject { Receiver }
10
+ it { should respond_to(:receive) }
11
+
12
+ let(:params) {
13
+ { "_json" => [
14
+ {"email" => "foo@bar.com", "timestamp" => 1322000095,"sendgrid_events_id" => "7", "event" => "delivered"},
15
+ {"email" => "foo@bar.com", "timestamp" => 1322000096, "sendgrid_events_id" => "4", "event" => "delivered"}
16
+ ] }
17
+ }
18
+
19
+ it "sends the poorly composed params to be cleaned then handles" do
20
+ Handlers::Base.should_receive(:choose_and_handle).exactly(2).times
21
+ subject.receive params
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'action_mailer'
8
+ require 'rails'
9
+ require 'sqlite3'
10
+ require 'active_record'
11
+ require 'enumerated_field'
12
+
13
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
14
+ ActiveRecord::Schema.define do
15
+ create_table :sendgrid_email_records, :force => true do |t|
16
+ t.string :status
17
+ t.string :to
18
+ t.string :from
19
+ t.string :subject
20
+
21
+ t.timestamps
22
+ end
23
+ end
24
+
25
+ Dir.glob(File.expand_path('../../app/models/**/*.rb', __FILE__)).each { |file| require file }
26
+ require 'sendgrid_events'
27
+
28
+ ActionMailer::Base.delivery_method = :test
29
+ ActionMailer::Base.register_interceptor(SendGrid::MailInterceptor)
30
+
31
+ RSpec.configure do |config|
32
+
33
+ config.treat_symbols_as_metadata_keys_with_true_values = true
34
+ config.run_all_when_everything_filtered = true
35
+ config.filter_run :focus
36
+
37
+ # Run specs in random order to surface order dependencies. If you find an
38
+ # order dependency and want to debug it, you can fix the order by providing
39
+ # the seed, which is printed after each run.
40
+ # --seed 1234
41
+ config.order = 'random'
42
+ end