ruport-util 0.1.0 → 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/Rakefile +3 -3
- data/example/mailed_report.rb +0 -0
- data/example/mailer.rb +29 -0
- data/lib/ruport/util/mailer.rb +179 -0
- data/test/samples/data.csv +3 -0
- data/test/test_mailer.rb +205 -0
- metadata +22 -6
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ end
|
|
18
18
|
|
19
19
|
spec = Gem::Specification.new do |spec|
|
20
20
|
spec.name = "ruport-util"
|
21
|
-
spec.version = "0.
|
21
|
+
spec.version = "0.2.0"
|
22
22
|
spec.platform = Gem::Platform::RUBY
|
23
23
|
spec.summary = "A set of tools and helper libs for Ruby Reports"
|
24
24
|
spec.files = Dir.glob("{example,lib,test,bin}/**/**/*") +
|
@@ -34,6 +34,7 @@ spec = Gem::Specification.new do |spec|
|
|
34
34
|
# '--main' << 'README' << '-q'
|
35
35
|
spec.add_dependency('ruport', ">=0.9.3")
|
36
36
|
spec.add_dependency('scruffy', ">=0.2.2")
|
37
|
+
spec.add_dependency('mailfactory',">=1.2.3")
|
37
38
|
spec.author = "Gregory Brown"
|
38
39
|
spec.email = " gregory.t.brown@gmail.com"
|
39
40
|
spec.rubyforge_project = "ruport"
|
@@ -45,8 +46,7 @@ END_DESC
|
|
45
46
|
end
|
46
47
|
|
47
48
|
Rake::RDocTask.new do |rdoc|
|
48
|
-
rdoc.rdoc_files.include( "
|
49
|
-
"TODO", #"CHANGELOG",
|
49
|
+
rdoc.rdoc_files.include( "TODO", #"CHANGELOG",
|
50
50
|
"AUTHORS", "COPYING",
|
51
51
|
"LICENSE", "lib/" )
|
52
52
|
rdoc.main = "README"
|
File without changes
|
data/example/mailer.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require "ruport"
|
2
|
+
require "ruport/util/mailer"
|
3
|
+
require "fileutils"
|
4
|
+
class MyReport < Ruport::Report
|
5
|
+
def prepare
|
6
|
+
mailer :default,
|
7
|
+
:host => "mail.adelphia.net",
|
8
|
+
:address => "gregory.t.brown@gmail.com"
|
9
|
+
end
|
10
|
+
|
11
|
+
def generate
|
12
|
+
Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }.to_pdf
|
13
|
+
end
|
14
|
+
|
15
|
+
def cleanup
|
16
|
+
FileUtils.rm("foo.pdf")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
MyReport.run do |res|
|
21
|
+
res.write "foo.pdf";
|
22
|
+
res.send_to("gregory.t.brown@gmail.com") do |mail|
|
23
|
+
mail.subject = "Sample report"
|
24
|
+
mail.attach "foo.pdf"
|
25
|
+
mail.text = <<-EOS
|
26
|
+
this is a sample of sending an emailed report from within Ruport.
|
27
|
+
EOS
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# mailer.rb
|
2
|
+
# Created by Gregory Brown on 2005-08-16
|
3
|
+
# Copyright 2005 (Gregory Brown) All Rights Reserved.
|
4
|
+
# This product is free software, you may distribute it as such
|
5
|
+
# under your choice of the Ruby license or the GNU GPL
|
6
|
+
# See LICENSE for details
|
7
|
+
require "net/smtp"
|
8
|
+
require "forwardable"
|
9
|
+
|
10
|
+
module Ruport
|
11
|
+
|
12
|
+
#
|
13
|
+
# === Overview
|
14
|
+
#
|
15
|
+
# This class uses SMTP to provide a simple mail sending mechanism.
|
16
|
+
# It also uses MailFactory to provide attachment and HTML email support.
|
17
|
+
#
|
18
|
+
# === Example
|
19
|
+
#
|
20
|
+
# Here is a simple example of a message which attaches a README file:
|
21
|
+
#
|
22
|
+
# require "ruport"
|
23
|
+
#
|
24
|
+
# Ruport.configure do |config|
|
25
|
+
# config.mailer :default,
|
26
|
+
# :host => "mail.adelphia.net",
|
27
|
+
# :address => "gregory.t.brown@gmail.com"
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# mailer = Ruport::Mailer.new
|
31
|
+
#
|
32
|
+
# mailer.attach "README"
|
33
|
+
#
|
34
|
+
# mailer.deliver :to => "gregory.t.brown@gmail.com",
|
35
|
+
# :from => "gregory.t.brown@gmail.com",
|
36
|
+
# :subject => "Hey there",
|
37
|
+
# :text => "This is what you asked for"
|
38
|
+
#
|
39
|
+
class Mailer
|
40
|
+
extend Forwardable
|
41
|
+
|
42
|
+
#
|
43
|
+
# Creates a new Mailer object. Optionally, you can select a mailer
|
44
|
+
# specified by Ruport::Config.
|
45
|
+
#
|
46
|
+
# Example:
|
47
|
+
#
|
48
|
+
# a = Mailer.new # uses the :default mailer
|
49
|
+
# a = Mailer.new :foo # uses :foo mail config from Ruport::Config
|
50
|
+
#
|
51
|
+
def initialize( mailer_label=:default )
|
52
|
+
select_mailer(mailer_label);
|
53
|
+
mail_object.from = @mailer.address if mail_object.from.to_s.empty?
|
54
|
+
rescue
|
55
|
+
raise "you need to specify a mailer to use"
|
56
|
+
end
|
57
|
+
|
58
|
+
def_delegators( :@mail, :to, :to=, :from, :from=,
|
59
|
+
:subject, :subject=, :attach,
|
60
|
+
:text, :text=, :html, :html= )
|
61
|
+
|
62
|
+
#
|
63
|
+
# Sends the message.
|
64
|
+
#
|
65
|
+
# Example:
|
66
|
+
#
|
67
|
+
# mailer.deliver :from => "gregory.t.brown@gmail.com",
|
68
|
+
# :to => "greg7224@gmail.com"
|
69
|
+
#
|
70
|
+
def deliver(options={})
|
71
|
+
options.each { |k,v| send("#{k}=",v) if respond_to? "#{k}=" }
|
72
|
+
|
73
|
+
Net::SMTP.start(@host,@port,@host,@user,@password,@auth) do |smtp|
|
74
|
+
smtp.send_message((options[:mail_object] || mail_object).to_s, options[:from], options[:to] )
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def select_mailer(label)
|
81
|
+
@mailer = Ruport::Config.mailers[label]
|
82
|
+
@host = @mailer.host
|
83
|
+
@user = @mailer.user
|
84
|
+
@password = @mailer.password
|
85
|
+
@address = @mailer.address
|
86
|
+
@port = @mailer.port || 25
|
87
|
+
@auth = @mailer.auth_type || :plain
|
88
|
+
@mail_klass = @mailer.mail_klass
|
89
|
+
end
|
90
|
+
|
91
|
+
def mail_object
|
92
|
+
return @mail if @mail
|
93
|
+
return @mail ||= @mail_klass.new if @mail_klass
|
94
|
+
require "mailfactory"
|
95
|
+
@mail ||= MailFactory.new
|
96
|
+
end
|
97
|
+
|
98
|
+
end
|
99
|
+
|
100
|
+
class Report
|
101
|
+
# Creates a new Mailer and sets the <tt>to</tt> attribute to the addresses
|
102
|
+
# specified. Yields a Mailer object, which can be modified before delivery.
|
103
|
+
#
|
104
|
+
def send_to(adds)
|
105
|
+
use_mailer(:default) unless @mailer
|
106
|
+
m = Mailer.new
|
107
|
+
m.to = adds
|
108
|
+
yield(m)
|
109
|
+
m.send(:select_mailer,@mailer)
|
110
|
+
m.deliver :from => m.from, :to => m.to
|
111
|
+
end
|
112
|
+
|
113
|
+
# Sets the active mailer to the Ruport::Config source requested by <tt>label</tt>.
|
114
|
+
def use_mailer(label)
|
115
|
+
@mailer = label
|
116
|
+
end
|
117
|
+
|
118
|
+
def_delegator Ruport::Config, :mailer
|
119
|
+
|
120
|
+
end
|
121
|
+
|
122
|
+
module Config
|
123
|
+
module_function
|
124
|
+
# Verifies that you have provided a host for your mailer.
|
125
|
+
def check_mailer(settings, label) # :nodoc:
|
126
|
+
unless settings.host
|
127
|
+
Ruport.log(
|
128
|
+
"Missing host for mailer #{label}",
|
129
|
+
:status => :fatal, :level => :log_only,
|
130
|
+
:raises => ArgumentError
|
131
|
+
)
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
# :call-seq:
|
136
|
+
# mailer(mailer_name, options)
|
137
|
+
#
|
138
|
+
# Creates or retrieves a mailer configuration. Available options:
|
139
|
+
# <b><tt>:host</tt></b>:: The SMTP host to use.
|
140
|
+
# <b><tt>:address</tt></b>:: Address the email is being sent from.
|
141
|
+
# <b><tt>:user</tt></b>:: The username to use on the SMTP server
|
142
|
+
# <b><tt>:password</tt></b>:: The password to use on the SMTP server.
|
143
|
+
# Optional.
|
144
|
+
# <b><tt>:port</tt></b>:: The SMTP port to use. Optional, defaults
|
145
|
+
# to 25.
|
146
|
+
# <b><tt>:auth_type</tt></b>:: SMTP authorization method. Optional,
|
147
|
+
# defaults to <tt>:plain</tt>.
|
148
|
+
# <b><tt>:mail_klass</tt></b>:: If you don't want to use the default
|
149
|
+
# <tt>MailFactory</tt> object, you can
|
150
|
+
# pass another mailer to use here.
|
151
|
+
#
|
152
|
+
# Example (creating a mailer config):
|
153
|
+
# mailer :alternate, :host => "mail.test.com",
|
154
|
+
# :address => "test@test.com",
|
155
|
+
# :user => "test",
|
156
|
+
# :password => "blinky"
|
157
|
+
# :auth_type => :cram_md5
|
158
|
+
#
|
159
|
+
# Example (retreiving a mailer config):
|
160
|
+
# mail_conf = mailer(:alternate) #=> <OpenStruct ..>
|
161
|
+
# mail_conf.address #=> test@test.com
|
162
|
+
#
|
163
|
+
def mailer(*args)
|
164
|
+
return mailers[args.first] if args.length == 1
|
165
|
+
mailers[args.first] = OpenStruct.new(*args[1..-1])
|
166
|
+
check_mailer(mailers[args.first],args.first)
|
167
|
+
end
|
168
|
+
|
169
|
+
# Alias for <tt>mailers[:default]</tt>.
|
170
|
+
def default_mailer
|
171
|
+
mailers[:default]
|
172
|
+
end
|
173
|
+
|
174
|
+
# Returns all the <tt>mailer</tt>s defined in this <tt>Config</tt>.
|
175
|
+
def mailers; @mailers ||= {}; end
|
176
|
+
end
|
177
|
+
|
178
|
+
|
179
|
+
end
|
data/test/test_mailer.rb
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
begin; require "rubygems"; rescue LoadError; nil end
|
3
|
+
require "ruport"
|
4
|
+
require "ruport/util/mailer"
|
5
|
+
begin
|
6
|
+
require 'mocha'
|
7
|
+
require 'stubba'
|
8
|
+
require 'net/smtp'
|
9
|
+
rescue LoadError
|
10
|
+
$stderr.puts "Warning: Mocha not found -- skipping some Mailer tests"
|
11
|
+
end
|
12
|
+
|
13
|
+
class TestMailer < Test::Unit::TestCase
|
14
|
+
|
15
|
+
def setup
|
16
|
+
@default_opts = {
|
17
|
+
:host => "mail.example.com",
|
18
|
+
:address => "sue@example.com",
|
19
|
+
:user => "inky",
|
20
|
+
:password => "chunky"
|
21
|
+
}
|
22
|
+
|
23
|
+
@other_opts = {
|
24
|
+
:host => "moremail.example.com",
|
25
|
+
:address => "clyde@example.com",
|
26
|
+
:user => "blinky",
|
27
|
+
:password => "bacon"
|
28
|
+
}
|
29
|
+
|
30
|
+
Ruport::Config.mailer :default, @default_opts
|
31
|
+
Ruport::Config.mailer :other, @other_opts
|
32
|
+
|
33
|
+
@default_mailer = Ruport::Mailer.new
|
34
|
+
@other_mailer = Ruport::Mailer.new :other
|
35
|
+
end
|
36
|
+
|
37
|
+
def assert_mailer_equal(expected, mailer)
|
38
|
+
assert_equal expected[:host], mailer.instance_variable_get(:@host)
|
39
|
+
assert_equal expected[:address], mailer.instance_variable_get(:@address)
|
40
|
+
assert_equal expected[:user], mailer.instance_variable_get(:@user)
|
41
|
+
assert_equal expected[:password], mailer.instance_variable_get(:@password)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_send_to
|
45
|
+
return unless Object.const_defined? :Mocha
|
46
|
+
setup_mock_report_mailer
|
47
|
+
@report = Ruport::Report.new
|
48
|
+
assert_equal "250 ok", @report.send_to("clyde@example.com") { |mail|
|
49
|
+
mail.subject = "Test Report"
|
50
|
+
mail.text = "Test"
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_mail_defaults
|
55
|
+
assert_equal( Ruport::Config.mailers[:default],
|
56
|
+
Ruport::Config.default_mailer)
|
57
|
+
end
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
def test_default_constructor
|
62
|
+
assert_mailer_equal @default_opts, @default_mailer
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_constructor_with_mailer_label
|
66
|
+
assert_mailer_equal @other_opts, @other_mailer
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_constructor_without_mailer
|
70
|
+
Ruport::Config.mailers[:default] = nil
|
71
|
+
assert_raise(RuntimeError) { Ruport::Mailer.new }
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_select_mailer
|
75
|
+
mailer = Ruport::Mailer.new :default
|
76
|
+
assert_mailer_equal @default_opts, mailer
|
77
|
+
|
78
|
+
mailer.send(:select_mailer, :other)
|
79
|
+
assert_mailer_equal @other_opts, mailer
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_mailer_config_errors
|
83
|
+
assert_raise(ArgumentError) {
|
84
|
+
Ruport::Config.mailer :bar, :user => :foo, :address => "foo@bar.com"
|
85
|
+
}
|
86
|
+
assert_nothing_raised { Ruport::Config.mailer :bar, :host => "localhost" }
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
def test_to
|
92
|
+
@default_mailer.instance_eval "@mail.to = ['foo@bar.com']"
|
93
|
+
assert_equal ['foo@bar.com'], @default_mailer.to
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_to_equals
|
97
|
+
@default_mailer.to = ['foo@bar.com']
|
98
|
+
assert_equal ['foo@bar.com'], @default_mailer.to
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_from
|
102
|
+
@default_mailer.instance_eval "@mail.from = ['foo@bar.com']"
|
103
|
+
assert_equal ['foo@bar.com'], @default_mailer.from
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_from_equals
|
107
|
+
@default_mailer.from = ['foo@bar.com']
|
108
|
+
assert_equal ['foo@bar.com'], @default_mailer.from
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_subject
|
112
|
+
@default_mailer.instance_eval "@mail.subject = ['RuportDay!']"
|
113
|
+
assert_equal ['RuportDay!'], @default_mailer.subject
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_subject_equals
|
117
|
+
@default_mailer.subject = ['RuportDay!']
|
118
|
+
assert_equal ['RuportDay!'], @default_mailer.subject
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_send_mail_with_default
|
122
|
+
return unless Object.const_defined? :Mocha
|
123
|
+
setup_mock_mailer(1)
|
124
|
+
assert_equal "250 ok",
|
125
|
+
@default_mailer.deliver(:to => "clyde@example.com",
|
126
|
+
:from => "sue@example.com",
|
127
|
+
:subject => "Hello",
|
128
|
+
:text => "This is a test.")
|
129
|
+
end
|
130
|
+
|
131
|
+
def test_send_mail_with_other
|
132
|
+
return unless Object.const_defined? :Mocha
|
133
|
+
setup_mock_mailer(1, @other_mailer)
|
134
|
+
assert_equal "250 ok",
|
135
|
+
@other_mailer.deliver(:to => "sue@example.com",
|
136
|
+
:from => "clyde@example.com",
|
137
|
+
:subject => "Hello",
|
138
|
+
:text => "This is a test.")
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_send_mail_without_to
|
142
|
+
return unless Object.const_defined? :Mocha
|
143
|
+
setup_mock_mailer(1)
|
144
|
+
assert_raise(Net::SMTPSyntaxError) {
|
145
|
+
@default_mailer.deliver(:from => "sue@example.com",
|
146
|
+
:subject => "Hello",
|
147
|
+
:text => "This is a test.")
|
148
|
+
}
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_send_html_mail
|
152
|
+
return unless Object.const_defined? :Mocha
|
153
|
+
setup_mock_mailer(1)
|
154
|
+
assert_equal "250 ok",
|
155
|
+
@default_mailer.deliver(:to => "clyde@example.com",
|
156
|
+
:from => "sue@example.com",
|
157
|
+
:subject => "Hello",
|
158
|
+
:html => "<p>This is a test.</p>")
|
159
|
+
end
|
160
|
+
|
161
|
+
def test_send_mail_with_attachment
|
162
|
+
return unless Object.const_defined? :Mocha
|
163
|
+
setup_mock_mailer(1)
|
164
|
+
@default_mailer.attach 'test/samples/data.csv'
|
165
|
+
assert_equal "250 ok",
|
166
|
+
@default_mailer.deliver(:to => "clyde@example.com",
|
167
|
+
:from => "sue@example.com",
|
168
|
+
:subject => "Hello",
|
169
|
+
:text => "This is a test.")
|
170
|
+
end
|
171
|
+
|
172
|
+
private
|
173
|
+
|
174
|
+
def setup_mock_mailer(count, mailer=@default_mailer)
|
175
|
+
host = mailer.instance_variable_get(:@host)
|
176
|
+
port = mailer.instance_variable_get(:@port)
|
177
|
+
user = mailer.instance_variable_get(:@user)
|
178
|
+
password = mailer.instance_variable_get(:@password)
|
179
|
+
auth = mailer.instance_variable_get(:@auth)
|
180
|
+
|
181
|
+
@smtp = mock('smtp')
|
182
|
+
|
183
|
+
Net::SMTP.expects(:start).
|
184
|
+
with(host,port,host,user,password,auth).
|
185
|
+
yields(@smtp).
|
186
|
+
returns("250 ok").times(count)
|
187
|
+
@smtp.stubs(:send_message).
|
188
|
+
with {|*params| !params[2].nil? }.
|
189
|
+
returns("250 ok")
|
190
|
+
@smtp.stubs(:send_message).
|
191
|
+
with {|*params| params[2].nil? }.
|
192
|
+
raises(Net::SMTPSyntaxError)
|
193
|
+
end
|
194
|
+
|
195
|
+
def setup_mock_report_mailer
|
196
|
+
@smtp = mock('smtp')
|
197
|
+
|
198
|
+
Net::SMTP.expects(:start).
|
199
|
+
yields(@smtp).
|
200
|
+
returns("250 ok").at_least_once
|
201
|
+
@smtp.stubs(:send_message).
|
202
|
+
returns("250 ok")
|
203
|
+
end
|
204
|
+
end
|
205
|
+
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
|
|
3
3
|
specification_version: 1
|
4
4
|
name: ruport-util
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date: 2007-04-
|
6
|
+
version: 0.2.0
|
7
|
+
date: 2007-04-10 00:00:00 -04:00
|
8
8
|
summary: A set of tools and helper libs for Ruby Reports
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -29,24 +29,31 @@ post_install_message:
|
|
29
29
|
authors:
|
30
30
|
- Gregory Brown
|
31
31
|
files:
|
32
|
-
- example/
|
32
|
+
- example/mailed_report.rb
|
33
33
|
- example/invoice_report.rb
|
34
34
|
- example/managed_report.rb
|
35
|
+
- example/mailer.rb
|
36
|
+
- example/graph_report.rb
|
35
37
|
- lib/ruport
|
36
38
|
- lib/ruport/util
|
37
39
|
- lib/ruport/util.rb
|
38
40
|
- lib/ruport/util/graph.rb
|
39
|
-
- lib/ruport/util/invoice.rb
|
40
41
|
- lib/ruport/util/report_manager.rb
|
42
|
+
- lib/ruport/util/mailer.rb
|
43
|
+
- lib/ruport/util/invoice.rb
|
44
|
+
- test/samples
|
41
45
|
- test/init.rb
|
46
|
+
- test/test_report_manager.rb
|
42
47
|
- test/test_graph_renderer.rb
|
48
|
+
- test/test_mailer.rb
|
43
49
|
- test/test_invoice.rb
|
44
|
-
- test/
|
50
|
+
- test/samples/data.csv
|
45
51
|
- Rakefile
|
46
52
|
test_files:
|
53
|
+
- test/test_report_manager.rb
|
47
54
|
- test/test_graph_renderer.rb
|
55
|
+
- test/test_mailer.rb
|
48
56
|
- test/test_invoice.rb
|
49
|
-
- test/test_report_manager.rb
|
50
57
|
rdoc_options:
|
51
58
|
- --title
|
52
59
|
- Ruport Documentation
|
@@ -77,3 +84,12 @@ dependencies:
|
|
77
84
|
- !ruby/object:Gem::Version
|
78
85
|
version: 0.2.2
|
79
86
|
version:
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: mailfactory
|
89
|
+
version_requirement:
|
90
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 1.2.3
|
95
|
+
version:
|