padrino-mailer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +7 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/lib/padrino-mailer/base.rb +75 -0
- data/lib/padrino-mailer/delivery.rb +94 -0
- data/lib/padrino-mailer/mail_object.rb +39 -0
- data/lib/padrino-mailer.rb +11 -0
- data/padrino-mailer.gemspec +86 -0
- data/test/active_support_helpers.rb +7 -0
- data/test/fixtures/mailer_app/app.rb +50 -0
- data/test/fixtures/mailer_app/views/demo_mailer/sample_mail.erb +1 -0
- data/test/fixtures/mailer_app/views/sample_mailer/anniversary_message.erb +2 -0
- data/test/fixtures/mailer_app/views/sample_mailer/birthday_message.erb +2 -0
- data/test/helper.rb +74 -0
- data/test/test_mail_object.rb +25 -0
- data/test/test_mailer_base.rb +82 -0
- data/test/test_padrino_mailer.rb +33 -0
- metadata +152 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Padrino
|
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
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "padrino-mailer"
|
8
|
+
gem.summary = "Mailer system for padrino"
|
9
|
+
gem.description = "Mailer system for padrino allowing easy delivery of application emails"
|
10
|
+
gem.email = "nesquena@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/padrino/padrino-mailer"
|
12
|
+
gem.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
13
|
+
gem.add_runtime_dependency "sinatra", ">= 0.9.2"
|
14
|
+
gem.add_runtime_dependency "tilt", ">= 0.2"
|
15
|
+
gem.add_development_dependency "shoulda", ">= 0"
|
16
|
+
gem.add_development_dependency "haml", ">= 2.2.1"
|
17
|
+
gem.add_development_dependency "mocha", ">= 0.9.7"
|
18
|
+
gem.add_development_dependency "rack-test", ">= 0.5.0"
|
19
|
+
gem.add_development_dependency "webrat", ">= 0.5.1"
|
20
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
21
|
+
end
|
22
|
+
Jeweler::GemcutterTasks.new
|
23
|
+
rescue LoadError
|
24
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'rake/testtask'
|
28
|
+
Rake::TestTask.new(:test) do |test|
|
29
|
+
test.libs << 'lib' << 'test'
|
30
|
+
test.pattern = 'test/**/test_*.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'rcov/rcovtask'
|
36
|
+
Rcov::RcovTask.new do |test|
|
37
|
+
test.libs << 'test'
|
38
|
+
test.pattern = 'test/**/test_*.rb'
|
39
|
+
test.verbose = true
|
40
|
+
end
|
41
|
+
rescue LoadError
|
42
|
+
task :rcov do
|
43
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
task :test => :check_dependencies
|
48
|
+
|
49
|
+
task :default => :test
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "padrino-mailer #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,75 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
This is the abstract class that other mailers will inherit from in order to send mail
|
4
|
+
|
5
|
+
You can set the default delivery settings through:
|
6
|
+
|
7
|
+
Padrino::Mailer::Base.smtp_settings = {
|
8
|
+
:host => 'smtp.yourserver.com',
|
9
|
+
:port => '25',
|
10
|
+
:user => 'user',
|
11
|
+
:pass => 'pass',
|
12
|
+
:auth => :plain # :plain, :login, :cram_md5, no auth by default
|
13
|
+
:domain => "localhost.localdomain" # the HELO domain provided by the client to the server
|
14
|
+
}
|
15
|
+
|
16
|
+
and then all delivered mail will use these settings unless otherwise specified.
|
17
|
+
|
18
|
+
=end
|
19
|
+
|
20
|
+
module Padrino
|
21
|
+
module Mailer
|
22
|
+
class Base
|
23
|
+
# Returns the available mail fields when composing a message
|
24
|
+
def self.mail_fields
|
25
|
+
[:to, :from, :subject, :type, :charset, :via, :attachments]
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_accessor :mail_attributes
|
29
|
+
|
30
|
+
def initialize(mail_name=nil)
|
31
|
+
@mail_name = mail_name
|
32
|
+
@mail_attributes = {}
|
33
|
+
end
|
34
|
+
|
35
|
+
# Defines a method allowing mail attributes to be set into a hash for use when delivering
|
36
|
+
self.mail_fields.each do |field|
|
37
|
+
define_method(field) { |value| @mail_attributes[field] = value }
|
38
|
+
end
|
39
|
+
|
40
|
+
# Assigns the body key to the mail attributes either with the rendered body from a template or the given string value
|
41
|
+
def body(body_value)
|
42
|
+
@mail_attributes[:body] = Tilt.new(template_path).render(self, body_value.symbolize_keys) if body_value.is_a?(Hash)
|
43
|
+
@mail_attributes[:body] = body_value if body_value.is_a?(String)
|
44
|
+
end
|
45
|
+
|
46
|
+
# Returns the path to the email template searched for using glob pattern
|
47
|
+
def template_path
|
48
|
+
Dir[File.join(self.views_path, self.class.name.underscore.split("/").last, "#{@mail_name}.*")].first
|
49
|
+
end
|
50
|
+
|
51
|
+
cattr_accessor :smtp_settings
|
52
|
+
cattr_accessor :views_path
|
53
|
+
|
54
|
+
# Delivers the specified message for mail_name to the intended recipients
|
55
|
+
# mail_name corresponds to the name of a defined method within the mailer class
|
56
|
+
# SampleMailer.deliver(:birthday_message)
|
57
|
+
def self.deliver(mail_name, *args)
|
58
|
+
mail_object = self.new(mail_name)
|
59
|
+
mail_object.method(mail_name).call(*args)
|
60
|
+
MailObject.new(mail_object.mail_attributes, self.smtp_settings).deliver
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns true if a mail exists with the name being delivered
|
64
|
+
def self.respond_to?(method_sym, include_private = false)
|
65
|
+
method_sym.to_s =~ /deliver_(.*)/ ? self.method_defined?($1) : super
|
66
|
+
end
|
67
|
+
|
68
|
+
# Handles method missing for a mailer class. Delivers a message based on the method
|
69
|
+
# being called i.e #deliver_birthday_message(22) invokes #birthday_message(22) to setup mail object
|
70
|
+
def self.method_missing(method_sym, *arguments, &block)
|
71
|
+
method_sym.to_s =~ /deliver_(.*)/ ? self.deliver($1, *arguments) : super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'net/smtp'
|
3
|
+
begin
|
4
|
+
require 'smtp_tls'
|
5
|
+
rescue LoadError
|
6
|
+
end
|
7
|
+
require 'base64'
|
8
|
+
require 'tmail'
|
9
|
+
|
10
|
+
module Padrino
|
11
|
+
module Mailer
|
12
|
+
module Delivery
|
13
|
+
|
14
|
+
class << self
|
15
|
+
|
16
|
+
def mail(options)
|
17
|
+
raise(ArgumentError, ":to is required") unless options[:to]
|
18
|
+
|
19
|
+
via = options.delete(:via)
|
20
|
+
if via.nil?
|
21
|
+
transport build_tmail(options)
|
22
|
+
else
|
23
|
+
if via_options.include?(via.to_s)
|
24
|
+
send("transport_via_#{via}", build_tmail(options), options)
|
25
|
+
else
|
26
|
+
raise(ArgumentError, ":via must be either smtp or sendmail")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def build_tmail(options)
|
32
|
+
mail = TMail::Mail.new
|
33
|
+
mail.to = options[:to]
|
34
|
+
mail.from = options[:from] || 'pony@unknown'
|
35
|
+
mail.subject = options[:subject]
|
36
|
+
mail.body = options[:body] || ""
|
37
|
+
mail.set_content_type 'text', options[:type] || 'plain', {'charset'=> options[:charset] || 'utf-8'}
|
38
|
+
(options[:attachments] || []).each do |name, body|
|
39
|
+
attachment = TMail::Mail.new
|
40
|
+
attachment.transfer_encoding = "base64"
|
41
|
+
attachment.body = Base64.encode64(body)
|
42
|
+
# attachment.set_content_type # TODO: if necessary
|
43
|
+
attachment.set_content_disposition "attachment", "filename" => name
|
44
|
+
mail.parts.push attachment
|
45
|
+
end
|
46
|
+
mail
|
47
|
+
end
|
48
|
+
|
49
|
+
def sendmail_binary
|
50
|
+
@sendmail_binary ||= `which sendmail`.chomp
|
51
|
+
end
|
52
|
+
|
53
|
+
def transport(tmail)
|
54
|
+
if File.executable? sendmail_binary
|
55
|
+
transport_via_sendmail(tmail)
|
56
|
+
else
|
57
|
+
transport_via_smtp(tmail)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def via_options
|
62
|
+
%w(sendmail smtp)
|
63
|
+
end
|
64
|
+
|
65
|
+
def transport_via_sendmail(tmail, options={})
|
66
|
+
IO.popen('-', 'w+') do |pipe|
|
67
|
+
if pipe
|
68
|
+
pipe.write(tmail.to_s)
|
69
|
+
else
|
70
|
+
exec(sendmail_binary, *tmail.to)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def transport_via_smtp(tmail, options={:smtp => {}})
|
76
|
+
default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
|
77
|
+
o = default_options[:smtp].merge(options[:smtp])
|
78
|
+
smtp = Net::SMTP.new(o[:host], o[:port])
|
79
|
+
if o[:tls]
|
80
|
+
raise "You may need: gem install smtp_tls" unless smtp.respond_to?(:enable_starttls)
|
81
|
+
smtp.enable_starttls
|
82
|
+
end
|
83
|
+
if o.include?(:auth)
|
84
|
+
smtp.start(o[:domain], o[:user], o[:password], o[:auth])
|
85
|
+
else
|
86
|
+
smtp.start(o[:domain])
|
87
|
+
end
|
88
|
+
smtp.send_message tmail.to_s, tmail.from, tmail.to
|
89
|
+
smtp.finish
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# This represents a particular mail object which will need to be sent
|
2
|
+
# A mail_object requires the mail attributes and the delivery_settings
|
3
|
+
|
4
|
+
module Padrino
|
5
|
+
module Mailer
|
6
|
+
class MailObject
|
7
|
+
def initialize(mail_attributes={}, smtp_settings={})
|
8
|
+
@mail_attributes = mail_attributes.dup
|
9
|
+
@smtp_settings = smtp_settings.dup if smtp_settings.present?
|
10
|
+
end
|
11
|
+
|
12
|
+
# Constructs the delivery attributes for the message and then sends the mail
|
13
|
+
# @mail_object.deliver
|
14
|
+
def deliver
|
15
|
+
@mail_attributes.reverse_merge!(:via => self.delivery_method.to_sym)
|
16
|
+
@mail_attributes.reverse_merge!(:smtp => @smtp_settings) if using_smtp?
|
17
|
+
self.send_mail(@mail_attributes)
|
18
|
+
end
|
19
|
+
|
20
|
+
protected
|
21
|
+
|
22
|
+
# Returns the delivery method to use for this mail object
|
23
|
+
# @mo.delivery_method => :smtp || :sendmail
|
24
|
+
def delivery_method
|
25
|
+
@mail_attributes[:via] || (@smtp_settings.present? ? :smtp : :sendmail)
|
26
|
+
end
|
27
|
+
|
28
|
+
# Returns true if the mail object is going to be delivered using smtp
|
29
|
+
def using_smtp?
|
30
|
+
delivery_method.to_s =~ /smtp/
|
31
|
+
end
|
32
|
+
|
33
|
+
# Performs the actual email sending
|
34
|
+
def send_mail(delivery_attributes)
|
35
|
+
Delivery.mail(delivery_attributes) && true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'tilt'
|
2
|
+
# require 'padrino-core/support_lite'
|
3
|
+
Dir[File.dirname(__FILE__) + '/padrino-mailer/**/*.rb'].each { |file| require file }
|
4
|
+
|
5
|
+
module Padrino
|
6
|
+
module Mailer
|
7
|
+
def self.registered(app)
|
8
|
+
Padrino::Mailer::Base::views_path = app.views
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{padrino-mailer}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Padrino Team", "Nathan Esquenazi", "Davide D'Agostino", "Arthur Chiu"]
|
12
|
+
s.date = %q{2009-11-16}
|
13
|
+
s.description = %q{Mailer system for padrino allowing easy delivery of application emails}
|
14
|
+
s.email = %q{nesquena@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"lib/padrino-mailer.rb",
|
27
|
+
"lib/padrino-mailer/base.rb",
|
28
|
+
"lib/padrino-mailer/delivery.rb",
|
29
|
+
"lib/padrino-mailer/mail_object.rb",
|
30
|
+
"padrino-mailer.gemspec",
|
31
|
+
"test/active_support_helpers.rb",
|
32
|
+
"test/fixtures/mailer_app/app.rb",
|
33
|
+
"test/fixtures/mailer_app/views/demo_mailer/sample_mail.erb",
|
34
|
+
"test/fixtures/mailer_app/views/sample_mailer/anniversary_message.erb",
|
35
|
+
"test/fixtures/mailer_app/views/sample_mailer/birthday_message.erb",
|
36
|
+
"test/helper.rb",
|
37
|
+
"test/test_mail_object.rb",
|
38
|
+
"test/test_mailer_base.rb",
|
39
|
+
"test/test_padrino_mailer.rb"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/padrino/padrino-mailer}
|
42
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.3.5}
|
45
|
+
s.summary = %q{Mailer system for padrino}
|
46
|
+
s.test_files = [
|
47
|
+
"test/active_support_helpers.rb",
|
48
|
+
"test/fixtures/mailer_app/app.rb",
|
49
|
+
"test/helper.rb",
|
50
|
+
"test/test_mail_object.rb",
|
51
|
+
"test/test_mailer_base.rb",
|
52
|
+
"test/test_padrino_mailer.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
s.add_runtime_dependency(%q<sinatra>, [">= 0.9.2"])
|
61
|
+
s.add_runtime_dependency(%q<tilt>, [">= 0.2"])
|
62
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
63
|
+
s.add_development_dependency(%q<haml>, [">= 2.2.1"])
|
64
|
+
s.add_development_dependency(%q<mocha>, [">= 0.9.7"])
|
65
|
+
s.add_development_dependency(%q<rack-test>, [">= 0.5.0"])
|
66
|
+
s.add_development_dependency(%q<webrat>, [">= 0.5.1"])
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
69
|
+
s.add_dependency(%q<tilt>, [">= 0.2"])
|
70
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
71
|
+
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
72
|
+
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
73
|
+
s.add_dependency(%q<rack-test>, [">= 0.5.0"])
|
74
|
+
s.add_dependency(%q<webrat>, [">= 0.5.1"])
|
75
|
+
end
|
76
|
+
else
|
77
|
+
s.add_dependency(%q<sinatra>, [">= 0.9.2"])
|
78
|
+
s.add_dependency(%q<tilt>, [">= 0.2"])
|
79
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
80
|
+
s.add_dependency(%q<haml>, [">= 2.2.1"])
|
81
|
+
s.add_dependency(%q<mocha>, [">= 0.9.7"])
|
82
|
+
s.add_dependency(%q<rack-test>, [">= 0.5.0"])
|
83
|
+
s.add_dependency(%q<webrat>, [">= 0.5.1"])
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'haml'
|
3
|
+
|
4
|
+
class MailerDemo < Sinatra::Base
|
5
|
+
configure do
|
6
|
+
set :root, File.dirname(__FILE__)
|
7
|
+
set :smtp_settings, {
|
8
|
+
:host => 'smtp.gmail.com',
|
9
|
+
:port => '587',
|
10
|
+
:tls => true,
|
11
|
+
:user => 'user',
|
12
|
+
:pass => 'pass',
|
13
|
+
:auth => :plain
|
14
|
+
}
|
15
|
+
end
|
16
|
+
|
17
|
+
register Padrino::Mailer
|
18
|
+
|
19
|
+
class SampleMailer < Padrino::Mailer::Base
|
20
|
+
def birthday_message(name, age)
|
21
|
+
subject "Happy Birthday!"
|
22
|
+
to 'john@fake.com'
|
23
|
+
from 'noreply@birthday.com'
|
24
|
+
body 'name' => name, 'age' => age
|
25
|
+
via :smtp
|
26
|
+
end
|
27
|
+
|
28
|
+
def anniversary_message(names, years_married)
|
29
|
+
subject "Happy anniversary!"
|
30
|
+
to 'julie@fake.com'
|
31
|
+
from 'noreply@anniversary.com'
|
32
|
+
body 'names' => names, 'years_married' => years_married
|
33
|
+
type 'html'
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
post "/deliver/plain" do
|
38
|
+
result = SampleMailer.deliver_birthday_message("Joey", 21)
|
39
|
+
result ? "mail delivered" : 'mail not delivered'
|
40
|
+
end
|
41
|
+
|
42
|
+
post "/deliver/html" do
|
43
|
+
result = SampleMailer.deliver_anniversary_message("Joey & Charlotte", 16)
|
44
|
+
result ? "mail delivered" : 'mail not delivered'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class MailerUser
|
49
|
+
|
50
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
This is a sample message
|
data/test/helper.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'shoulda'
|
4
|
+
require 'mocha'
|
5
|
+
require 'rack/test'
|
6
|
+
require 'webrat'
|
7
|
+
|
8
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
9
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
10
|
+
require 'active_support_helpers'
|
11
|
+
require File.dirname(__FILE__) + '/../../padrino-helpers/lib/padrino-helpers.rb'
|
12
|
+
require File.dirname(__FILE__) + '/../lib/padrino-mailer.rb'
|
13
|
+
|
14
|
+
class Test::Unit::TestCase
|
15
|
+
include Padrino::Helpers::OutputHelpers
|
16
|
+
include Padrino::Helpers::TagHelpers
|
17
|
+
include Padrino::Helpers::AssetTagHelpers
|
18
|
+
include Rack::Test::Methods
|
19
|
+
include Webrat::Methods
|
20
|
+
include Webrat::Matchers
|
21
|
+
|
22
|
+
Webrat.configure do |config|
|
23
|
+
config.mode = :rack
|
24
|
+
end
|
25
|
+
|
26
|
+
def stop_time_for_test
|
27
|
+
time = Time.now
|
28
|
+
Time.stubs(:now).returns(time)
|
29
|
+
return time
|
30
|
+
end
|
31
|
+
|
32
|
+
# assert_has_tag(:h1, :content => "yellow") { "<h1>yellow</h1>" }
|
33
|
+
# In this case, block is the html to evaluate
|
34
|
+
def assert_has_tag(name, attributes = {}, &block)
|
35
|
+
html = block && block.call
|
36
|
+
matcher = HaveSelector.new(name, attributes)
|
37
|
+
raise "Please specify a block!" if html.blank?
|
38
|
+
assert matcher.matches?(html), matcher.failure_message
|
39
|
+
end
|
40
|
+
|
41
|
+
# assert_has_no_tag, tag(:h1, :content => "yellow") { "<h1>green</h1>" }
|
42
|
+
# In this case, block is the html to evaluate
|
43
|
+
def assert_has_no_tag(name, attributes = {}, &block)
|
44
|
+
html = block && block.call
|
45
|
+
attributes.merge!(:count => 0)
|
46
|
+
matcher = HaveSelector.new(name, attributes)
|
47
|
+
raise "Please specify a block!" if html.blank?
|
48
|
+
assert matcher.matches?(html), matcher.failure_message
|
49
|
+
end
|
50
|
+
|
51
|
+
# Silences the output by redirecting to stringIO
|
52
|
+
# silence_logger { ...commands... } => "...output..."
|
53
|
+
def silence_logger(&block)
|
54
|
+
orig_stdout = $stdout
|
55
|
+
$stdout = log_buffer = StringIO.new
|
56
|
+
block.call
|
57
|
+
$stdout = orig_stdout
|
58
|
+
log_buffer.rewind && log_buffer.read
|
59
|
+
end
|
60
|
+
|
61
|
+
# Asserts that a file matches the pattern
|
62
|
+
def assert_match_in_file(pattern, file)
|
63
|
+
assert File.exist?(file), "File '#{file}' does not exist!"
|
64
|
+
assert_match pattern, File.read(file)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module Webrat
|
69
|
+
module Logging
|
70
|
+
def logger # :nodoc:
|
71
|
+
@logger = nil
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class TestMailObject < Test::Unit::TestCase
|
4
|
+
include Padrino::Mailer
|
5
|
+
|
6
|
+
context 'for #deliver method' do
|
7
|
+
should "send mail with attributes default to sendmail no smtp" do
|
8
|
+
mail_object = Padrino::Mailer::MailObject.new({:to => "test@john.com", :from => "sender@sent.com", :body => "Hello"}, {})
|
9
|
+
Delivery.expects(:mail).with(:to => "test@john.com", :from => "sender@sent.com", :body => "Hello", :via => :sendmail)
|
10
|
+
mail_object.deliver
|
11
|
+
end
|
12
|
+
|
13
|
+
should "send mail with attributes default to smtp if set" do
|
14
|
+
mail_object = Padrino::Mailer::MailObject.new({:to => "test@john.com", :body => "Hello"}, { :host => 'smtp.gmail.com' })
|
15
|
+
Delivery.expects(:mail).with(:to => "test@john.com", :body => "Hello", :via => :smtp, :smtp => { :host => 'smtp.gmail.com' })
|
16
|
+
mail_object.deliver
|
17
|
+
end
|
18
|
+
|
19
|
+
should "send mail with attributes use sendmail if explicit" do
|
20
|
+
mail_object = Padrino::Mailer::MailObject.new({:to => "test@john.com", :via => :sendmail }, { :host => 'smtp.gmail.com' })
|
21
|
+
Delivery.expects(:mail).with(:to => "test@john.com", :via => :sendmail)
|
22
|
+
mail_object.deliver
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
class DemoMailer < Padrino::Mailer::Base
|
4
|
+
def sample_mail
|
5
|
+
from 'test@default.com'
|
6
|
+
to 'test@test.com'
|
7
|
+
body "Hello world!"
|
8
|
+
via :sendmail
|
9
|
+
end
|
10
|
+
|
11
|
+
def sample_mail_smtp
|
12
|
+
from 'test@default.com'
|
13
|
+
to 'test@test.com'
|
14
|
+
body "SMTP Hello world!"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
class TestMailerBase < Test::Unit::TestCase
|
19
|
+
include Padrino::Mailer
|
20
|
+
|
21
|
+
context 'for defining email attributes' do
|
22
|
+
DemoMailer.mail_fields.each do |field|
|
23
|
+
should "support setting '#{field}' attribute" do
|
24
|
+
demo_mailer = DemoMailer.new(:sample_mail)
|
25
|
+
demo_mailer.send(field, "some_value")
|
26
|
+
assert_equal({ field => "some_value" }, demo_mailer.mail_attributes)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
should "allow defining text body" do
|
31
|
+
demo_mailer = DemoMailer.new(:sample_mail)
|
32
|
+
demo_mailer.body("Hello world!")
|
33
|
+
assert_equal({ :body => "Hello world!" }, demo_mailer.mail_attributes)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'for retrieving template path' do
|
38
|
+
should "return correct path" do
|
39
|
+
demo_mailer = DemoMailer.new(:sample_mail)
|
40
|
+
assert_match %r{demo_mailer/sample_mail.erb}, demo_mailer.template_path
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'for #deliver class method' do
|
45
|
+
should "perform the email delivery for sendmail" do
|
46
|
+
Delivery.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com', :body => "Hello world!", :via => :sendmail)
|
47
|
+
DemoMailer.deliver(:sample_mail)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "perform the email delivery for smtp" do
|
51
|
+
DemoMailer.smtp_settings = { :host => 'smtp.arcadic.com' }
|
52
|
+
Delivery.expects(:mail).with(:from => 'test@default.com', :to => 'test@test.com',
|
53
|
+
:body => "SMTP Hello world!", :via => :smtp, :smtp => { :host => 'smtp.arcadic.com' })
|
54
|
+
DemoMailer.deliver(:sample_mail_smtp)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context 'for #respond_to? class method' do
|
59
|
+
should "respond as true for any delivery method calls for mails that exist" do
|
60
|
+
assert DemoMailer.respond_to?(:deliver_sample_mail)
|
61
|
+
end
|
62
|
+
|
63
|
+
should "respond as false for any delivery method calls for mails that don't exist" do
|
64
|
+
assert_equal false, DemoMailer.respond_to?(:deliver_faker_mail)
|
65
|
+
end
|
66
|
+
|
67
|
+
should "respond as true for any non-delivery methods that exist" do
|
68
|
+
assert DemoMailer.respond_to?(:inspect)
|
69
|
+
end
|
70
|
+
|
71
|
+
should "respond as false for any non-delivery methods that don't exist" do
|
72
|
+
assert_equal false, DemoMailer.respond_to?(:fake_method)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context 'for #method_missing dynamic delivery' do
|
77
|
+
should 'invoke deliver method with appropriate parameters' do
|
78
|
+
DemoMailer.expects(:deliver).with("example_name", "test", 5)
|
79
|
+
DemoMailer.deliver_example_name("test", 5)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
require File.dirname(__FILE__) + '/fixtures/mailer_app/app'
|
3
|
+
|
4
|
+
class TestPadrinoMailer < Test::Unit::TestCase
|
5
|
+
def app
|
6
|
+
MailerDemo.tap { |app| app.set :environment, :test }
|
7
|
+
end
|
8
|
+
|
9
|
+
context 'for mail delivery in sample application' do
|
10
|
+
setup { MailerDemo::SampleMailer.smtp_settings = MailerDemo.smtp_settings }
|
11
|
+
|
12
|
+
should 'be able to deliver plain text emails' do
|
13
|
+
assert_email_sent(:to => 'john@fake.com', :from => 'noreply@birthday.com', :via => :smtp,
|
14
|
+
:subject => "Happy Birthday!", :body => "Happy Birthday Joey! \nYou are turning 21")
|
15
|
+
visit '/deliver/plain', :post
|
16
|
+
assert_equal 'mail delivered', last_response.body
|
17
|
+
end
|
18
|
+
|
19
|
+
should 'be able to deliver html emails' do
|
20
|
+
assert_email_sent(:to => 'julie@fake.com', :from => 'noreply@anniversary.com', :type => 'html', :via => :smtp,
|
21
|
+
:subject => "Happy anniversary!", :body => "<p>Yay Joey & Charlotte!</p>\n<p>You have been married 16 years</p>")
|
22
|
+
visit '/deliver/html', :post
|
23
|
+
assert_equal 'mail delivered', last_response.body
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
|
29
|
+
def assert_email_sent(mail_attributes)
|
30
|
+
delivery_attributes = mail_attributes.merge(:smtp => MailerDemo.smtp_settings)
|
31
|
+
Padrino::Mailer::MailObject.any_instance.expects(:send_mail).with(delivery_attributes).once.returns(true)
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: padrino-mailer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Padrino Team
|
8
|
+
- Nathan Esquenazi
|
9
|
+
- Davide D'Agostino
|
10
|
+
- Arthur Chiu
|
11
|
+
autorequire:
|
12
|
+
bindir: bin
|
13
|
+
cert_chain: []
|
14
|
+
|
15
|
+
date: 2009-11-16 00:00:00 -08:00
|
16
|
+
default_executable:
|
17
|
+
dependencies:
|
18
|
+
- !ruby/object:Gem::Dependency
|
19
|
+
name: sinatra
|
20
|
+
type: :runtime
|
21
|
+
version_requirement:
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.2
|
27
|
+
version:
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: tilt
|
30
|
+
type: :runtime
|
31
|
+
version_requirement:
|
32
|
+
version_requirements: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: "0.2"
|
37
|
+
version:
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: shoulda
|
40
|
+
type: :development
|
41
|
+
version_requirement:
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: haml
|
50
|
+
type: :development
|
51
|
+
version_requirement:
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 2.2.1
|
57
|
+
version:
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: mocha
|
60
|
+
type: :development
|
61
|
+
version_requirement:
|
62
|
+
version_requirements: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: 0.9.7
|
67
|
+
version:
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rack-test
|
70
|
+
type: :development
|
71
|
+
version_requirement:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: 0.5.0
|
77
|
+
version:
|
78
|
+
- !ruby/object:Gem::Dependency
|
79
|
+
name: webrat
|
80
|
+
type: :development
|
81
|
+
version_requirement:
|
82
|
+
version_requirements: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: 0.5.1
|
87
|
+
version:
|
88
|
+
description: Mailer system for padrino allowing easy delivery of application emails
|
89
|
+
email: nesquena@gmail.com
|
90
|
+
executables: []
|
91
|
+
|
92
|
+
extensions: []
|
93
|
+
|
94
|
+
extra_rdoc_files:
|
95
|
+
- LICENSE
|
96
|
+
- README.rdoc
|
97
|
+
files:
|
98
|
+
- .document
|
99
|
+
- .gitignore
|
100
|
+
- LICENSE
|
101
|
+
- README.rdoc
|
102
|
+
- Rakefile
|
103
|
+
- VERSION
|
104
|
+
- lib/padrino-mailer.rb
|
105
|
+
- lib/padrino-mailer/base.rb
|
106
|
+
- lib/padrino-mailer/delivery.rb
|
107
|
+
- lib/padrino-mailer/mail_object.rb
|
108
|
+
- padrino-mailer.gemspec
|
109
|
+
- test/active_support_helpers.rb
|
110
|
+
- test/fixtures/mailer_app/app.rb
|
111
|
+
- test/fixtures/mailer_app/views/demo_mailer/sample_mail.erb
|
112
|
+
- test/fixtures/mailer_app/views/sample_mailer/anniversary_message.erb
|
113
|
+
- test/fixtures/mailer_app/views/sample_mailer/birthday_message.erb
|
114
|
+
- test/helper.rb
|
115
|
+
- test/test_mail_object.rb
|
116
|
+
- test/test_mailer_base.rb
|
117
|
+
- test/test_padrino_mailer.rb
|
118
|
+
has_rdoc: true
|
119
|
+
homepage: http://github.com/padrino/padrino-mailer
|
120
|
+
licenses: []
|
121
|
+
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options:
|
124
|
+
- --charset=UTF-8
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: "0"
|
132
|
+
version:
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: "0"
|
138
|
+
version:
|
139
|
+
requirements: []
|
140
|
+
|
141
|
+
rubyforge_project:
|
142
|
+
rubygems_version: 1.3.5
|
143
|
+
signing_key:
|
144
|
+
specification_version: 3
|
145
|
+
summary: Mailer system for padrino
|
146
|
+
test_files:
|
147
|
+
- test/active_support_helpers.rb
|
148
|
+
- test/fixtures/mailer_app/app.rb
|
149
|
+
- test/helper.rb
|
150
|
+
- test/test_mail_object.rb
|
151
|
+
- test/test_mailer_base.rb
|
152
|
+
- test/test_padrino_mailer.rb
|