qc-mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rvmrc ADDED
@@ -0,0 +1,2 @@
1
+ rvm_gemset_create_on_use_flag=1
2
+ rvm use 1.9.2@qc-mailer
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ language: ruby
2
+
3
+ notifications:
4
+ email: false
5
+ webhooks:
6
+ on_success: always
7
+ on_failure: always
8
+
9
+ rvm:
10
+ - 1.9.2
11
+ - 1.9.3
12
+
13
+ script: bundle exec rake test
14
+
15
+ before_script:
16
+ - psql -c 'create database queue_classic_test;' -U postgres
17
+
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in qc-mailer.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ QueueClassic Mailer
2
+ ==================
3
+
4
+ Add to a Rails 3.x project to send email in the background using QueueClassic.
5
+
6
+ This gem was inspired by ResqueMailer, https://github.com/zapnap/resque_mailer
7
+
8
+ **WARNING: USE AT OWN RISK! THIS GEM IS CONSIDERED EXTREME ALPHA!**
9
+
10
+ [![Build Status](https://secure.travis-ci.org/zerobearing2/qc-mailer.png)](http://travis-ci.org/zerobearing2/qc-mailer)
11
+
12
+ Usage
13
+ -----
14
+
15
+ Install as gem
16
+
17
+ gem install qc-mailer
18
+
19
+ Add to Gemfile
20
+
21
+ gem "qc-mailer"
22
+
23
+ Include QC::Mailer in your ActionMailer subclass(es) like this:
24
+
25
+ ```ruby
26
+ class MyMailer < ActionMailer::Base
27
+ include QC::Mailer
28
+ end
29
+ ```
30
+
31
+ Now, when ```MyMailer.subject_email(params).deliver``` is called, an entry will be created in the job queue.
32
+
33
+ Note that you can still have mail delivered synchronously by using the bang method variant:
34
+ ```ruby
35
+ MyMailer.subject_email(params).deliver!
36
+ ```
37
+
38
+ If you want to set a different default queue name for your mailer, you can change the default_queue property like so:
39
+ ```ruby
40
+ # config/initializers/qc_mailer.rb
41
+ QC::Mailer.default_queue = 'application_specific_mailer'
42
+ ```
43
+
44
+ TODO
45
+ ----
46
+
47
+ - TBD
48
+
49
+
50
+ Meta
51
+ ----
52
+
53
+ Released under the [MIT license](http://www.opensource.org/licenses/mit-license.php).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+ require 'rake/testtask'
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs.push "lib"
9
+ t.test_files = FileList[File.expand_path('../test/**/*_test.rb', __FILE__)]
10
+ t.verbose = true
11
+ end
12
+
@@ -0,0 +1,33 @@
1
+ require "queue_classic"
2
+
3
+ module QC
4
+ module Mailer
5
+
6
+ class MessageDecoy
7
+ delegate :to_s, :to => :actual_message
8
+
9
+ def initialize(mailer_class, method_name, *args)
10
+ @mailer_class = mailer_class
11
+ @method_name = method_name.to_s
12
+ *@args = *args
13
+ end
14
+
15
+ def actual_message
16
+ @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
17
+ end
18
+
19
+ def deliver
20
+ @mailer_class.queue.enqueue("#{@mailer_class}.deliver", @method_name, *@args)
21
+ end
22
+
23
+ def deliver!
24
+ actual_message.deliver
25
+ end
26
+
27
+ def method_missing(method_name, *args)
28
+ actual_message.send(method_name, *args)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module QC
2
+ module Mailer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/lib/qc-mailer.rb ADDED
@@ -0,0 +1,47 @@
1
+ require "active_support/concern"
2
+ require "qc-mailer/version"
3
+ require "qc-mailer/message_decoy"
4
+
5
+ module QC
6
+ module Mailer
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ extend(ClassMethods)
11
+ end
12
+
13
+ # ModuleMethods
14
+ mattr_accessor :default_queue
15
+ def self.default_queue
16
+ @@default_queue || QC::QUEUE || "default"
17
+ end
18
+
19
+ # ClassMethods
20
+ module ClassMethods
21
+ def queue_name
22
+ @queue_name || QC::Mailer.default_queue
23
+ end
24
+
25
+ def queue_name=(val)
26
+ @queue_name = val
27
+ end
28
+
29
+ def queue
30
+ @queue ||= QC::Queue.new(queue_name)
31
+ end
32
+
33
+ def deliver(method, *args)
34
+ send(method, *args).deliver!
35
+ end
36
+
37
+ def method_missing(method_name, *args)
38
+ if action_methods.include?(method_name.to_s)
39
+ MessageDecoy.new(self, method_name, *args)
40
+ else
41
+ super
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
data/qc-mailer.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/qc-mailer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["David Bradford"]
6
+ gem.email = ["david@zerobearing.com"]
7
+ gem.description = %q{Rails plugin for sending asynchronous email with ActionMailer and QueueClassic.}
8
+ gem.summary = %q{Rails plugin for sending asynchronous email with ActionMailer and QueueClassic.}
9
+ gem.homepage = "https://github.com/zerobearing2/qc-mailer"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "qc-mailer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = QC::Mailer::VERSION
17
+
18
+ # specify any dependencies here; for example:
19
+ gem.add_runtime_dependency "activesupport", "~> 3.0"
20
+ gem.add_runtime_dependency "actionmailer", "~> 3.0"
21
+ gem.add_runtime_dependency "i18n", "~> 0.6.0"
22
+ gem.add_runtime_dependency "queue_classic", "~> 2.0.1"
23
+
24
+ gem.add_development_dependency "minitest", "~> 3.3.0"
25
+ gem.add_development_dependency "minitest-reporters"
26
+ gem.add_development_dependency 'rake'
27
+ gem.add_development_dependency "pry"
28
+ end
@@ -0,0 +1,101 @@
1
+ require_relative "./test_helper"
2
+
3
+ class Rails3Mailer < ActionMailer::Base
4
+ include QC::Mailer
5
+ default :from => "from@example.org", :subject => "Subject"
6
+
7
+ def test_mail(*params)
8
+ mail(*params)
9
+ end
10
+ end
11
+
12
+ class PriorityMailer < Rails3Mailer
13
+ self.queue_name = 'priority-mailer'
14
+ end
15
+
16
+
17
+ describe 'QC::Mailer' do
18
+ include QCHelper
19
+
20
+ before do
21
+ ActionMailer::Base.deliveries = []
22
+ @test_mail = Rails3Mailer.test_mail( 'to' => "testme@example.org" )
23
+ end
24
+
25
+ describe "default_queue" do
26
+ it "should be 'default'" do
27
+ QC::Mailer.default_queue.must_equal("default")
28
+ end
29
+ end
30
+
31
+ describe "queue_name" do
32
+ before do
33
+ QC::Mailer.default_queue = nil
34
+ Rails3Mailer.queue_name = nil
35
+ end
36
+
37
+ it "allows overriding of the default queue target" do
38
+ QC::Mailer.default_queue = "my-queue"
39
+ Rails3Mailer.queue_name.must_equal("my-queue")
40
+ end
41
+
42
+ it "set queue_name manually" do
43
+ Rails3Mailer.queue_name = "testing"
44
+ Rails3Mailer.queue_name.must_equal("testing")
45
+ end
46
+
47
+ it "set at class level" do
48
+ QC::Mailer.default_queue.must_equal("default")
49
+ Rails3Mailer.queue_name.must_equal("default")
50
+ PriorityMailer.queue_name.must_equal('priority-mailer')
51
+ end
52
+ end
53
+
54
+ describe "queue" do
55
+ it "should be a QC::Queue" do
56
+ Rails3Mailer.queue.must_be_instance_of(QC::Queue)
57
+ end
58
+ end
59
+
60
+ describe '#deliver' do
61
+ it 'should not deliver the email synchronously' do
62
+ @test_mail.deliver
63
+ ActionMailer::Base.deliveries.must_be_empty
64
+ end
65
+
66
+ it 'should place the deliver action on the QC "mailer" queue' do
67
+ Rails3Mailer.queue.count.must_equal(0)
68
+ @test_mail.deliver
69
+ Rails3Mailer.queue.count.must_equal(1)
70
+ end
71
+ end
72
+
73
+ describe "#deliver job" do
74
+ it "should process the job" do
75
+ Rails3Mailer.queue.stub(:enqueue, enqueue_stub) do
76
+ ActionMailer::Base.deliveries.must_be_empty
77
+ @test_mail.deliver
78
+ ActionMailer::Base.deliveries.wont_be_empty
79
+ end
80
+ end
81
+ end
82
+
83
+ describe '#deliver!' do
84
+ it "should send synchronously" do
85
+ @test_mail.deliver!
86
+ ActionMailer::Base.deliveries.wont_be_empty
87
+ end
88
+ end
89
+
90
+ describe 'original mail methods' do
91
+ it 'should be preserved' do
92
+ @test_mail.subject.must_equal 'Subject'
93
+ @test_mail.from.must_include 'from@example.org'
94
+ @test_mail.to.must_include 'testme@example.org'
95
+ end
96
+
97
+ it "to_s should have Content-Type" do
98
+ @test_mail.to_s.must_include "Content-Type:"
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,31 @@
1
+ module QCHelper
2
+
3
+ def setup
4
+ init_db
5
+ silence_logger
6
+ end
7
+
8
+ def teardown
9
+ QC.delete_all
10
+ end
11
+
12
+ def silence_logger
13
+ QC.define_singleton_method :log do |*args| nil; end # silence QC logger
14
+ end
15
+
16
+ def init_db(table_name="queue_classic_jobs")
17
+ QC::Conn.execute("SET client_min_messages TO 'warning'")
18
+ QC::Setup.drop
19
+ QC::Setup.create
20
+ QC::Conn.disconnect
21
+ end
22
+
23
+ def enqueue_stub
24
+ proc do |method, *args|
25
+ klass = eval(method.split(".").first)
26
+ msg = method.split(".").last
27
+ klass.send(msg, *args)
28
+ end
29
+ end
30
+
31
+ end
@@ -0,0 +1,20 @@
1
+ # test/test_helper.rb
2
+
3
+ ENV["TEST"] = 'true'
4
+ ENV["DATABASE_URL"] ||= "postgres:///queue_classic_test"
5
+
6
+ $:.unshift File.expand_path("../../lib")
7
+ require 'rubygems'
8
+ require 'minitest/autorun'
9
+ require "minitest/reporters"
10
+ require 'pry'
11
+ require 'time'
12
+ require 'action_mailer'
13
+ require 'qc-mailer'
14
+
15
+ MiniTest::Reporters.use! [MiniTest::Reporters::SpecReporter.new]
16
+
17
+ ActionMailer::Base.delivery_method = :test
18
+ # ActionMailer::Base.perform_deliveries = false
19
+
20
+ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each {|f| require f}
metadata ADDED
@@ -0,0 +1,195 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qc-mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - David Bradford
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: actionmailer
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '3.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: '3.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: i18n
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.6.0
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.6.0
62
+ - !ruby/object:Gem::Dependency
63
+ name: queue_classic
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: 2.0.1
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: 2.0.1
78
+ - !ruby/object:Gem::Dependency
79
+ name: minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: 3.3.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: 3.3.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: minitest-reporters
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: rake
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: pry
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
+ description: Rails plugin for sending asynchronous email with ActionMailer and QueueClassic.
143
+ email:
144
+ - david@zerobearing.com
145
+ executables: []
146
+ extensions: []
147
+ extra_rdoc_files: []
148
+ files:
149
+ - .gitignore
150
+ - .rvmrc
151
+ - .travis.yml
152
+ - Gemfile
153
+ - README.md
154
+ - Rakefile
155
+ - lib/qc-mailer.rb
156
+ - lib/qc-mailer/message_decoy.rb
157
+ - lib/qc-mailer/version.rb
158
+ - qc-mailer.gemspec
159
+ - test/qc_mailer_test.rb
160
+ - test/support/qc_helper.rb
161
+ - test/test_helper.rb
162
+ homepage: https://github.com/zerobearing2/qc-mailer
163
+ licenses: []
164
+ post_install_message:
165
+ rdoc_options: []
166
+ require_paths:
167
+ - lib
168
+ required_ruby_version: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ segments:
175
+ - 0
176
+ hash: -851662944801137892
177
+ required_rubygems_version: !ruby/object:Gem::Requirement
178
+ none: false
179
+ requirements:
180
+ - - ! '>='
181
+ - !ruby/object:Gem::Version
182
+ version: '0'
183
+ segments:
184
+ - 0
185
+ hash: -851662944801137892
186
+ requirements: []
187
+ rubyforge_project:
188
+ rubygems_version: 1.8.24
189
+ signing_key:
190
+ specification_version: 3
191
+ summary: Rails plugin for sending asynchronous email with ActionMailer and QueueClassic.
192
+ test_files:
193
+ - test/qc_mailer_test.rb
194
+ - test/support/qc_helper.rb
195
+ - test/test_helper.rb