sidekiq_mailer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .rvmrc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sidekiq_mailer.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Anderson Dias
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Sidekiq::Mailer
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sidekiq_mailer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sidekiq_mailer
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env rake
2
+ # encoding: UTF-8
3
+ require "bundler/gem_tasks"
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'test'
9
+ t.pattern = 'test/**/*_test.rb'
10
+ t.verbose = true
11
+ end
12
+
13
+ desc "Run tests"
14
+ task :default => :test
@@ -0,0 +1,48 @@
1
+ require 'sidekiq_mailer/version'
2
+ require 'sidekiq_mailer/worker'
3
+ require 'sidekiq_mailer/proxy'
4
+
5
+ module Sidekiq
6
+ module Mailer
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ base.class_attribute :sidekiq_options_hash
10
+ end
11
+
12
+ module ClassMethods
13
+ ##
14
+ # Allows customization for this type of Worker.
15
+ # Legal options:
16
+ #
17
+ # :queue - use a named queue for this Worker, default 'default'
18
+ # :retry - enable the RetryJobs middleware for this Worker, default *true*
19
+ # :timeout - timeout the perform method after N seconds, default *nil*
20
+ # :backtrace - whether to save any error backtrace in the retry payload to display in web UI,
21
+ # can be true, false or an integer number of lines to save, default *false*
22
+ def sidekiq_options(opts={})
23
+ self.sidekiq_options_hash = get_sidekiq_options.merge(stringify_keys(opts || {}))
24
+ end
25
+
26
+ DEFAULT_OPTIONS = { 'retry' => false, 'queue' => 'mailer' }
27
+
28
+ def get_sidekiq_options # :nodoc:
29
+ self.sidekiq_options_hash ||= DEFAULT_OPTIONS
30
+ end
31
+
32
+ def stringify_keys(hash) # :nodoc:
33
+ hash.keys.each do |key|
34
+ hash[key.to_s] = hash.delete(key)
35
+ end
36
+ hash
37
+ end
38
+
39
+ def method_missing(method_name, *args)
40
+ if action_methods.include?(method_name.to_s)
41
+ Sidekiq::Mailer::Proxy.new(self, method_name, *args)
42
+ else
43
+ super
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,53 @@
1
+ class Sidekiq::Mailer::Proxy
2
+ delegate :to_s, :to => :actual_message
3
+
4
+ def initialize(mailer_class, method_name, *args)
5
+ @mailer_class = mailer_class
6
+ @method_name = method_name
7
+ *@args = *args
8
+ end
9
+
10
+ def actual_message
11
+ @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
12
+ end
13
+
14
+ def deliver
15
+ params = {
16
+ 'class' => Sidekiq::Mailer::Worker,
17
+ 'args' => [@mailer_class.to_s, @method_name, *@args]
18
+ }
19
+ Sidekiq::Mailer::Worker.client_push(params.merge(@mailer_class.get_sidekiq_options))
20
+ end
21
+
22
+ # def deliver_at(time)
23
+ # return deliver! if environment_excluded?
24
+
25
+ # unless resque.respond_to? :enqueue_at
26
+ # raise "You need to install resque-scheduler to use deliver_at"
27
+ # end
28
+
29
+ # if @mailer_class.deliver?
30
+ # resque.enqueue_at(time, @mailer_class, @method_name, *@args)
31
+ # end
32
+ # end
33
+
34
+ # def deliver_in(time)
35
+ # return deliver! if environment_excluded?
36
+
37
+ # unless resque.respond_to? :enqueue_in
38
+ # raise "You need to install resque-scheduler to use deliver_in"
39
+ # end
40
+
41
+ # if @mailer_class.deliver?
42
+ # resque.enqueue_in(time, @mailer_class, @method_name, *@args)
43
+ # end
44
+ # end
45
+
46
+ def deliver!
47
+ actual_message.deliver!
48
+ end
49
+
50
+ def method_missing(method_name, *args)
51
+ actual_message.send(method_name, *args)
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ module Sidekiq
2
+ module Mailer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ class Sidekiq::Mailer::Worker
2
+ include Sidekiq::Worker
3
+
4
+ def perform(mailer_class, action, params)
5
+ mailer_class.constantize.send(action, *params).deliver!
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sidekiq_mailer/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Anderson Dias"]
6
+ gem.email = ["andersondaraujo@gmail.com"]
7
+ gem.description = %q{Asynchronous mail delivery using sidekiq}
8
+ gem.summary = %q{Turning ActiveMailer deliveries asynchronous using the power of sidekiq}
9
+ gem.homepage = "http://github.com/andersondias/sidekiq_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 = "sidekiq_mailer"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Sidekiq::Mailer::VERSION
17
+
18
+ gem.add_dependency("activesupport", "~> 3.0")
19
+ gem.add_dependency("actionmailer", "~> 3.0")
20
+ gem.add_dependency("sidekiq", "~> 2.3")
21
+ end
@@ -0,0 +1,68 @@
1
+ require 'test_helper'
2
+
3
+ class BasicMailer < ActionMailer::Base
4
+ include Sidekiq::Mailer
5
+
6
+ default :from => "from@example.org", :subject => "Subject"
7
+
8
+ def welcome(to)
9
+ mail(to: to)
10
+ end
11
+
12
+ def hi(to, name)
13
+ mail(to: to)
14
+ end
15
+ end
16
+
17
+ class MailerInAnotherQueue < ActionMailer::Base
18
+ include Sidekiq::Mailer
19
+ sidekiq_options queue: 'priority', retry: 'false'
20
+
21
+ default :from => "from@example.org", :subject => "Subject"
22
+
23
+ def bye(to)
24
+ mail(to: to)
25
+ end
26
+ end
27
+
28
+ class SidekiqMailerTest < Test::Unit::TestCase
29
+ def setup
30
+ ActionMailer::Base.deliveries.clear
31
+ Sidekiq::Mailer::Worker.jobs.clear
32
+ end
33
+
34
+ def test_queue_a_new_job
35
+ BasicMailer.hi('test@test.com', 'Tester').deliver
36
+
37
+ job_args = Sidekiq::Mailer::Worker.jobs.first['args']
38
+ expected_args = ['BasicMailer', :hi, 'test@test.com', 'Tester']
39
+ assert_equal expected_args, job_args
40
+ end
41
+
42
+ def test_queues_at_mailer_queue_by_default
43
+ BasicMailer.welcome('test@test.com').deliver
44
+ assert_equal 'mailer', Sidekiq::Mailer::Worker.jobs.first['queue']
45
+ end
46
+
47
+ def test_enables_sidekiq_options_overriding
48
+ MailerInAnotherQueue.bye('test@test.com').deliver
49
+ assert_equal 'priority', Sidekiq::Mailer::Worker.jobs.first['queue']
50
+ assert_equal 'false', Sidekiq::Mailer::Worker.jobs.first['retry']
51
+ end
52
+
53
+ def test_delivers_asynchronously
54
+ BasicMailer.welcome('test@test.com').deliver
55
+ assert_equal 1, Sidekiq::Mailer::Worker.jobs.size
56
+ assert_equal 0, ActionMailer::Base.deliveries.size
57
+ end
58
+
59
+ def test_can_deliver_now
60
+ BasicMailer.welcome('test@test.com').deliver!
61
+ assert_equal 1, ActionMailer::Base.deliveries.size
62
+ end
63
+
64
+ def test_realy_delivers_email_when_performing_worker_job
65
+ Sidekiq::Mailer::Worker.new.perform('BasicMailer', 'welcome', 'test@test.com')
66
+ assert_equal 1, ActionMailer::Base.deliveries.size
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = 'test'
2
+ require 'test/unit'
3
+
4
+ require 'action_mailer'
5
+ ActionMailer::Base.delivery_method = :test
6
+
7
+ require 'sidekiq'
8
+ require 'sidekiq/testing'
9
+
10
+ require 'sidekiq_mailer'
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sidekiq_mailer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Anderson Dias
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-09 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: sidekiq
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.3'
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: '2.3'
62
+ description: Asynchronous mail delivery using sidekiq
63
+ email:
64
+ - andersondaraujo@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - lib/sidekiq_mailer.rb
75
+ - lib/sidekiq_mailer/proxy.rb
76
+ - lib/sidekiq_mailer/version.rb
77
+ - lib/sidekiq_mailer/worker.rb
78
+ - sidekiq_mailer.gemspec
79
+ - test/sidekiq_mailer_test.rb
80
+ - test/test_helper.rb
81
+ homepage: http://github.com/andersondias/sidekiq_mailer
82
+ licenses: []
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Turning ActiveMailer deliveries asynchronous using the power of sidekiq
105
+ test_files:
106
+ - test/sidekiq_mailer_test.rb
107
+ - test/test_helper.rb