special-delivery 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +3 -0
  4. data/Rakefile +23 -0
  5. data/app/assets/javascripts/special_delivery/application.js +13 -0
  6. data/app/assets/javascripts/special_delivery/events.js +2 -0
  7. data/app/assets/stylesheets/special_delivery/application.css +13 -0
  8. data/app/assets/stylesheets/special_delivery/events.css +4 -0
  9. data/app/controllers/special_delivery/application_controller.rb +4 -0
  10. data/app/controllers/special_delivery/events_controller.rb +35 -0
  11. data/app/models/special_delivery/delivery.rb +18 -0
  12. data/app/views/layouts/special_delivery/application.html.erb +14 -0
  13. data/config/routes.rb +3 -0
  14. data/db/migrate/20131111152047_create_special_delivery_deliveries.rb +12 -0
  15. data/lib/special_delivery/authenticator.rb +37 -0
  16. data/lib/special_delivery/base_event.rb +23 -0
  17. data/lib/special_delivery/bounce_event.rb +10 -0
  18. data/lib/special_delivery/callback.rb +40 -0
  19. data/lib/special_delivery/click_event.rb +10 -0
  20. data/lib/special_delivery/complaint_event.rb +10 -0
  21. data/lib/special_delivery/delivery_event.rb +10 -0
  22. data/lib/special_delivery/drop_event.rb +10 -0
  23. data/lib/special_delivery/engine.rb +7 -0
  24. data/lib/special_delivery/event_factory.rb +19 -0
  25. data/lib/special_delivery/mailer.rb +27 -0
  26. data/lib/special_delivery/open_event.rb +10 -0
  27. data/lib/special_delivery/unsubscribe_event.rb +10 -0
  28. data/lib/special_delivery/version.rb +3 -0
  29. data/lib/special_delivery.rb +4 -0
  30. data/lib/tasks/special_delivery_tasks.rake +4 -0
  31. metadata +172 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4768d6c31c2e94ba4eb4f37a4a62bb5f7f94da1f
