outbox-rails 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,26 +1,70 @@
1
- # Outbox::Rails
1
+ Outbox::Rails
2
+ =============
2
3
 
3
- TODO: Write a gem description
4
+ [![Gem Version](https://badge.fury.io/rb/outbox-rails.png)](http://badge.fury.io/rb/outbox-rails)
4
5
 
5
- ## Installation
6
+ Rails Railtie for sending email, SMS, and push notifications using the [Outbox](https://github.com/localmed/outbox) gem. Please view the [Outbox documentation](https://github.com/localmed/outbox) to understand the philosophy behind this interface and how to use it.
7
+
8
+ Installation
9
+ ------------
6
10
 
7
11
  Add this line to your application's Gemfile:
8
12
 
9
- gem 'outbox-rails'
13
+ ``` ruby
14
+ gem 'outbox-rails'
15
+ ```
10
16
 
11
17
  And then execute:
12
18
 
13
- $ bundle
19
+ ``` bash
20
+ $ bundle
21
+ ```
14
22
 
15
23
  Or install it yourself as:
16
24
 
17
- $ gem install outbox-rails
25
+ ``` bash
26
+ $ gem install outbox-rails
27
+ ```
28
+
29
+ Usage
30
+ -----
31
+
32
+ Outbox::Notifier uses a very similar interface to ActionMailer.
33
+
34
+ First, define a notifier in `app/notifiers`:
35
+
36
+ ``` ruby
37
+ class AccountNotifier < Outbox::Notifier
38
+ default email: { from: 'noreply@myapp.com' },
39
+ sms: { from: '+15551234567' }
40
+
41
+ def welcome
42
+ # Compose message types using the Outbox::Message interface
43
+ email do
44
+ subject 'Welcome to our App!'
45
+ end
46
+
47
+ sms do
48
+ body 'Welcome to our App!'
49
+ end
50
+
51
+ # Render the body of the message. This is analogous to ActionMailer::Base#mail,
52
+ # but unlike in ActionMailer, #render_message is not required.
53
+ render_message
54
+ end
55
+ end
56
+ ```
18
57
 
19
- ## Usage
58
+ Send a message using the `deliver` method:
20
59
 
21
- TODO: Write usage instructions here
60
+ ```ruby
61
+ # Unlike ActionMailer, deliver takes a single argument that defines the recipients
62
+ # for the message types you want to send.
63
+ AccountNotifier.welcome.deliver email: 'user@gmail.com', sms: '+15557654321'
64
+ ```
22
65
 
23
- ## Contributing
66
+ Contributing
67
+ ------------
24
68
 
25
69
  1. Fork it
26
70
  2. Create your feature branch (`git checkout -b my-new-feature`)
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize!
7
+ run Combustion::Application
@@ -0,0 +1,107 @@
1
+ require 'action_mailer'
2
+
3
+ module Outbox
4
+ class Notifier < ActionMailer::Base
5
+ abstract!
6
+
7
+ delegate :attachments, :headers, to: :email
8
+ alias_method :_render_email, :mail
9
+ undef :mail
10
+
11
+ class << self
12
+ alias :defaults :default
13
+
14
+ # Returns the name of current notifier. This method is also being used
15
+ # as a path for a view lookup. If this is an anonymous notifier,
16
+ # this method will return +anonymous+ instead.
17
+ def notifier_name(value = nil)
18
+ if value.nil?
19
+ self.mailer_name
20
+ else
21
+ self.mailer_name = value
22
+ end
23
+ end
24
+ alias :notifier_name= :notifier_name
25
+
26
+ protected
27
+
28
+ def method_missing(method_name, *args) # :nodoc:
29
+ if respond_to?(method_name)
30
+ new(method_name, *args).message
31
+ else
32
+ super
33
+ end
34
+ end
35
+ end
36
+
37
+ def initialize(method_name = nil, *args) # :nodoc:
38
+ super()
39
+ # Make sure we don't ever get a NullMail object.
40
+ @_mail_was_called = true
41
+ @_message_rendered = false
42
+ @_message = Outbox::Message.new self.class.default_params.dup
43
+ process(method_name, *args) if method_name
44
+ end
45
+
46
+ # The composed Outbox::Message instance.
47
+ def message
48
+ render_message unless message_rendered?
49
+ @_message
50
+ end
51
+
52
+ # Returns true if the message has already been rendered.
53
+ def message_rendered?
54
+ @_message_rendered
55
+ end
56
+
57
+ # Renders the message body. This is analagous to ActionMailer's #mail
58
+ # method, but is not required - it will be called implicitly when the
59
+ # #message object is retrieved.
60
+ def render_message(options = {}, &block)
61
+ @_message_rendered = true
62
+
63
+ # Render an email using the #mail interface so we don't have
64
+ # to rewrite the template logic. Even if we aren't sending an email
65
+ # we can still use the rendered templates in other messages types.
66
+ email_options = options.extract! :content_type, :charset, :parts_order,
67
+ :body, :template_name, :template_path
68
+ email_options.merge!(options.delete(:email)) if options[:email]
69
+ email_options[:subject] ||= email.subject
70
+ email = render_email(email_options, &block)
71
+
72
+ @_message.assign_message_type_values(options)
73
+ assign_body_from_email(email)
74
+ @_message
75
+ end
76
+
77
+ protected
78
+
79
+ def assign_body_from_email(email)
80
+ # TODO: Implement this later once we have multiple message types. This
81
+ # will extract the rendered body from the mail (preferring text/plain)
82
+ # and assign it to the other message types.
83
+ end
84
+
85
+ def render_email(options, &block)
86
+ outbox_message = @_message
87
+ @_message = outbox_message.email || Mail.new
88
+ email = _render_email(options, &block)
89
+ @_message = outbox_message
90
+ email
91
+ end
92
+
93
+ def method_missing(method, *args, &block)
94
+ if @_message.respond_to?(method)
95
+ @_message.public_send(method, *args, &block)
96
+ else
97
+ super
98
+ end
99
+ end
100
+
101
+ def respond_to_missing?(method, include_private = false)
102
+ super || @_message.respond_to?(method, include_private)
103
+ end
104
+
105
+ ActiveSupport.run_load_hooks(:outbox, self)
106
+ end
107
+ end
data/lib/outbox/rails.rb CHANGED
@@ -1,7 +1,11 @@
1
+ require 'outbox'
1
2
  require 'outbox/rails/version'
3
+ require 'outbox/rails/railtie'
4
+ require 'active_support/rails'
2
5
 
3
6
  module Outbox
7
+ autoload :Notifier, 'outbox/notifier'
8
+
4
9
  module Rails
5
- # Your code goes here...
6
10
  end
7
11
  end
@@ -0,0 +1,13 @@
1
+ require 'rails'
2
+
3
+ module Outbox
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ config.outbox = Outbox::Message
7
+
8
+ initializer 'outbox.logger' do
9
+ ActiveSupport.on_load(:outbox) { self.logger ||= ::Rails.logger }
10
+ end
11
+ end
12
+ end
13
+ end
@@ -1,5 +1,5 @@
1
1
  module Outbox
2
2
  module Rails
3
- VERSION = '0.0.1'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
data/outbox-rails.gemspec CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
8
8
  spec.version = Outbox::Rails::VERSION
9
9
  spec.authors = ['Pete Browne']
10
10
  spec.email = ['pete.browne@localmed.com']
11
- spec.description = %q{Rails Engine for sending email, SMS, and push notifications.}
12
- spec.summary = %q{Rails Engine for sending email, SMS, and push notifications.}
13
- spec.homepage = 'https://github.com/localmed/outbox'
11
+ spec.description = %q{Rails Railtie for sending email, SMS, and push notifications using the Outbox gem.}
12
+ spec.summary = %q{Rails Railtie for sending email, SMS, and push notifications using the Outbox gem.}
13
+ spec.homepage = 'https://github.com/localmed/outbox-rails'
14
14
  spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,8 +18,13 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency 'outbox', '~> 0.1.0'
21
+ spec.add_runtime_dependency 'outbox', '~> 0.1.1'
22
+ spec.add_runtime_dependency 'rails', '~> 4.0.0'
22
23
  spec.add_development_dependency 'bundler', '~> 1.3'
23
24
  spec.add_development_dependency 'rake'
24
25
  spec.add_development_dependency 'rspec'
26
+ spec.add_development_dependency 'rspec-rails'
27
+ spec.add_development_dependency 'rspec_junit_formatter'
28
+ spec.add_development_dependency 'sqlite3'
29
+ spec.add_development_dependency 'combustion'
25
30
  end
@@ -0,0 +1,18 @@
1
+ class BaseNotifier < Outbox::Notifier
2
+ defaults email: { from: 'noreply@myapp.com' }
3
+
4
+ def welcome(hash = {})
5
+ render_message(hash)
6
+ end
7
+
8
+ def implicit_multipart(hash = {})
9
+ attachments['invoice.pdf'] = 'This is test File content' if hash.delete(:attachments)
10
+ render_message(hash)
11
+ end
12
+
13
+ def composed_message_with_implicit_render
14
+ email do
15
+ subject 'Composed Message'
16
+ end
17
+ end
18
+ end
@@ -0,0 +1 @@
1
+ Composed Message with Implicit Render
@@ -0,0 +1 @@
1
+ HTML Implicit Multipart
@@ -0,0 +1 @@
1
+ TEXT Implicit Multipart
@@ -0,0 +1 @@
1
+ Welcome
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ ActiveRecord::Schema.define do
2
+ #
3
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ describe Outbox::Notifier do
4
+ describe 'calling actions' do
5
+ it 'does not raise error' do
6
+ expect{BaseNotifier.welcome}.not_to raise_error()
7
+ end
8
+
9
+ it 'returns an Outbox Message' do
10
+ message = BaseNotifier.welcome
11
+ expect(message).to be_an_instance_of(Outbox::Message)
12
+ end
13
+
14
+ it 'passes the args to the action' do
15
+ message = BaseNotifier.welcome email: { subject: 'Subject Line' }
16
+ expect(message.email.subject).to eq('Subject Line')
17
+ end
18
+ end
19
+
20
+ describe '.logger' do
21
+ it 'defaults to Rails.logger' do
22
+ expect(BaseNotifier.logger).to be(Rails.logger)
23
+ end
24
+ end
25
+
26
+ describe '.defaults' do
27
+ it 'sets the default values' do
28
+ message = BaseNotifier.welcome
29
+ expect(message.email.from).to eq(['noreply@myapp.com'])
30
+ end
31
+ end
32
+
33
+ describe '.notifier_name' do
34
+ it 'returns the underscored class name' do
35
+ expect(BaseNotifier.notifier_name).to eq('base_notifier')
36
+ end
37
+
38
+ it 'returns anonymous for anonymous classes' do
39
+ notifier = Class.new Outbox::Notifier
40
+ expect(notifier.notifier_name).to eq('anonymous')
41
+ end
42
+
43
+ it 'is configurable' do
44
+ notifier = Class.new Outbox::Notifier
45
+ notifier.notifier_name 'some_notifier'
46
+ expect(notifier.notifier_name).to eq('some_notifier')
47
+ notifier.notifier_name = 'another_notifier'
48
+ expect(notifier.notifier_name).to eq('another_notifier')
49
+ end
50
+ end
51
+
52
+ describe '#email' do
53
+ it 'composes an email using Outbox interface' do
54
+ message = BaseNotifier.composed_message_with_implicit_render
55
+ expect(message.email.from).to eq(['noreply@myapp.com'])
56
+ expect(message.email.subject).to eq('Composed Message')
57
+ end
58
+ end
59
+
60
+ describe '#message' do
61
+ it 'renders the template' do
62
+ message = BaseNotifier.welcome
63
+ expect(message.email.body.encoded.strip).to eq('Welcome')
64
+ end
65
+
66
+ it 'handles multipart templates' do
67
+ message = BaseNotifier.implicit_multipart
68
+ expect(message.email.parts.size).to eq(2)
69
+ part_1 = message.email.parts[0]
70
+ part_2 = message.email.parts[1]
71
+ expect(part_1.mime_type).to eq('text/plain')
72
+ expect(part_1.body.encoded.strip).to eq('TEXT Implicit Multipart')
73
+ expect(part_2.mime_type).to eq('text/html')
74
+ expect(part_2.body.encoded.strip).to eq('HTML Implicit Multipart')
75
+ end
76
+ end
77
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,8 @@
1
1
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
2
 
3
+ require 'combustion'
4
+
5
+ Combustion.initialize!
6
+
7
+ require 'rspec-rails'
3
8
  require 'outbox/rails'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: outbox-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-31 00:00:00.000000000 Z
12
+ date: 2013-08-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: outbox
@@ -18,7 +18,7 @@ dependencies:
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.1.0
21
+ version: 0.1.1
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,23 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 0.1.0
29
+ version: 0.1.1
30
+ - !ruby/object:Gem::Dependency
31
+ name: rails
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 4.0.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 4.0.0
30
46
  - !ruby/object:Gem::Dependency
31
47
  name: bundler
32
48
  requirement: !ruby/object:Gem::Requirement
@@ -75,7 +91,72 @@ dependencies:
75
91
  - - ! '>='
76
92
  - !ruby/object:Gem::Version
77
93
  version: '0'
78
- description: Rails Engine for sending email, SMS, and push notifications.
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec-rails
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: rspec_junit_formatter
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: sqlite3
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: combustion
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ description: Rails Railtie for sending email, SMS, and push notifications using the
159
+ Outbox gem.
79
160
  email:
80
161
  - pete.browne@localmed.com
81
162
  executables: []
@@ -88,13 +169,27 @@ files:
88
169
  - LICENSE.txt
89
170
  - README.md
90
171
  - Rakefile
172
+ - config.ru
91
173
  - lib/outbox-rails.rb
174
+ - lib/outbox/notifier.rb
92
175
  - lib/outbox/rails.rb
176
+ - lib/outbox/rails/railtie.rb
93
177
  - lib/outbox/rails/version.rb
94
178
  - outbox-rails.gemspec
179
+ - spec/internal/app/notifiers/base_notifier.rb
180
+ - spec/internal/app/views/base_notifier/composed_message_with_implicit_render.erb
181
+ - spec/internal/app/views/base_notifier/implicit_multipart.html.erb
182
+ - spec/internal/app/views/base_notifier/implicit_multipart.text.erb
183
+ - spec/internal/app/views/base_notifier/welcome.erb
184
+ - spec/internal/config/database.yml
185
+ - spec/internal/config/routes.rb
186
+ - spec/internal/db/schema.rb
187
+ - spec/internal/log/.gitignore
188
+ - spec/internal/public/favicon.ico
189
+ - spec/outbox/rails/notifier_spec.rb
95
190
  - spec/outbox/rails_spec.rb
96
191
  - spec/spec_helper.rb
97
- homepage: https://github.com/localmed/outbox
192
+ homepage: https://github.com/localmed/outbox-rails
98
193
  licenses:
99
194
  - MIT
100
195
  post_install_message:
@@ -109,7 +204,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
109
204
  version: '0'
110
205
  segments:
111
206
  - 0
112
- hash: -487938589802225081
207
+ hash: -2287336915534194965
113
208
  required_rubygems_version: !ruby/object:Gem::Requirement
114
209
  none: false
115
210
  requirements:
@@ -118,13 +213,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
213
  version: '0'
119
214
  segments:
120
215
  - 0
121
- hash: -487938589802225081
216
+ hash: -2287336915534194965
122
217
  requirements: []
123
218
  rubyforge_project:
124
219
  rubygems_version: 1.8.23
125
220
  signing_key:
126
221
  specification_version: 3
127
- summary: Rails Engine for sending email, SMS, and push notifications.
222
+ summary: Rails Railtie for sending email, SMS, and push notifications using the Outbox
223
+ gem.
128
224
  test_files:
225
+ - spec/internal/app/notifiers/base_notifier.rb
226
+ - spec/internal/app/views/base_notifier/composed_message_with_implicit_render.erb
227
+ - spec/internal/app/views/base_notifier/implicit_multipart.html.erb
228
+ - spec/internal/app/views/base_notifier/implicit_multipart.text.erb
229
+ - spec/internal/app/views/base_notifier/welcome.erb
230
+ - spec/internal/config/database.yml
231
+ - spec/internal/config/routes.rb
232
+ - spec/internal/db/schema.rb
233
+ - spec/internal/log/.gitignore
234
+ - spec/internal/public/favicon.ico
235
+ - spec/outbox/rails/notifier_spec.rb
129
236
  - spec/outbox/rails_spec.rb
130
237
  - spec/spec_helper.rb