mandrill-rails 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODA5YzY1YTI5ODBjOTg0M2Y4NDdhNzc0ZTg5MGFhNTk0MDgwMmNhMg==
4
+ ZDEzMWQyMzk4ZDZjMzk2ZTBkM2M5YzdiMjc4ZWNmN2RmM2NlYjFlNw==
5
5
  data.tar.gz: !binary |-
6
- N2UyZTI0NWU3OTkxNjM2MmUyM2EzODIxNDcxMGMxMGVjZDAxYzU0YQ==
6
+ YWE3NjlmM2IzZWNlZTZlMzgyMzA0ZjQyMTVlZjFkYzJkZGRkNjgwNA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YjcyY2E1MTI1NzNiYWFjMjIyN2I5NTVmYmM0M2E1NzllZDM0MzhiZGIyZTc2
10
- Nzk5Zjg3NDUwMDdlYWRlMmNhNWQxNjIyYWRjNDAyZGQ4OTdkYjNmNzEzZjA2
11
- YWU0MzYzYjFjZGVkZjYwZDQ4OTA2ZmZjZDc4N2RkYzJjN2RjYTk=
9
+ MzIzZDNkYzRiMjAzZTlhYTIyZjJlNzkyY2Y1ZWQ4NDM1YmQ1NTBiYWUyMTE1
10
+ YmFiYmIyZDViNDRlNzYxY2EzZWQxZjg3MzVlNTM5YWY3ODNmZDVmMGEwNWIy
11
+ MThlYTMxYzBlYzA3NTBlNDVlYzNkZDNmNmY5M2YwYTc4NmUyZWU=
12
12
  data.tar.gz: !binary |-
