mail_rotator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +1 -0
- data/app/models/mailer_store.rb +43 -0
- data/bin/console +7 -0
- data/bin/setup +5 -0
- data/db/schema.rb +14 -0
- data/lib/mail_rotator/switcher.rb +54 -0
- data/lib/mail_rotator/version.rb +3 -0
- data/lib/mail_rotator.rb +10 -0
- data/lib/tasks/tasks.rake +26 -0
- data/mail_rotator.gemspec +30 -0
- data/samples/mail_rotator.rb.sample +35 -0
- data/samples/migration.rb.sample +19 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8844d53a9f13746403464d17b09858ec6092cdab
|
4
|
+
data.tar.gz: 3affa2be85e4b3d8c25837176056c88510be5ceb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb63803027bf31f95f8c6b0dc97f6ad74440b08fd114d581f64a0f2b6ad4ef6dd80b5fc9f319073d39f2b22f98fcbffdd4553656e9492c4589e9abd18608ee2d
|
7
|
+
data.tar.gz: 8689c918259849afad4107674bfc12057656eec851559d32e1ac04696416ada3b47ed578f0de857d3d0d2e729eb79bfa1c6eaa1d78fa1edcd8c17acb89fead6d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
mail_rotator
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.0.0
|
4
|
+
env: RAKE_ENV=test RAILS_ENV=test
|
5
|
+
before_install:
|
6
|
+
- 'gem install bundler'
|
7
|
+
script:
|
8
|
+
- 'bundle exec rspec'
|
9
|
+
notifications:
|
10
|
+
email:
|
11
|
+
recipients:
|
12
|
+
- humzashah+travis@gmail.com
|
13
|
+
on_success: never
|
14
|
+
on_failure: always
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Syed Humza Shah
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# MailRotator :mailbox:
|
2
|
+
|
3
|
+
### What is this?
|
4
|
+
|
5
|
+
The short version: This is a Ruby gem intended to be used with Rails projects.
|
6
|
+
|
7
|
+
The not-so-short version: [Mandrill](https://www.mandrill.com/), [SendGrid](http://sendgrid.com), and similar services are free for the first few emails. But once you've exhausted your period quota, you have to pay! :moneybag:
|
8
|
+
|
9
|
+
So why not string such services together and use the free email quota each one of them provides? With this tool, you can assign different quotas to your mail service accounts. Once a particular account reaches its limit, the next one will kick in!
|
10
|
+
|
11
|
+
Thus you can use this to not pay anything to send email or simply to rotate your email servers.
|
12
|
+
|
13
|
+
### Usage
|
14
|
+
|
15
|
+
Add like any other gem:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'mail_rotator'
|
19
|
+
```
|
20
|
+
|
21
|
+
followed by `bundle install`.
|
22
|
+
|
23
|
+
The sent mails are tracked through a database table. So you will need to create a migration with the contents of [this file](https://raw.githubusercontent.com/humzashah/mail_rotator/master/samples/migration.rb.sample).
|
24
|
+
|
25
|
+
Define the SMTP settings of each of your services in any initializer e.g. `config/initializers/mail_rotator.rb`. You can get a template for the initializer [here](https://raw.githubusercontent.com/humzashah/mail_rotator/master/samples/mail_rotator.rb.sample). Add the limits, settings, any, additional services, and run the following rake task:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
bundle exec rake db:migrate mail_rotator:update_rows
|
29
|
+
```
|
30
|
+
|
31
|
+
Go to the mailer you are using and prepend the `MailRotator` module:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class CustomMailer < ActionMailer::Base
|
35
|
+
prepend MailRotator # which implies that you should have Ruby 2.0.0+
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
You're done! :clap:
|
40
|
+
|
41
|
+
### Get in touch
|
42
|
+
|
43
|
+
You can report an issue if there is a bug; for collaborations, create pull-requests; to offer me pizza, email me! :pizza: humzashah [at] gmail [dot] com
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
module MailRotator
|
4
|
+
class MailerStore < ActiveRecord::Base
|
5
|
+
self.table_name = "mail_rotator_mailer_stores"
|
6
|
+
|
7
|
+
validates :name, :limit, :sent_count, presence: true
|
8
|
+
validates :name, uniqueness: true
|
9
|
+
|
10
|
+
after_save :activate_next_mailer
|
11
|
+
after_save :deactivate_other_mailers
|
12
|
+
|
13
|
+
def activate
|
14
|
+
self.update_attributes(active: true)
|
15
|
+
end
|
16
|
+
|
17
|
+
def increment
|
18
|
+
new_count = self.sent_count + 1
|
19
|
+
self.update_attributes(sent_count: new_count, active: true)
|
20
|
+
end
|
21
|
+
|
22
|
+
def reset
|
23
|
+
self.update_attributes(sent_count: 0, active: false)
|
24
|
+
end
|
25
|
+
|
26
|
+
def over_limit?
|
27
|
+
sent_count >= limit
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def activate_next_mailer
|
33
|
+
return if active
|
34
|
+
next_mailer = self.class.where("id > ?", id).first
|
35
|
+
next_mailer ||= self.class.first
|
36
|
+
next_mailer.update_attributes(sent_count: 0, active: true)
|
37
|
+
end
|
38
|
+
|
39
|
+
def deactivate_other_mailers
|
40
|
+
self.class.where("id != ?", id).update_all(active: false) unless self.active
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/db/schema.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
ActiveRecord::Schema.define(version: 20150510144529) do
|
2
|
+
table_name = "mail_rotator_mailer_stores"
|
3
|
+
|
4
|
+
create_table table_name do |t|
|
5
|
+
t.string :name, null: false
|
6
|
+
t.integer :limit, null: false, default: 1
|
7
|
+
t.integer :sent_count, null: false, default: 0
|
8
|
+
t.boolean :active, default: false
|
9
|
+
|
10
|
+
t.timestamps null: false
|
11
|
+
end
|
12
|
+
|
13
|
+
add_index table_name, :name, unique: true
|
14
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require_relative "../../app/models/mailer_store"
|
2
|
+
|
3
|
+
module MailRotator
|
4
|
+
module Switcher
|
5
|
+
module_function
|
6
|
+
|
7
|
+
def treat(klass)
|
8
|
+
validate_constant
|
9
|
+
validate_rows
|
10
|
+
|
11
|
+
mailer = current_mailer
|
12
|
+
mailer.public_send(mailer.over_limit? ? :reset : :increment)
|
13
|
+
|
14
|
+
assign_mailer(klass, current_mailer)
|
15
|
+
end
|
16
|
+
|
17
|
+
def validate_constant
|
18
|
+
return if Switcher.const_defined?("SMTP_SETTINGS")
|
19
|
+
raise "MailRotator: MailRotator::Switcher::SMTP_SETTINGS is not defined. Refer to the README."
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
module_function
|
24
|
+
|
25
|
+
def validate_rows
|
26
|
+
config_mailer_names = SMTP_SETTINGS.keys.sort
|
27
|
+
db_mailer_names = MailerStore.pluck(:name).sort
|
28
|
+
|
29
|
+
return if (config_mailer_names == db_mailer_names)
|
30
|
+
raise "Please run 'rake db:migrate mail_rotator:update_rows' for RAILS_ENV=#{ENV['RAILS_ENV']}."
|
31
|
+
end
|
32
|
+
|
33
|
+
def assign_mailer(klass, mailer)
|
34
|
+
hash = SMTP_SETTINGS[mailer.name]
|
35
|
+
klass.smtp_settings = hash[:settings] || hash['settings']
|
36
|
+
end
|
37
|
+
|
38
|
+
def current_mailer
|
39
|
+
mailer = active_mailer
|
40
|
+
return mailer if mailer
|
41
|
+
mailer = first_mailer
|
42
|
+
first_mailer.activate
|
43
|
+
first_mailer
|
44
|
+
end
|
45
|
+
|
46
|
+
def active_mailer
|
47
|
+
MailerStore.where(active: true).first
|
48
|
+
end
|
49
|
+
|
50
|
+
def first_mailer
|
51
|
+
MailerStore.first
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
data/lib/mail_rotator.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
namespace :mail_rotator do
|
4
|
+
desc "creates rows MailRotator::MailerStore"
|
5
|
+
task update_rows: :environment do
|
6
|
+
MailRotator::Switcher.validate_constant
|
7
|
+
puts "MailRotator: Creating/updating mailer rows; please wait..."
|
8
|
+
count = 0
|
9
|
+
|
10
|
+
ActiveRecord::Base.transaction do
|
11
|
+
MailRotator::Switcher::SMTP_SETTINGS.each do |name, options|
|
12
|
+
mailer = MailRotator::MailerStore.where(name: name).first_or_initialize(sent_count: 0)
|
13
|
+
mailer.limit = options[:limit] || options['limit']
|
14
|
+
count += 1 if mailer.save!
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# destroy rows no longer valid:
|
19
|
+
MailRotator::MailerStore.where("name NOT IN (?)", MailRotator::Switcher::SMTP_SETTINGS.keys).destroy_all
|
20
|
+
|
21
|
+
# set an active mailer:
|
22
|
+
MailRotator::MailerStore.where("").limit(1).update_all(active: true) unless MailRotator::MailerStore.where(active: true).exists?
|
23
|
+
|
24
|
+
puts "MailRotator: Successfully created/updated #{count} mailer rows."
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'mail_rotator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "mail_rotator"
|
8
|
+
spec.version = MailRotator::VERSION
|
9
|
+
spec.authors = ["Syed Humza Shah"]
|
10
|
+
spec.email = ["humzashah+github@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Rotate services used to send Rails emails."
|
13
|
+
spec.homepage = "https://github.com/humzashah/mail_rotator"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.required_ruby_version = '>= 2.0.0'
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", ">= 1.8"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "sqlite3"
|
26
|
+
spec.add_development_dependency "factory_girl_rails"
|
27
|
+
spec.add_development_dependency "shoulda-matchers"
|
28
|
+
|
29
|
+
spec.add_dependency "rails"
|
30
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
=begin
|
2
|
+
smtp_settings must include the following information:
|
3
|
+
{
|
4
|
+
user_name: username,
|
5
|
+
password: password,
|
6
|
+
domain: domain,
|
7
|
+
address: address,
|
8
|
+
port: port_number,
|
9
|
+
authentication: :plain,
|
10
|
+
enable_starttls_auto: true
|
11
|
+
}
|
12
|
+
=end
|
13
|
+
|
14
|
+
require "mail_rotator"
|
15
|
+
|
16
|
+
module MailRotator
|
17
|
+
module Switcher
|
18
|
+
SMTP_SETTINGS = {
|
19
|
+
"SENDGRID" => {
|
20
|
+
limit: 400,
|
21
|
+
settings: sendgrid_smtp_settings
|
22
|
+
},
|
23
|
+
|
24
|
+
"MANDRILL" => {
|
25
|
+
limit: 1200,
|
26
|
+
settings: mandrill_smtp_settings
|
27
|
+
},
|
28
|
+
|
29
|
+
"GMAIL" => {
|
30
|
+
limit: 100,
|
31
|
+
settings: gmail_smtp_settings
|
32
|
+
}
|
33
|
+
}
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class CreateMailRotatorMailerStores < ActiveRecord::Migration
|
2
|
+
def change
|
3
|
+
create_table table_name do |t|
|
4
|
+
t.string :name, null: false
|
5
|
+
t.integer :limit, null: false, default: 1
|
6
|
+
t.integer :sent_count, null: false, default: 0
|
7
|
+
t.boolean :active, default: false
|
8
|
+
|
9
|
+
t.timestamps null: false
|
10
|
+
end
|
11
|
+
|
12
|
+
add_index table_name, :name, unique: true
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def table_name
|
17
|
+
MailRotator::MailerStore.table_name
|
18
|
+
end
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mail_rotator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Syed Humza Shah
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: factory_girl_rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: shoulda-matchers
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rails
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- humzashah+github@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- .ruby-gemset
|
121
|
+
- .ruby-version
|
122
|
+
- .travis.yml
|
123
|
+
- Gemfile
|
124
|
+
- LICENSE.txt
|
125
|
+
- README.md
|
126
|
+
- Rakefile
|
127
|
+
- app/models/mailer_store.rb
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- db/schema.rb
|
131
|
+
- lib/mail_rotator.rb
|
132
|
+
- lib/mail_rotator/switcher.rb
|
133
|
+
- lib/mail_rotator/version.rb
|
134
|
+
- lib/tasks/tasks.rake
|
135
|
+
- mail_rotator.gemspec
|
136
|
+
- samples/mail_rotator.rb.sample
|
137
|
+
- samples/migration.rb.sample
|
138
|
+
homepage: https://github.com/humzashah/mail_rotator
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: 2.0.0
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - '>='
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.4.6
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Rotate services used to send Rails emails.
|
162
|
+
test_files: []
|