4
+ data.tar.gz: 4b32439c6ff8ffaf4d852e070fa5fa5f2d7b0c6d
5
+ SHA512:
6
+ metadata.gz: 7b64162f3a7aa0efa1cd687984c7fd591099eecbb475ef215ca43b9a4be8fc2e00e65fdd576a7d2f91466787a25d90556ad957983e81f0a833c2cd78cc105ea1
7
+ data.tar.gz: b1ff851ceea36294cd22efc969019b4b3eef2c97f26f97306a5dec9ce24a17ed62f166f81309245dbb69b7beadce46798a86026cf5e703e7aa975966cd782b18
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = SpecialDelivery
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SpecialDelivery'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,4 @@
1
+ module SpecialDelivery
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,35 @@
1
+ require_dependency "special_delivery/application_controller"
2
+
3
+ module SpecialDelivery
4
+ class EventsController < ApplicationController
5
+ skip_before_filter :verify_authenticity_token
6
+
7
+ before_filter :ensure_authenticity
8
+
9
+ rescue_from ActiveRecord::RecordNotFound,
10
+ :with => :issue_accepted_response
11
+
12
+ def create
13
+ event.issue_callback
14
+ render_nothing(:ok)
15
+ end
16
+
17
+ private
18
+
19
+ def issue_accepted_response
20
+ render_nothing(:accepted)
21
+ end
22
+
23
+ def render_nothing(http_status)
24
+ render nothing: true, status: http_status
25
+ end
26
+
27
+ def ensure_authenticity
28
+ Authenticator.new(params).authentic? || render_nothing(:unauthorized)
29
+ end
30
+
31
+ def event
32
+ @event ||= EventFactory.new_event(params)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,18 @@
1
+ module SpecialDelivery
2
+ class Delivery < ActiveRecord::Base
3
+ belongs_to :deliverable, polymorphic: true
4
+
5
+ validates :callback_class_name, :message_id, :presence => true
6
+
7
+ def callback(event_type)
8
+ callback_class.send(event_type, deliverable) if callback_class
9
+ end
10
+
11
+ private
12
+
13
+ def callback_class
14
+ callback_class_name.constantize
15
+ rescue NameError
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>SpecialDelivery</title>
5
+ <%= stylesheet_link_tag "special_delivery/application", media: "all" %>
6
+ <%= javascript_include_tag "special_delivery/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ SpecialDelivery::Engine.routes.draw do
2
+ post "/" => "events#create", :as => "root"
3
+ end
@@ -0,0 +1,12 @@
1
+ class CreateSpecialDeliveryDeliveries < ActiveRecord::Migration
2
+ def change
3
+ create_table :special_delivery_deliveries do |t|
4
+ t.string :callback_class_name
5
+ t.integer :deliverable_id
6
+ t.string :deliverable_type
7
+ t.string :message_id
8
+
9
+ t.timestamps
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,37 @@
1
+ module SpecialDelivery
2
+ class Authenticator
3
+ def initialize(event_params)
4
+ @event_params = event_params.to_hash
5
+ end
6
+
7
+ def authentic?
8
+ actual_signature == expected_signature
9
+ rescue
10
+ false
11
+ end
12
+
13
+ private
14
+
15
+ attr_reader :event_params
16
+
17
+ def expected_signature
18
+ OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), api_key,"#{timestamp}#{token}")
19
+ end
20
+
21
+ def api_key
22
+ MAILGUN_CONFIG[:api_key]
23
+ end
24
+
25
+ def timestamp
26
+ event_params.fetch('timestamp')
27
+ end
28
+
29
+ def token
30
+ event_params.fetch('token')
31
+ end
32
+
33
+ def actual_signature
34
+ event_params.fetch('signature')
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ module SpecialDelivery
2
+ class BaseEvent
3
+ attr_reader :params
4
+
5
+ def initialize(params)
6
+ @params = params || {}
7
+ end
8
+
9
+ def issue_callback
10
+ delivery.callback(callback_method)
11
+ end
12
+
13
+ private
14
+
15
+ def callback_method
16
+ raise NotImplementedError, 'Event subclasses must implement #callback_method'
17
+ end
18
+
19
+ def delivery
20
+ @delivery ||= Delivery.find_by_message_id!(params.fetch('Message-Id'))
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class BounceEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :bounced
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,40 @@
1
+ module SpecialDelivery
2
+ class Callback
3
+ def bounced(*args); end
4
+ def clicked(*args); end
5
+ def complained(*args); end
6
+ def delivered(*args); end
7
+ def dropped(*args); end
8
+ def opened(*args); end
9
+ def unsubscribed(*args); end
10
+
11
+ def evaluate
12
+ if callback_argument.nil?
13
+ send(callback_method)
14
+ else
15
+ send(callback_method, callback_argument)
16
+ end
17
+ end
18
+
19
+ def self.respond_to?(method, include_private = false)
20
+ super || public_instance_methods(true).include?(method)
21
+ end
22
+
23
+ def self.method_missing(method_name, *args)
24
+ if respond_to?(method_name)
25
+ new(method_name, *args).evaluate
26
+ else
27
+ super
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :callback_argument, :callback_method
34
+
35
+ def initialize(callback_method, *args)
36
+ @callback_method = callback_method
37
+ @callback_argument = args[0].presence
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class ClickEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :clicked
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class ComplaintEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :complained
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class DeliveryEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :delivered
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class DropEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :dropped
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module SpecialDelivery
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace SpecialDelivery
4
+
5
+ config.autoload_paths += Dir["#{config.root}/lib/**/"]
6
+ end
7
+ end
@@ -0,0 +1,19 @@
1
+ module SpecialDelivery
2
+ class EventFactory
3
+ EVENT_TYPE_CLASSES = {
4
+ 'opened' => OpenEvent,
5
+ 'unsubscribed' => UnsubscribeEvent,
6
+ 'complained' => ComplaintEvent,
7
+ 'bounced' => BounceEvent,
8
+ 'clicked' => ClickEvent,
9
+ 'dropped' => DropEvent,
10
+ 'delivered' => DeliveryEvent
11
+ }
12
+
13
+ def self.new_event(event_params)
14
+ event_type = event_params.fetch(:event)
15
+
16
+ EVENT_TYPE_CLASSES.fetch(event_type).new(event_params)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ module SpecialDelivery
2
+ module Mailer
3
+ def special_delivery(params = {})
4
+ callback_class = params[:callback_class]
5
+
6
+ yield.tap do |mail|
7
+ if mail && callback_class
8
+ Delivery.new.tap do |delivery|
9
+ delivery.callback_class_name = callback_class.to_s
10
+ delivery.deliverable = params[:callback_record] if params[:callback_record]
11
+ delivery.message_id = message_id_for(mail)
12
+ end.save
13
+ end
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def message_id_for(mail)
20
+ if mail.message_id.blank?
21
+ mail.message_id = SecureRandom.uuid
22
+ end
23
+
24
+ "<#{mail.message_id}>"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class OpenEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :opened
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SpecialDelivery
2
+ class UnsubscribeEvent < BaseEvent
3
+
4
+ private
5
+
6
+ def callback_method
7
+ :unsubscribed
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module SpecialDelivery
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ require "special_delivery/engine"
2
+
3
+ module SpecialDelivery
4
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :special_delivery do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: special-delivery
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lawson Kurtz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: mysql2
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: capybara
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda-matchers
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Special Delivery is a Rails engine that enables powerful and easy-to-use
112
+ callbacks in response to Mailgun events POSTed via webhook.
113
+ email:
114
+ - lawson.kurtz@viget.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - app/assets/javascripts/special_delivery/application.js
120
+ - app/assets/javascripts/special_delivery/events.js
121
+ - app/assets/stylesheets/special_delivery/application.css
122
+ - app/assets/stylesheets/special_delivery/events.css
123
+ - app/controllers/special_delivery/application_controller.rb
124
+ - app/controllers/special_delivery/events_controller.rb
125
+ - app/models/special_delivery/delivery.rb
126
+ - app/views/layouts/special_delivery/application.html.erb
127
+ - config/routes.rb
128
+ - db/migrate/20131111152047_create_special_delivery_deliveries.rb
129
+ - lib/special_delivery/authenticator.rb
130
+ - lib/special_delivery/base_event.rb
131
+ - lib/special_delivery/bounce_event.rb
132
+ - lib/special_delivery/callback.rb
133
+ - lib/special_delivery/click_event.rb
134
+ - lib/special_delivery/complaint_event.rb
135
+ - lib/special_delivery/delivery_event.rb
136
+ - lib/special_delivery/drop_event.rb
137
+ - lib/special_delivery/engine.rb
138
+ - lib/special_delivery/event_factory.rb
139
+ - lib/special_delivery/mailer.rb
140
+ - lib/special_delivery/open_event.rb
141
+ - lib/special_delivery/unsubscribe_event.rb
142
+ - lib/special_delivery/version.rb
143
+ - lib/special_delivery.rb
144
+ - lib/tasks/special_delivery_tasks.rake
145
+ - MIT-LICENSE
146
+ - Rakefile
147
+ - README.rdoc
148
+ homepage: https://github.com/vigetlabs/special-delivery
149
+ licenses: []
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - '>='
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - '>='
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.1.11
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: A Mailgun Webhook Event Manager
171
+ test_files: []
172
+ has_rdoc: