mail_queue 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ binlog*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in mail_queue.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mail_queue (0.1.0)
5
+ mail (~> 2.2.14)
6
+ stalker (>= 0.6.1)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ activesupport (3.0.3)
12
+ beanstalk-client (1.1.0)
13
+ diff-lcs (1.1.2)
14
+ i18n (0.5.0)
15
+ json_pure (1.4.6)
16
+ mail (2.2.14)
17
+ activesupport (>= 2.3.6)
18
+ i18n (>= 0.4.0)
19
+ mime-types (~> 1.16)
20
+ treetop (~> 1.4.8)
21
+ mime-types (1.16)
22
+ polyglot (0.3.1)
23
+ rspec (2.4.0)
24
+ rspec-core (~> 2.4.0)
25
+ rspec-expectations (~> 2.4.0)
26
+ rspec-mocks (~> 2.4.0)
27
+ rspec-core (2.4.0)
28
+ rspec-expectations (2.4.0)
29
+ diff-lcs (~> 1.1.2)
30
+ rspec-mocks (2.4.0)
31
+ stalker (0.6.1)
32
+ beanstalk-client
33
+ json_pure
34
+ treetop (1.4.9)
35
+ polyglot (>= 0.3.1)
36
+
37
+ PLATFORMS
38
+ ruby
39
+
40
+ DEPENDENCIES
41
+ bundler (>= 1.0.0.rc.5)
42
+ mail (~> 2.2.14)
43
+ mail_queue!
44
+ rspec (~> 2.4.0)
45
+ stalker (>= 0.6.1)
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
data/lib/mail_queue.rb ADDED
@@ -0,0 +1,47 @@
1
+ require 'stalker'
2
+
3
+ module MailQueue
4
+ class Beanstalk
5
+
6
+ def initialize(options = {})
7
+ self.settings = {
8
+ :pri => 65536,
9
+ :delay => 0,
10
+ :ttr => 120,
11
+ :tube => 'email.send'
12
+ }.merge!(options)
13
+ end
14
+
15
+ attr_accessor :settings
16
+
17
+
18
+ def deliver!(mail)
19
+ # render mail and push onto queue
20
+ envelope_from = mail.return_path || mail.sender || mail.from_addrs.first
21
+ if envelope_from.blank?
22
+ raise ArgumentError.new('A sender (Return-Path, Sender or From) is required to send a message')
23
+ end
24
+
25
+ destinations ||= mail.destinations if mail.respond_to?(:destinations) && mail.destinations
26
+ if destinations.blank?
27
+ raise ArgumentError.new('At least one recipient (To, Cc or Bcc) is required to send a message')
28
+ end
29
+
30
+ message ||= mail.encoded if mail.respond_to?(:encoded)
31
+ if message.blank?
32
+ raise ArgumentError.new('Encoded content is required to send a message')
33
+ end
34
+
35
+ envelope = {
36
+ :from => envelope_from,
37
+ :destinations => destinations,
38
+ :message => message
39
+ }
40
+
41
+ Stalker.enqueue(settings[:tube], envelope, settings)
42
+
43
+ self
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module MailQueue
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
3
+ require 'mail_queue/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mail_queue"
7
+ s.version = MailQueue::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Han Kessels"]
10
+ s.email = ["han.kessels@gmail.com"]
11
+ s.homepage = "https://github.com/han/mailqueue"
12
+ s.summary = "Delivery method for Mail gem to deliver to a beanstalk queue"
13
+ s.description = "Delivery method for Mail gem. Delivers mail to a beanstalk queue"
14
+
15
+ s.required_rubygems_version = ">= 1.3.6"
16
+ s.rubyforge_project = "mail_queue"
17
+
18
+ s.add_dependency "stalker", ">= 0.6.1"
19
+ s.add_dependency "mail", "~> 2.2.14"
20
+ s.add_development_dependency "bundler", ">= 1.0.0.rc.5"
21
+ s.add_development_dependency "rspec", "~> 2.4.0 "
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.executables = `git ls-files`.split("\n").select{|f| f =~ /^bin/}
25
+ s.require_path = 'lib'
26
+ end
data/readme.md ADDED
@@ -0,0 +1,63 @@
1
+ MailQueue
2
+ =========
3
+
4
+
5
+ MailQueue is an asynchronous mail delivery method for the Mail gem. It delivers mail to a [Beanstalk](http://kr.github.com/beanstalkd/) queue. Worker processes can then pick up the mail from the queue and send it on over SMTP or the like.
6
+
7
+ Installation
8
+ ------------
9
+
10
+ gem install mail_queue
11
+
12
+ Configuration
13
+ -------------
14
+
15
+ + Mail gem:
16
+
17
+ Mail.defaults { delivery_method MailQueue::Beanstalk, :tube => 'rculosis' }
18
+
19
+ + Rails:
20
+
21
+ ActionMailer::Base.delivery_method = MailQueue::Beanstalk
22
+
23
+ or, if you insist on changing the default options:
24
+
25
+ ActionMailer::Base.add_delivery_method(:beanstalk, MailQueue::Beanstalk)
26
+ ActionMailer::Base.beanstalk_settings = {
27
+ :tube => "email"
28
+ }
29
+ ActionMailer::Base.delivery_method = :beanstalk
30
+
31
+
32
+ options are:
33
+ * :pri priority, from 0 to 2^32, 0 being the highest, 65536 the default
34
+ * :ttr time to run, default 120 (seconds)
35
+ * :delay how many seconds before the job is put on the ready queue (default 0)
36
+ * :tube tube name used on beanstalk, defaults to 'email.send'
37
+
38
+
39
+ Worker
40
+ ------
41
+
42
+ Have a worker pull the mails off the queue.
43
+ For example, using [Stalker](http://github.com/adamwiggins/stalker):
44
+
45
+ smtp = Net::SMTP.new('smtp.example.com', 25)
46
+ job 'email.send' do |args|
47
+ smtp.start('example.com', 'me', 'mypassword', 'plain') do |smtp|
48
+ smtp.sendmail(args['message'], args['from'], args['destinations'])
49
+ end
50
+ end
51
+
52
+ See [Stalker](http://github.com/adamwiggins/stalker) documentation for how to run a job.
53
+
54
+
55
+ Meta
56
+ ----
57
+
58
+ Created by Han Kessels
59
+
60
+ Released under the MIT License: http://www.opensource.org/licenses/mit-license.php
61
+
62
+ http://github.com/han/mailqueue
63
+
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+ require 'mail'
3
+ require 'stalker'
4
+ require 'mail_queue'
5
+ # require 'ruby-debug'
6
+ # Debugger.start
7
+
8
+ def Stalker.log(msg)
9
+ end
10
+
11
+
12
+ describe "beanstalk" do
13
+ it "should be running" do
14
+ Stalker.beanstalk.stats
15
+ end
16
+ end
17
+
18
+ describe MailQueue do
19
+
20
+ it "should be valid" do
21
+ MailQueue.should be_a(Module)
22
+ end
23
+
24
+ describe "sending a mail"
25
+
26
+ before(:each) do
27
+ @mail = Mail.new do
28
+ from 'sjeng@example.com'
29
+ to 'sjeng@example.com'
30
+ subject 'test from MailQueue'
31
+ body 'this is my body'
32
+ end
33
+ @mail.delivery_method MailQueue::Beanstalk
34
+ @msg = @mail.to_s
35
+ end
36
+
37
+ it "should use Mail to serialize an e-mail" do
38
+ @msg.should match /From: sjeng@example\.com/
39
+ @msg.should match /this is my body/
40
+ @msg.should_not match /and my soul/
41
+ end
42
+
43
+
44
+ it "should put the message on a queue" do
45
+ @mail.deliver!
46
+
47
+ Stalker.job 'email.send' do |args|
48
+ msg = args['message']
49
+ msg.should match /From: sjeng@example\.com/
50
+ msg.should match /this is my body/
51
+ msg.should_not match /and my soul/
52
+ args['from'].should == "sjeng@example.com"
53
+ args['destinations'].should == ["sjeng@example.com"]
54
+ end
55
+ Stalker.prep
56
+ Stalker.work_one_job
57
+
58
+ end
59
+
60
+ it "should validate presence of from attribute" do
61
+ expect {
62
+ @mail.from = ""
63
+ @mail.deliver!
64
+ }.to raise_error(ArgumentError)
65
+ end
66
+
67
+ it "should validate presence of to attribute" do
68
+ expect {
69
+ @mail.to = []
70
+ @mail.deliver!
71
+ }.to raise_error(ArgumentError)
72
+ end
73
+
74
+ it "should put a message on the correct tube" do
75
+ @mail.delivery_method.settings["tube"] = "boe"
76
+ @mail.deliver!
77
+
78
+ Stalker.job 'boe' do |args|
79
+ msg = args['message']
80
+ msg.should match /From: sjeng@example\.com/
81
+ msg.should match /this is my body/
82
+ msg.should_not match /and my soul/
83
+ args['from'].should == "sjeng@example.com"
84
+ args['destinations'].should == ["sjeng@example.com"]
85
+ end
86
+ Stalker.prep
87
+ Stalker.work_one_job
88
+
89
+ end
90
+
91
+ end
@@ -0,0 +1,10 @@
1
+
2
+ RSpec.configure do |config|
3
+ # Remove this line if you don't want RSpec's should and should_not
4
+ # methods or matchers
5
+ require 'rspec/expectations'
6
+ config.include RSpec::Matchers
7
+
8
+ # == Mock Framework
9
+ config.mock_with :rspec
10
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_queue
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Han Kessels
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-17 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: stalker
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 5
30
+ segments:
31
+ - 0
32
+ - 6
33
+ - 1
34
+ version: 0.6.1
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: mail
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 27
46
+ segments:
47
+ - 2
48
+ - 2
49
+ - 14
50
+ version: 2.2.14
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: bundler
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 15424063
62
+ segments:
63
+ - 1
64
+ - 0
65
+ - 0
66
+ - rc
67
+ - 5
68
+ version: 1.0.0.rc.5
69
+ type: :development
70
+ version_requirements: *id003
71
+ - !ruby/object:Gem::Dependency
72
+ name: rspec
73
+ prerelease: false
74
+ requirement: &id004 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ~>
78
+ - !ruby/object:Gem::Version
79
+ hash: 31
80
+ segments:
81
+ - 2
82
+ - 4
83
+ - 0
84
+ version: 2.4.0
85
+ type: :development
86
+ version_requirements: *id004
87
+ description: Delivery method for Mail gem. Delivers mail to a beanstalk queue
88
+ email:
89
+ - han.kessels@gmail.com
90
+ executables: []
91
+
92
+ extensions: []
93
+
94
+ extra_rdoc_files: []
95
+
96
+ files:
97
+ - .gitignore
98
+ - Gemfile
99
+ - Gemfile.lock
100
+ - Rakefile
101
+ - lib/mail_queue.rb
102
+ - lib/mail_queue/version.rb
103
+ - mail_queue.gemspec
104
+ - readme.md
105
+ - spec/mail_queue_spec.rb
106
+ - spec/spec_helper.rb
107
+ has_rdoc: true
108
+ homepage: https://github.com/han/mailqueue
109
+ licenses: []
110
+
111
+ post_install_message:
112
+ rdoc_options: []
113
+
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ none: false
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ hash: 23
131
+ segments:
132
+ - 1
133
+ - 3
134
+ - 6
135
+ version: 1.3.6
136
+ requirements: []
137
+
138
+ rubyforge_project: mail_queue
139
+ rubygems_version: 1.3.7
140
+ signing_key:
141
+ specification_version: 3
142
+ summary: Delivery method for Mail gem to deliver to a beanstalk queue
143
+ test_files: []
144
+