george-sanitize_email 0.2.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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Peter H. Boling of 9thBit LLC
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,116 @@
1
+ = sanitize_email
2
+
3
+ This gem allows you to globally override your mail delivery settings. It's particularly helpful when you want to omit the delivery of email (e.g. in development/test environments) or alter the to/cc/bcc (e.g. in staging or demo environments) of all email generated from your application.
4
+
5
+ It is a "configure it and forget it" type gem that requires very little setup. It includes some very innocuous monkey patching of ActionMailer::Base to work its magic.
6
+
7
+ Peter Boling wrote the plugin that this gem is derived from. It currently solves three common problems in ruby web applications that use ActionMailer:
8
+
9
+ === Working Locally with Production Data
10
+
11
+ Peter described this common problem in his original plugin implementation as such:
12
+
13
+ # I have a production site with live data.
14
+ # I dump the live data and securely transfer it to another machine (rync -e ssh), and import it using scripts that I will soon open source to.
15
+ # On this separate machine (staging, or development) I run tests, and test various features.
16
+ # I usually want the emails to get sent from these non-production environments so I can verify what they look like when sent, but I don't ever want to risk them getting sent to addresses that are not mine.
17
+
18
+ === Re-routing Email on a Staging or QA Server
19
+
20
+ Another very important use case for me is to transparently re-route email generated from a staging or QA server to an appropriate person. For example, it's common for us to set up a staging server for a client to use to view our progress and test out new features. It's important for any email that is generated from our web application be delivered to the client's inbox so that they can review the content and ensure that it's acceptable. Similarly, we set up QA instances for our own QA team and we use rails-caddy to allow each QA person to configure it specifically for them.
21
+
22
+ === Testing Email from a Hot Production Server
23
+
24
+ If you install this gem on a production server (which I don't always do), you can load up script/console and override the to/cc/bcc on all emails for the duration of your console session. This allows you to poke and prod a live production instance, and route all email to your own inbox for inspection. The best part is that this can all be accomplished without changing a single line of your application code.
25
+
26
+ == Install
27
+
28
+ gem sources -a http://gems.github.com/
29
+ gem install jtrupiano-sanitize_email
30
+
31
+ == Setup
32
+
33
+ It only requires a few lines of configuration:
34
+
35
+ * Rails 1.x: Add to bottom of environment.rb
36
+
37
+ * Rails 2.x: Use an initializer, stick it in any initializer file, or create a new one for sanitize_email
38
+
39
+ Add this bit and customize for your app:
40
+
41
+ <pre>
42
+ # Settings for sanitize_email gem. These can be overridden in individual config/%env%/environment.rb files.
43
+
44
+ require 'sanitize_email'
45
+ ActionMailer::Base.sanitized_recipients = "jtrupiano@gmail.com"
46
+ ActionMailer::Base.sanitized_bcc = nil
47
+ ActionMailer::Base.sanitized_cc = nil
48
+
49
+ # optionally, you can configure sanitize_email to to include the "real" email address as the 'user name' of the
50
+ # "sanitized" email (e.g. "real@address.com <sanitized@email.com>")
51
+ ActionMailer::Base.use_actual_email_as_sanitized_user_name = true # defaults to false
52
+
53
+ # These are the environments whose outgoing email BCC, CC and recipients fields will be overridden!
54
+ # All environments not listed will be treated as normal.
55
+ ActionMailer::Base.local_environments = %w( development test staging )
56
+ </pre>
57
+
58
+ Keep in mind, this is ruby (and possibly rails), so you can add conditionals or utilize different environment.rb files to customize these settings on a per-environment basis.
59
+
60
+ But wait there's more:
61
+
62
+ Let's say you have a method in your model that you can call to test the signup email.
63
+ You want to be able to test sending it to any user at any time... but you don't want
64
+ the user to ACTUALLY get the email, even in production. A dilemma, yes? Not anymore!
65
+
66
+ All your mailers get a force_sanitize class method which takes precedence over the environment override.
67
+
68
+ When force_sanitize is nil it will not be used by sanitize_email to determine if it should override the recipients, bcc, and cc.
69
+
70
+
71
+ == Example
72
+
73
+ So here's how you can use force_sanitize to override the override.
74
+
75
+ Even if you set:
76
+
77
+ ActionMailer::Base.local_environments = %w( development )
78
+
79
+ and are in the development environment, you can override the override anywhere in your code.
80
+
81
+ <pre>
82
+ class User < ActiveRecord::Base
83
+ def test_signup_email_me_only
84
+ UserMailer.force_sanitize = true
85
+ UserMailer.deliver_signup_notification(self)
86
+ UserMailer.force_sanitize = nil
87
+ end
88
+
89
+ def test_signup_email_user_only
90
+ UserMailer.force_sanitize = false
91
+ UserMailer.deliver_signup_notification(self)
92
+ UserMailer.force_sanitize = nil
93
+ end
94
+
95
+ # this third method would conditionally use the overridden recipients based on current Rails environment
96
+ def test_signup_email_environment
97
+ UserMailer.deliver_signup_notification(self)
98
+ end
99
+
100
+ end
101
+ </pre>
102
+
103
+ Load the console with ruby script/console and regardless of what environment you are in:
104
+
105
+ > User.find(4).test_signup_email_me_only
106
+
107
+ and the email will have it's recipients, bcc, and cc overridden to be whatever you set the sanitized values to be.
108
+ Then if you want to send it to the actual user, instead of yourself
109
+
110
+ > User.find(4).test_signup_email_user_only
111
+
112
+ That's it! Enjoy!
113
+
114
+ Copyright (c) 2009 John Trupiano of SmartLogic Solutions, LLC
115
+ Copyright (c) 2008 Peter H. Boling of 9thBit LLC
116
+ Released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,40 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |s|
8
+ s.name = "sanitize_email"
9
+ #s.executables = "jeweler"
10
+ s.summary = "Gemified fork of pboling's sanitize_email plugin: allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses."
11
+ s.email = "jtrupiano@gmail.com"
12
+ s.homepage = "http://github.com/jtrupiano/sanitize_email"
13
+ s.description = "allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses"
14
+ s.authors = ["John Trupiano", "Peter Boling"]
15
+ #s.files = FileList["[A-Z]*", "{bin,generators,lib,test}/**/*", 'lib/jeweler/templates/.gitignore']
16
+ #s.add_dependency 'schacon-git'
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler, or one of its dependencies, is not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
20
+ end
21
+
22
+ desc 'Default: run unit tests.'
23
+ task :default => :test
24
+
25
+ desc 'Test the sanitize_email plugin.'
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.pattern = 'test/**/*_test.rb'
29
+ t.verbose = true
30
+ end
31
+
32
+ desc 'Generate documentation for the sanitize_email plugin.'
33
+ Rake::RDocTask.new(:rdoc) do |rdoc|
34
+ rdoc.rdoc_dir = 'rdoc'
35
+ rdoc.title = 'SanitizeEmail'
36
+ rdoc.options << '--line-numbers' << '--inline-source'
37
+ rdoc.rdoc_files.include('README')
38
+ rdoc.rdoc_files.include('lib/**/*.rb')
39
+ end
40
+
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 2
4
+ :patch: 0
@@ -0,0 +1,5 @@
1
+ require File.join(File.dirname(__FILE__), "sanitize_email", "sanitize_email")
2
+ require File.join(File.dirname(__FILE__), "sanitize_email", "custom_environments")
3
+
4
+ ActionMailer::Base.send :include, NinthBit::CustomEnvironments
5
+ ActionMailer::Base.send :include, NinthBit::SanitizeEmail
@@ -0,0 +1,27 @@
1
+ #Copyright (c) 2008 Peter H. Boling of 9thBit LLC
2
+ #Released under the MIT license
3
+
4
+ module NinthBit
5
+ module CustomEnvironments
6
+
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+
10
+ base.cattr_accessor :local_environments
11
+ base.local_environments = %w( development test )
12
+
13
+ base.cattr_accessor :deployed_environments
14
+ base.deployed_environments = %w( production staging )
15
+ end
16
+
17
+ module ClassMethods
18
+ def consider_local?
19
+ local_environments.include?(ENV['RAILS_ENV'])
20
+ end
21
+ def consider_deployed?
22
+ deployed_environments.include?(ENV['RAILS_ENV'])
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,87 @@
1
+ #Copyright (c) 2008 Peter H. Boling of 9thBit LLC
2
+ #Released under the MIT license
3
+
4
+ module NinthBit
5
+ module SanitizeEmail
6
+
7
+ def self.included(base)
8
+
9
+ # Adds the following class attributes to the classes that include NinthBit::SanitizeEmail
10
+ base.cattr_accessor :force_sanitize
11
+ base.force_sanitize = nil
12
+
13
+ # Specify the BCC addresses for the messages that go out in 'local' environments
14
+ base.cattr_accessor :sanitized_bcc
15
+ base.sanitized_bcc = nil
16
+
17
+ # Specify the CC addresses for the messages that go out in 'local' environments
18
+ base.cattr_accessor :sanitized_cc
19
+ base.sanitized_cc = nil
20
+
21
+ # The recipient addresses for the messages, either as a string (for a single
22
+ # address) or an array (for multiple addresses) that go out in 'local' environments
23
+ base.cattr_accessor :sanitized_recipients
24
+ base.sanitized_recipients = nil
25
+
26
+ # Use the 'real' email address as the username for the sanitized email address
27
+ # e.g. "real@email.com <sanitized@email.com>"
28
+ base.cattr_accessor :use_actual_email_as_sanitized_user_name
29
+ base.use_actual_email_as_sanitized_user_name = false
30
+
31
+ base.class_eval do
32
+ # We need to alias these methods so that our new methods get used instead
33
+ alias :real_bcc :bcc
34
+ alias :real_cc :cc
35
+ alias :real_recipients :recipients
36
+
37
+ def localish?
38
+ # consider_local is a method in sanitize_email/lib/custom_environments.rb
39
+ # it is included in ActionMailer in sanitize_email/init.rb
40
+ !self.class.force_sanitize.nil? ? self.class.force_sanitize : self.class.consider_local?
41
+ end
42
+
43
+ def recipients(*addresses)
44
+ real_recipients *addresses
45
+ puts "sanitize_email error: sanitized_recipients is not set" if self.class.sanitized_recipients.nil?
46
+ localish? ? override(:recipients) : real_recipients
47
+ end
48
+
49
+ def cc(*addresses)
50
+ real_cc *addresses
51
+ localish? ? override(:cc) : real_cc
52
+ end
53
+
54
+ def bcc(*addresses)
55
+ real_bcc *addresses
56
+ localish? ? override(:bcc) : real_bcc
57
+ end
58
+
59
+ #######
60
+ private
61
+ #######
62
+
63
+ def override(type)
64
+ real_addresses, sanitized_addresses =
65
+ case type
66
+ when :recipients
67
+ [real_recipients, self.class.sanitized_recipients]
68
+ when :cc
69
+ [real_cc, self.class.sanitized_cc]
70
+ when :bcc
71
+ [real_bcc, self.class.sanitized_bcc]
72
+ else raise "sanitize_email error: unknown email override"
73
+ end
74
+
75
+ return sanitized_addresses if sanitized_addresses.nil? || !self.class.use_actual_email_as_sanitized_user_name
76
+
77
+ out = real_addresses.inject([]) do |result, real_recipient|
78
+ result << sanitized_addresses.map{|sanitized| "#{real_recipient} <#{sanitized}>"}
79
+ result
80
+ end.flatten
81
+ return out
82
+ end
83
+
84
+ end
85
+ end
86
+ end # end Module SanitizeEmail
87
+ end # end Module NinthBit
@@ -0,0 +1,34 @@
1
+
2
+ require 'rubygems'
3
+ require 'action_mailer'
4
+
5
+ # configure ActionMailer
6
+ ActionMailer::Base.template_root = "."
7
+
8
+ require File.join(File.dirname(__FILE__), "..", "lib", 'sanitize_email')
9
+
10
+
11
+ class SampleMailer < ActionMailer::Base
12
+
13
+ def gmail_override
14
+ @recipients = "to_real@email.com"
15
+ @cc = "cc_real@email.com"
16
+ @bcc = "bcc_real@email.com"
17
+
18
+ @subject = "Hello there"
19
+
20
+ part :content_type => "text/html", :body => "Hello there"
21
+ end
22
+
23
+ def gmail_override_multiple_recipient
24
+ @recipients = ["to_0_real@email.com", "to_1_real@email.com"]
25
+ @cc = "cc_real@email.com"
26
+ @bcc = "bcc_real@email.com"
27
+
28
+ @subject = "Hello there, multiple"
29
+
30
+ part :content_type => "text/html", :body => "Hello there, multiple."
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,77 @@
1
+ require 'test/unit'
2
+ require 'test/sample_mailer'
3
+
4
+ class SanitizeEmailTest < Test::Unit::TestCase
5
+ def setup
6
+ ENV['RAILS_ENV'] = 'test'
7
+ end
8
+
9
+ def prepare_sanitizations(to = nil, cc = nil, bcc = nil, use_actual_email_as_sanitized_user_name = false)
10
+ ActionMailer::Base.sanitized_recipients = to
11
+ ActionMailer::Base.sanitized_cc = cc
12
+ ActionMailer::Base.sanitized_bcc = bcc
13
+ ActionMailer::Base.local_environments = %w( test )
14
+ ActionMailer::Base.use_actual_email_as_sanitized_user_name = use_actual_email_as_sanitized_user_name
15
+ end
16
+
17
+ def test_send_can_override_recips_cc_bcc_all_independently
18
+ prepare_sanitizations("to_sanitized@email.com", "cc_sanitized@email.com")
19
+
20
+ tmail = SampleMailer.create_gmail_override
21
+ assert_equal ["to_sanitized@email.com"], tmail.to
22
+ assert_equal ["cc_sanitized@email.com"], tmail.cc
23
+ assert_equal nil, tmail.bcc
24
+ end
25
+
26
+ def test_to_with_override
27
+ prepare_sanitizations("to_sanitized@email.com", nil, nil, :override_username)
28
+ tmail = SampleMailer.create_gmail_override
29
+ assert_equal "to_real@email.com", tmail.to_addrs[0].name
30
+ assert_equal "to_sanitized@email.com", tmail.to_addrs[0].address
31
+ end
32
+
33
+ def test_tcc_with_override
34
+ prepare_sanitizations("to_sanitized@email.com", "cc_sanitized@email.com", nil, :override_username)
35
+ tmail = SampleMailer.create_gmail_override
36
+ assert_equal "cc_real@email.com", tmail.cc_addrs[0].name
37
+ assert_equal "cc_sanitized@email.com", tmail.cc_addrs[0].address
38
+ end
39
+
40
+ def test_bcc_with_override
41
+ prepare_sanitizations("to_sanitized@email.com", nil, "bcc_sanitized@email.com", :override_username)
42
+ tmail = SampleMailer.create_gmail_override
43
+ assert_equal "bcc_real@email.com", tmail.bcc_addrs[0].name
44
+ assert_equal "bcc_sanitized@email.com", tmail.bcc_addrs[0].address
45
+ end
46
+
47
+ def test_override_with_multiple_santiized_emails
48
+ prepare_sanitizations(["to_0_sanitized@email.com", "to_1_sanitized@email.com"], nil, nil, :override_username)
49
+ tmail = SampleMailer.create_gmail_override
50
+ tmail.to_addrs.each_with_index do |mail, idx|
51
+ assert_equal "to_real@email.com", mail.name
52
+ assert_equal "to_#{idx}_sanitized@email.com", mail.address
53
+ end
54
+ end
55
+
56
+ def test_overriding_multiple_real_addresses
57
+ prepare_sanitizations("to_sanitized@email.com", nil, nil, :override_username)
58
+ tmail = SampleMailer.create_gmail_override_multiple_recipient
59
+ tmail.to_addrs.each_with_index do |mail, idx|
60
+ assert_equal "to_#{idx}_real@email.com", mail.name
61
+ assert_equal "to_sanitized@email.com", mail.address
62
+ end
63
+ end
64
+
65
+ def test_overriding_multiple_real_addresses_with_multiple_sanitized_emails
66
+ prepare_sanitizations(["to_0_sanitized@email.com", "to_1_sanitized@email.com"], nil, nil, :override_username)
67
+ tmail = SampleMailer.create_gmail_override_multiple_recipient
68
+
69
+ assert tmail.to_addrs.map(&:name).include?("to_0_real@email.com")
70
+ assert tmail.to_addrs.map(&:name).include?("to_1_real@email.com")
71
+
72
+ assert tmail.to_addrs.map(&:address).include?("to_0_sanitized@email.com")
73
+ assert tmail.to_addrs.map(&:address).include?("to_1_sanitized@email.com")
74
+ end
75
+
76
+
77
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: george-sanitize_email
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - John Trupiano
8
+ - Peter Boling
9
+ - George Anderson
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-06-09 00:00:00 -07:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses
19
+ email:
20
+ - jtrupiano@gmail.com
21
+ - george@benevolentcode.com
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - lib/sanitize_email/custom_environments.rb
30
+ - lib/sanitize_email/sanitize_email.rb
31
+ - lib/sanitize_email.rb
32
+ - MIT-LICENSE
33
+ - Rakefile
34
+ - README.rdoc
35
+ - VERSION.yml
36
+ - test/sample_mailer.rb
37
+ - test/sanitize_email_test.rb
38
+ has_rdoc: true
39
+ homepage: http://github.com/george/sanitize_email
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --inline-source
43
+ - --charset=UTF-8
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: "0"
51
+ version:
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.2.0
62
+ signing_key:
63
+ specification_version: 2
64
+ summary: "Gemified fork of pboling's sanitize_email plugin: allows you to play with your application's email abilities without worrying that emails will get sent to actual live addresses."
65
+ test_files: []
66
+