actionmailer_extensions 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Peter MacRobert and Empire42 Ltd (UK)
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.
@@ -0,0 +1,17 @@
1
+ = actionmailer_extensions
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Peter MacRobert. See LICENSE for details.
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "actionmailer_extensions"
8
+ gem.summary = %Q{Handy "save to disk" and "safe recipients" features for ActionMailer}
9
+ gem.description = %Q{Wraps the deliver! method on ActionMailer to save the outgoing mail to a .eml file,
10
+ which can be opened by most email clients. Also provides a mechanism for only sending to an approved list of
11
+ email recipients, which is useful for ensuring your application doesn't send email outside of an organization.}
12
+ gem.email = "originalpete@gmail.com"
13
+ gem.homepage = "http://github.com/originalpete/actionmailer_extensions"
14
+ gem.authors = ["Peter MacRobert"]
15
+ gem.add_development_dependency "yard", ">= 0"
16
+ gem.add_development_dependency "rspec", ">= 1.2.9"
17
+ gem.add_development_dependency "rr", ">= 0.10.5"
18
+ end
19
+ Jeweler::GemcutterTasks.new
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
22
+ end
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :test => :check_dependencies
37
+
38
+ task :default => :spec
39
+
40
+ begin
41
+ require 'yard'
42
+ YARD::Rake::YardocTask.new
43
+ rescue LoadError
44
+ task :yardoc do
45
+ abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
46
+ end
47
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'action_mailer'
3
+
4
+ module ActionmailerExtensions
5
+
6
+ def deliver_with_disk_save!(mail = @mail)
7
+ if save_emails_to_disk
8
+ FileUtils.mkdir_p(email_output_dir) unless File.directory?(email_output_dir)
9
+ filename = "#{Time.now.to_i}_#{mail.to.join(',')}.eml"
10
+ File.open(File.join(email_output_dir, filename), "w+") {|f|
11
+ f << mail.encoded
12
+ }
13
+ end
14
+
15
+ # ensure that the mail's "to" recipients are all contained in the safe_recipients list
16
+ send = case
17
+ when !safe_recipients || safe_recipients.empty? then false
18
+ when safe_recipients.include?(:any) then true
19
+ when (mail.to.map(&:downcase) - safe_recipients.map{|r| r.to_s.downcase}).empty? then true
20
+ end
21
+
22
+ return mail unless send
23
+
24
+ deliver_without_disk_save!(mail)
25
+ mail
26
+ end
27
+
28
+ end
29
+
30
+ module ActionMailer
31
+ class Base
32
+
33
+ # Add some additional class attributes that we need.
34
+ @@safe_recipients = [:any]
35
+ cattr_accessor :safe_recipients
36
+
37
+ @@save_emails_to_disk = false
38
+ cattr_accessor :save_emails_to_disk
39
+
40
+ @@email_output_dir = "/tmp/actionmailer_output_emails"
41
+ cattr_accessor :email_output_dir
42
+
43
+ private
44
+ include ActionmailerExtensions
45
+ alias_method_chain :deliver!, :disk_save
46
+ end
47
+ end
@@ -0,0 +1,142 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe ActionmailerExtensions do
4
+
5
+ # Creates an anonmyous throw-away class of type=parent, with an additional
6
+ # proc for defining methods on the class.
7
+ def new_anon_class(parent, name="", &proc)
8
+ klass = Class.new(parent)
9
+ mc = klass.instance_eval{ class << self ; self ; end }
10
+ mc.send(:define_method, :to_s) {name}
11
+ klass.class_eval(&proc) if proc
12
+ klass
13
+ end
14
+
15
+ # create a new anonymous ActionMailer::Base descendant class
16
+ def new_mailer_class(name, &proc)
17
+ return new_anon_class(ActionMailer::Base, name, proc) if proc
18
+ new_anon_class(ActionMailer::Base, name){
19
+ def test_email(to="someone@example.com")
20
+ from "noreply@nowhere.local"
21
+ recipients to
22
+ subject "test email"
23
+ content_type "text/plain"
24
+ body "test email"
25
+ end
26
+ }
27
+ end
28
+
29
+ before do
30
+ ActionMailer::Base.delivery_method = :test
31
+ ActionMailer::Base.deliveries = []
32
+ ActionMailer::Base.perform_deliveries = true
33
+ @test_mailer_klass = new_mailer_class('TestMailer')
34
+ end
35
+
36
+ describe "alias chain" do
37
+ it "should create new methods" do
38
+ ActionMailer::Base.instance_methods.include?("deliver!").should be_true
39
+ ActionMailer::Base.instance_methods.include?("deliver_with_disk_save!").should be_true
40
+ ActionMailer::Base.instance_methods.include?("deliver_without_disk_save!").should be_true
41
+ end
42
+ end
43
+
44
+ describe "deliver_with_disk_save!" do
45
+
46
+ describe "saving emails to disk" do
47
+ before do
48
+ ActionMailer::Base.save_emails_to_disk = true
49
+ ActionMailer::Base.email_output_dir = "/tmp/actionmailer_output_emails"
50
+ end
51
+
52
+ after do
53
+ # cleanup any mails we wrote to disk
54
+ FileUtils.rm_r(ActionMailer::Base.email_output_dir) if File.directory?(ActionMailer::Base.email_output_dir)
55
+ end
56
+
57
+ it "should create the output dir if it does not exist" do
58
+ File.directory?(ActionMailer::Base.email_output_dir).should be_false
59
+ @test_mailer_klass.deliver_test_email
60
+ File.directory?(ActionMailer::Base.email_output_dir).should be_true
61
+ end
62
+
63
+ it "should save email contents to disk if enabled" do
64
+ @mail = @test_mailer_klass.deliver_test_email
65
+ files = Dir[File.join(ActionMailer::Base.email_output_dir, "*")]
66
+ files.size.should == 1
67
+ IO.read(files.first).should == @mail.encoded
68
+ end
69
+
70
+ it "should not save email to disk if not enabled" do
71
+ ActionMailer::Base.save_emails_to_disk = false
72
+ @mail = @test_mailer_klass.deliver_test_email
73
+ files = Dir[File.join(ActionMailer::Base.email_output_dir, "*")].size.should == 0
74
+ end
75
+ end
76
+
77
+ describe "safe recipients" do
78
+ before do
79
+ ActionMailer::Base.safe_recipients = ['a@example.com', 'b@example.com']
80
+ @test_mailer_klass.create_test_email.to.should == ["someone@example.com"]
81
+ end
82
+
83
+ it "should restrict sending when list is empty" do
84
+ ActionMailer::Base.safe_recipients = []
85
+ lambda{
86
+ @test_mailer_klass.deliver_test_email()
87
+ }.should_not change{ActionMailer::Base.deliveries.size}
88
+ end
89
+
90
+ it "should restrict sending when list is nil" do
91
+ ActionMailer::Base.safe_recipients = nil
92
+ lambda{
93
+ @test_mailer_klass.deliver_test_email
94
+ }.should_not change{ActionMailer::Base.deliveries.size}
95
+ end
96
+
97
+ it "should not restrict sending when recipient is in the list" do
98
+ ActionMailer::Base.safe_recipients = ["someone@example.com"]
99
+ lambda{
100
+ @test_mailer_klass.deliver_test_email
101
+ }.should change{ActionMailer::Base.deliveries.size}.by(1)
102
+ end
103
+
104
+ it "should send to any address when safe recipients list contains :any" do
105
+ ActionMailer::Base.safe_recipients = [:any]
106
+ lambda{
107
+ @test_mailer_klass.deliver_test_email
108
+ }.should change{ActionMailer::Base.deliveries.size}.by(1)
109
+ end
110
+
111
+ it "should restrict sending when recipient is not in the list" do
112
+ ActionMailer::Base.safe_recipients.include?("someone@example.com").should be_false
113
+ lambda{
114
+ @test_mailer_klass.deliver_test_email
115
+ }.should_not change{ActionMailer::Base.deliveries.size}
116
+ end
117
+
118
+ it "should not have side-effects on the safe recipients list" do
119
+ ActionMailer::Base.safe_recipients = [:any, "foo@foo.com", "bar@bar.com"]
120
+ @test_mailer_klass.deliver_test_email
121
+ ActionMailer::Base.safe_recipients.should == [:any, "foo@foo.com", "bar@bar.com"]
122
+ end
123
+
124
+ it "should allow sending when multiple recipients are all in the safe list" do
125
+ ActionMailer::Base.safe_recipients = ['a@example.com', 'b@example.com']
126
+ lambda{
127
+ @test_mailer_klass.deliver_test_email(['a@example.com', 'b@example.com'])
128
+ }.should change{ActionMailer::Base.deliveries.size}.by(1)
129
+ end
130
+
131
+ it "should restrict sending when not all recipients are in the safe list" do
132
+ ActionMailer::Base.safe_recipients = ['a@example.com', 'b@example.com']
133
+ lambda{
134
+ @test_mailer_klass.deliver_test_email(['a@example.com', 'foo@example.com'])
135
+ }.should_not change{ActionMailer::Base.deliveries.size}
136
+ end
137
+
138
+ end
139
+
140
+ end
141
+
142
+ end
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format specdoc
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'spec'
4
+ require 'spec/autorun'
5
+ require 'actionmailer_extensions'
6
+
7
+ Spec::Runner.configure do |config|
8
+ config.mock_with :rr
9
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: actionmailer_extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Peter MacRobert
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-20 00:00:00 +00:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: yard
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.9
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: rr
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.10.5
44
+ version:
45
+ description: |-
46
+ Wraps the deliver! method on ActionMailer to save the outgoing mail to a .eml file,
47
+ which can be opened by most email clients. Also provides a mechanism for only sending to an approved list of
48
+ email recipients, which is useful for ensuring your application doesn't send email outside of an organization.
49
+ email: originalpete@gmail.com
50
+ executables: []
51
+
52
+ extensions: []
53
+
54
+ extra_rdoc_files:
55
+ - LICENSE
56
+ - README.rdoc
57
+ files:
58
+ - .document
59
+ - .gitignore
60
+ - LICENSE
61
+ - README.rdoc
62
+ - Rakefile
63
+ - VERSION
64
+ - lib/actionmailer_extensions.rb
65
+ - spec/action_mailer_extensions_spec.rb
66
+ - spec/spec.opts
67
+ - spec/spec_helper.rb
68
+ has_rdoc: true
69
+ homepage: http://github.com/originalpete/actionmailer_extensions
70
+ licenses: []
71
+
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.3.5
93
+ signing_key:
94
+ specification_version: 3
95
+ summary: Handy "save to disk" and "safe recipients" features for ActionMailer
96
+ test_files:
97
+ - spec/action_mailer_extensions_spec.rb
98
+ - spec/spec_helper.rb