13
- N2MwNGFkNTIyYTc2NGZiMDgwNDdhYzIzNWZjYmM2NjhjNGRlNzc0NzdkYmNi
14
- MDc5MTViMjg0OTdkY2NkMzY5MDA3ZGJkZTlkNTAyZGZjZDE5NmU1MzA2ZTRl
15
- ZTY2NTcwMjA0OWY0Y2VmZGFkMzdlY2JmNTA5YjIyOTFmYzYxZjQ=
13
+ YTFlMzEzNWZlZTJjYmU2MzM0MTllMzJmZjlkNzQwZGZmYTI0ZTlkNjY5YmIy
14
+ NWJkZDVhMDMyYzQzYjAzODQyNDk3YWMyM2ZmZDNlYmY5NTJhYzgxMTZiMDBl
15
+ Yzg4MDAyZDIzMzljNzUwNWQ5MTc5YjQyM2M2Y2Y5ZDEzMDEyNDM=
@@ -16,7 +16,6 @@ FYI, {Mandrill}[http://mandrill.com/] is the transactional email service by the
16
16
  * Requires Rails >= 3.0.3 (including 3.1, 3.2 and 4).
17
17
 
18
18
  Food for thought (upcoming features maybe)..
19
- * some generators may be handy to avoid the manual coding to wire up web hooks
20
19
  * I thought about implementing this as an engine, but the overhead did not seem appropriate. Maybe that view will change..
21
20
 
22
21
  == The Mandrill::Rails Cookbook
@@ -48,7 +47,29 @@ See the section below on 'Contributing to Mandrill::Rails' for more information.
48
47
 
49
48
  Say we have configured Mandrill to send requests to /inbox at our site (see the next recipes for how you do that).
50
49
 
51
- Once we have Mandrill::Rails in our project, we just need to do two things. There's no generator to help you do this at the moment, but it is pretty simple:
50
+ Once we have Mandrill::Rails in our project, we just need to do two things. You can run a generator to do it for you, or you can configure things manually:
51
+
52
+ ==== Using the generator
53
+
54
+ Run the generator and specify the name for your route and controller:
55
+
56
+ rails generate mandrill inbox
57
+
58
+ This will create a resource route and corresponding controller at /inbox.
59
+
60
+ If you need a namespaced controller, specify it in the name:
61
+
62
+ rails generate mandrill hooks/inbox
63
+
64
+ This creates an `inbox` route that points to the `Hooks:InboxController` class.
65
+
66
+ If you prefer pluralized names, that can be specified with a flag:
67
+
68
+ rails generate mandrill inbox --pluralize_names
69
+
70
+ This will create an `InboxesController` class and a resource route called `inboxes`.
71
+
72
+ ==== Manual configuration
52
73
 
53
74
  First, configure a resource route:
54
75
 
@@ -0,0 +1,18 @@
1
+ class <%= @controller_name %>Controller < ApplicationController
2
+ include Mandrill::Rails::WebHookProcessor
3
+
4
+ # To completely ignore unhandled events (not even logging), uncomment this line
5
+ # ignore_unhandled_events!
6
+
7
+ # If you want unhandled events to raise a hard exception, uncomment this line
8
+ # unhandled_events_raise_exceptions!
9
+
10
+ # To enable authentication, uncomment this line and set your API key.
11
+ # It is recommended you pull your API keys from environment settings,
12
+ # or use some other means to avoid committing the API keys in your source code.
13
+ # authenticate_with_mandrill_keys! 'YOUR_MANDRILL_WEBHOOK_KEY'
14
+
15
+ def handle_inbound(event_payload)
16
+ head(:ok)
17
+ end
18
+ end
@@ -0,0 +1,83 @@
1
+ require 'rails/generators/named_base'
2
+
3
+ module Mandrill
4
+ module Rails
5
+ module Generators
6
+ class WebHookGenerator < ::Rails::Generators::Base
7
+ namespace 'mandrill'
8
+ desc 'Generates a controller and routes for Mandrill web hooks.'
9
+ argument :name, type: :string
10
+ class_option :pluralize_names, aliases: '-p', type: :boolean, default: false,
11
+ desc: 'Pluralize names in route and controller'
12
+ class_option :routes, type: :boolean, default: true,
13
+ desc: 'Creates routes for web hooks'
14
+ class_option :controller, type: :boolean, default: true,
15
+ desc: 'Creates a controller for web hooks'
16
+
17
+ source_root File.expand_path("../templates", __FILE__)
18
+
19
+ def initialize(args, *options)
20
+ args[0] = args[0].dup if args[0].is_a?(String) && args[0].frozen?
21
+ super
22
+ assign_names!(self.name)
23
+ end
24
+
25
+ def add_routes
26
+ return unless options.routes?
27
+ hook_route = "resource :#{resource_name}"
28
+
29
+ controller = controller_path
30
+
31
+ hook_route << %Q(, :controller => '#{controller}')
32
+ hook_route << %Q(, :only => [:show,:create])
33
+ route hook_route
34
+ end
35
+
36
+ def add_controller
37
+ return unless options.controller?
38
+ @controller_name = class_name
39
+ template 'controller.rb', controller_destination
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :file_name
45
+
46
+ def assign_names!(name)
47
+ @class_path = name.include?('/') ? name.split('/') : name.split('::')
48
+ @class_path.map!(&:underscore)
49
+ @file_name = @class_path.pop
50
+ end
51
+
52
+ def class_name
53
+ @class_name ||= (@class_path + [resource_name]).map!(&:camelize).join('::')
54
+ end
55
+
56
+ def controller_destination
57
+ "app/controllers/#{controller_path}_controller.rb"
58
+ end
59
+
60
+ def controller_path
61
+ @controller_path ||= if class_name.include?('::')
62
+ @class_path.collect {|dname| dname }.join + "/" + resource_name
63
+ else
64
+ resource_name
65
+ end
66
+ end
67
+
68
+ def plural_name
69
+ @plural_name ||= singular_name.pluralize
70
+ end
71
+
72
+ def resource_name
73
+ return singular_name unless options.pluralize_names?
74
+ plural_name
75
+ end
76
+
77
+ def singular_name
78
+ file_name.downcase
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -3,5 +3,5 @@ module Mandrill
3
3
  end
4
4
  end
5
5
  unless defined?(Mandrill::Rails::VERSION)
6
- Mandrill::Rails::VERSION = "1.3.1"
6
+ Mandrill::Rails::VERSION = "1.4.0"
7
7
  end
@@ -21,6 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "bundler", "~> 1.7"
22
22
  spec.add_development_dependency "rake", "~> 10.0"
23
23
  spec.add_development_dependency "rspec", "~> 3.0"
24
+ spec.add_development_dependency "generator_spec", "~> 0.9"
24
25
  spec.add_development_dependency "guard-rspec", "~> 4.5"
25
26
  spec.add_development_dependency "rdoc"
26
27
 
@@ -0,0 +1,246 @@
1
+ require 'spec_helper'
2
+ require 'generator_spec'
3
+ require 'generators/web_hook_generator'
4
+
5
+ describe Mandrill::Rails::Generators::WebHookGenerator, type: :generator do
6
+ destination File.expand_path("../../tmp", __FILE__)
7
+
8
+ before do
9
+ prepare_destination
10
+ copy_routes
11
+ end
12
+
13
+ describe 'route generation' do
14
+ context 'with no pluralized option' do
15
+ context 'with simple names' do
16
+ before { run_generator %w(inbox) }
17
+
18
+ it 'creates a proper route' do
19
+ match = "resource :inbox, :controller => 'inbox', :only => [:show,:create]"
20
+ expect(destination_root).to have_structure {
21
+ directory 'config' do
22
+ file 'routes.rb' do
23
+ contains match
24
+ end
25
+ end
26
+ }
27
+ end
28
+ end
29
+
30
+ context 'with namespaced names' do
31
+ before { run_generator %w(hooks/inbox) }
32
+
33
+ it 'creates a proper route' do
34
+ match = "resource :inbox, :controller => 'hooks/inbox', :only => [:show,:create]"
35
+ expect(destination_root).to have_structure {
36
+ directory 'config' do
37
+ file 'routes.rb' do
38
+ contains match
39
+ end
40
+ end
41
+ }
42
+ end
43
+ end
44
+
45
+ context 'with capitalized names' do
46
+ before { run_generator %w(Inbox) }
47
+
48
+ it 'creates a proper route' do
49
+ match = "resource :inbox, :controller => 'inbox', :only => [:show,:create]"
50
+ expect(destination_root).to have_structure {
51
+ directory 'config' do
52
+ file 'routes.rb' do
53
+ contains match
54
+ end
55
+ end
56
+ }
57
+ end
58
+ end
59
+ end
60
+
61
+ context 'with an explicit pluralized option' do
62
+ context 'with simple names' do
63
+ before { run_generator %w(inbox --pluralize_names) }
64
+
65
+ it 'creates a proper route' do
66
+ match = "resource :inboxes, :controller => 'inboxes', :only => [:show,:create]"
67
+ expect(destination_root).to have_structure {
68
+ directory 'config' do
69
+ file 'routes.rb' do
70
+ contains match
71
+ end
72
+ end
73
+ }
74
+ end
75
+ end
76
+
77
+ context 'with namespaced names' do
78
+ before { run_generator %w(hooks/inbox --pluralize_names) }
79
+
80
+ it 'creates a proper route' do
81
+ match = "resource :inboxes, :controller => 'hooks/inboxes', :only => [:show,:create]"
82
+ expect(destination_root).to have_structure {
83
+ directory 'config' do
84
+ file 'routes.rb' do
85
+ contains match
86
+ end
87
+ end
88
+ }
89
+ end
90
+ end
91
+
92
+ context 'with capitalized names' do
93
+ before { run_generator %w(hooks/Inbox --pluralize_names) }
94
+
95
+ it 'creates a proper route' do
96
+ match = "resource :inboxes, :controller => 'hooks/inboxes', :only => [:show,:create]"
97
+ expect(destination_root).to have_structure {
98
+ directory 'config' do
99
+ file 'routes.rb' do
100
+ contains match
101
+ end
102
+ end
103
+ }
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ describe 'controller generation' do
110
+ context 'with controller explicitly skipped' do
111
+ before { run_generator %w(inbox --skip-controller) }
112
+
113
+ it 'does not create a controller file' do
114
+ expect(destination_root).to have_structure {
115
+ directory 'app' do
116
+ directory 'controllers' do
117
+ no_file 'inbox_controller.rb'
118
+ end
119
+ end
120
+ }
121
+ end
122
+ end
123
+
124
+ context 'with no pluralized option' do
125
+ context 'with simple names' do
126
+ before { run_generator %w(inbox) }
127
+
128
+ it 'creates a proper controller file' do
129
+ match = 'class InboxController < ApplicationController'
130
+ expect(destination_root).to have_structure {
131
+ directory 'app' do
132
+ directory 'controllers' do
133
+ file 'inbox_controller.rb' do
134
+ contains match
135
+ end
136
+ end
137
+ end
138
+ }
139
+ end
140
+ end
141
+
142
+ context 'with namespaced names' do
143
+ before { run_generator %w(hooks/inbox) }
144
+
145
+ it 'creates a proper controller file' do
146
+ match = 'class Hooks::InboxController < ApplicationController'
147
+ expect(destination_root).to have_structure {
148
+ directory 'app' do
149
+ directory 'controllers' do
150
+ directory 'hooks' do
151
+ file 'inbox_controller.rb' do
152
+ contains match
153
+ end
154
+ end
155
+ end
156
+ end
157
+ }
158
+ end
159
+ end
160
+
161
+ context 'with capitalized names' do
162
+ before { run_generator %w(hooks/Inbox) }
163
+
164
+ it 'creates a proper controller file' do
165
+ match = 'class Hooks::InboxController < ApplicationController'
166
+ expect(destination_root).to have_structure {
167
+ directory 'app' do
168
+ directory 'controllers' do
169
+ directory 'hooks' do
170
+ file 'inbox_controller.rb' do
171
+ contains match
172
+ end
173
+ end
174
+ end
175
+ end
176
+ }
177
+ end
178
+ end
179
+ end
180
+
181
+ context 'with an explicit pluralized option' do
182
+ context 'with simple names' do
183
+ before { run_generator %w(inbox --pluralize_names) }
184
+
185
+ it 'creates a proper controller file' do
186
+ match = 'class InboxesController < ApplicationController'
187
+ expect(destination_root).to have_structure {
188
+ directory 'app' do
189
+ directory 'controllers' do
190
+ file 'inboxes_controller.rb' do
191
+ contains match
192
+ end
193
+ end
194
+ end
195
+ }
196
+ end
197
+ end
198
+
199
+ context 'with namespaced names' do
200
+ before { run_generator %w(hooks/inbox --pluralize_names) }
201
+
202
+ it 'creates a proper controller file' do
203
+ match = 'class Hooks::InboxesController < ApplicationController'
204
+ expect(destination_root).to have_structure {
205
+ directory 'app' do
206
+ directory 'controllers' do
207
+ directory 'hooks' do
208
+ file 'inboxes_controller.rb' do
209
+ contains match
210
+ end
211
+ end
212
+ end
213
+ end
214
+ }
215
+ end
216
+ end
217
+
218
+ context 'with capitalized names' do
219
+ before { run_generator %w(hooks/Inboxes --pluralized_names) }
220
+
221
+ it 'creates a proper controller file' do
222
+ match = 'class Hooks::InboxesController < ApplicationController'
223
+ expect(destination_root).to have_structure {
224
+ directory 'app' do
225
+ directory 'controllers' do
226
+ directory 'hooks' do
227
+ file 'inboxes_controller.rb' do
228
+ contains match
229
+ end
230
+ end
231
+ end
232
+ end
233
+ }
234
+ end
235
+ end
236
+ end
237
+ end
238
+ end
239
+
240
+ def copy_routes
241
+ routes = File.expand_path('../../rails_app/config/routes.rb', __FILE__)
242
+ destination = File.join(destination_root, 'config')
243
+
244
+ FileUtils.mkdir_p(destination)
245
+ FileUtils.cp routes, destination
246
+ end
@@ -90,13 +90,6 @@ describe Mandrill::WebHook::Processor do
90
90
  end
91
91
  end
92
92
  context "and default missing handler behaviour" do
93
- before do
94
- class ::Rails
95
- end
96
- end
97
- after do
98
- Object.send(:remove_const, :Rails)
99
- end
100
93
  it "logs an error" do
101
94
  processor.on_unhandled_mandrill_events = :log
102
95
  logger = double()
@@ -0,0 +1,4 @@
1
+ Rails.application.routes.draw do
2
+ # Resources for testing
3
+ root to: "home#index", via: [:get, :post]
4
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mandrill-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.1
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Gallagher
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-27 00:00:00.000000000 Z
11
+ date: 2015-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ~>
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: generator_spec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '0.9'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '0.9'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: guard-rspec
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -110,6 +124,8 @@ files:
110
124
  - LICENSE
111
125
  - README.rdoc
112
126
  - Rakefile
127
+ - lib/generators/templates/controller.rb
128
+ - lib/generators/web_hook_generator.rb
113
129
  - lib/mandrill-rails.rb
114
130
  - lib/mandrill-rails/errors.rb
115
131
  - lib/mandrill-rails/version.rb
@@ -141,11 +157,13 @@ files:
141
157
  - spec/fixtures/webhook_examples/send.json
142
158
  - spec/fixtures/webhook_examples/sync_blacklist.json
143
159
  - spec/fixtures/webhook_examples/sync_whitelist.json
160
+ - spec/generators/web_hook_generator_spec.rb
144
161
  - spec/mandrill-rails/web_hook_processor_spec.rb
145
162
  - spec/mandrill/web_hook/attachment_spec.rb
146
163
  - spec/mandrill/web_hook/event_decorator_spec.rb
147
164
  - spec/mandrill/web_hook/image_spec.rb
148
165
  - spec/mandrill/web_hook/processor_spec.rb
166
+ - spec/rails_app/config/routes.rb
149
167
  - spec/spec_helper.rb
150
168
  - spec/support/fixtures_helper.rb
151
169
  homepage: https://github.com/evendis/mandrill-rails
@@ -195,10 +213,12 @@ test_files:
195
213
  - spec/fixtures/webhook_examples/send.json
196
214
  - spec/fixtures/webhook_examples/sync_blacklist.json
197
215
  - spec/fixtures/webhook_examples/sync_whitelist.json
216
+ - spec/generators/web_hook_generator_spec.rb
198
217
  - spec/mandrill-rails/web_hook_processor_spec.rb
199
218
  - spec/mandrill/web_hook/attachment_spec.rb
200
219
  - spec/mandrill/web_hook/event_decorator_spec.rb
201
220
  - spec/mandrill/web_hook/image_spec.rb
202
221
  - spec/mandrill/web_hook/processor_spec.rb
222
+ - spec/rails_app/config/routes.rb
203
223
  - spec/spec_helper.rb
204
224
  - spec/support/fixtures_helper.rb