bjornblomqvist-emailer 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
data/emailer.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{emailer}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Erik Hansson", "Bjorn Blomqvist"]
12
- s.date = %q{2009-09-18}
12
+ s.date = %q{2009-09-21}
13
13
  s.description = %q{}
14
14
  s.email = %q{erik@bits2life.com}
15
15
  s.extra_rdoc_files = [
@@ -31,11 +31,14 @@ Gem::Specification.new do |s|
31
31
  "lib/emailer/mock_smtp_facade.rb",
32
32
  "lib/emailer/smtp_facade.rb",
33
33
  "lib/emailer/string_utilities.rb",
34
+ "lib/emailer/tofile_smtp_facade.rb",
34
35
  "spec/emailer/authsmtp_facade_spec.rb",
35
36
  "spec/emailer/logger_smtp_facade_spec.rb",
36
37
  "spec/emailer/mail_queue_spec.rb",
37
38
  "spec/emailer/mock_smtp_facade_spec.rb",
38
39
  "spec/emailer/smtp_facade_spec.rb",
40
+ "spec/emailer/tofile_smtp_facade_spec.rb",
41
+ "spec/spec.opts",
39
42
  "spec/spec_helper.rb"
40
43
  ]
41
44
  s.has_rdoc = true
@@ -50,6 +53,7 @@ Gem::Specification.new do |s|
50
53
  "spec/emailer/mail_queue_spec.rb",
51
54
  "spec/emailer/mock_smtp_facade_spec.rb",
52
55
  "spec/emailer/smtp_facade_spec.rb",
56
+ "spec/emailer/tofile_smtp_facade_spec.rb",
53
57
  "spec/spec_helper.rb"
54
58
  ]
55
59
 
data/lib/emailer.rb CHANGED
@@ -1,6 +1,8 @@
1
1
 
2
- require 'emailer/smtp_facade'
3
- require 'emailer/authsmtp_facade'
4
- require 'emailer/mail_queue'
5
- require 'emailer/string_utilities'
6
- require 'emailer/mock_smtp_facade.rb'
2
+ require File.join File.dirname(__FILE__), 'emailer/smtp_facade'
3
+ require File.join File.dirname(__FILE__), 'emailer/authsmtp_facade'
4
+ require File.join File.dirname(__FILE__), 'emailer/mail_queue'
5
+ require File.join File.dirname(__FILE__), 'emailer/string_utilities'
6
+ require File.join File.dirname(__FILE__), 'emailer/mock_smtp_facade'
7
+ require File.join File.dirname(__FILE__), 'emailer/logger_smtp_facade'
8
+ require File.join File.dirname(__FILE__), 'emailer/tofile_smtp_facade'
@@ -1,12 +1,45 @@
1
1
  module Emailer
2
-
2
+
3
3
  class LoggerSmtpFacade < MockSmtpFacade
4
-
4
+
5
+ def initialize(settings = {})
6
+ @logger_settings = settings.clone
7
+ @logger_settings.keys.each do |key|
8
+ raise ArgumentError.new("invalid option, {"+key.to_s+" => "+@logger_settings[key].to_s+"}") unless
9
+ [:log_file, :use].include? key
10
+ end
11
+ raise ArgumentError.new(":log_file location is missing") unless @logger_settings[:log_file]
12
+ settings.clear
13
+ super
14
+ end
15
+
16
+ def open
17
+ if @logger_settings[:use]
18
+ @logger_settings[:use].open do
19
+ super
20
+ end
21
+ else
22
+ super
23
+ end
24
+ end
25
+
5
26
  # And save, don't send, write to a singel file
6
27
  def send_mail(options)
7
- super options
28
+ super
8
29
 
30
+ @logger_settings[:use].send_mail options if @logger_settings[:use]
31
+
32
+ File.open(@logger_settings[:log_file],File::WRONLY|File::APPEND|File::CREAT) do |f|
33
+
34
+ f.print "email.add {\n"
35
+ options.each do |option|
36
+ f.print "\t:"+option.first.to_s+" => \""+option.last.to_s+"\"\n" unless option.first == :body
37
+ end
38
+ f.print "\t:sent_at => \""+Time.now.to_s+"\"\n"
39
+ f.print "\t:body => \""+options[:body].to_s+"\"\n"
40
+ f.print "}\n"
41
+ end
9
42
  end
10
-
43
+
11
44
  end
12
45
  end
@@ -15,7 +15,7 @@ module Emailer
15
15
  def initialize(settings)
16
16
  @settings = settings
17
17
  @settings.keys.each do |key|
18
- raise ArgumentError unless [
18
+ raise ArgumentError.new("invalid option, {"+key.to_s+" => "+@settings[key].to_s+"}") unless [
19
19
  :host, :port, :username, :password, :authentication, :domain
20
20
  ].include?(key)
21
21
  end
@@ -0,0 +1,221 @@
1
+ module Emailer
2
+
3
+ class ToFileSmtpFacade < MockSmtpFacade
4
+
5
+ def initialize(settings = {})
6
+ @tofile_settings = settings.clone
7
+ @tofile_settings.keys.each do |key|
8
+ raise ArgumentError.new("invalid option, {"+key.to_s+" => "+@tofile_settings[key].to_s+"}") unless
9
+ [:file_dir, :use].include? key
10
+ end
11
+ raise ArgumentError.new(":log_file location is missing") unless @tofile_settings[:file_dir]
12
+ settings.clear
13
+ super
14
+ end
15
+
16
+ def open
17
+ if(@tofile_settings[:use])
18
+ @tofile_settings[:use].open do
19
+ super
20
+ end
21
+ else
22
+ super
23
+ end
24
+ end
25
+
26
+ def send_mail options
27
+ super
28
+
29
+ @tofile_settings[:use].send_mail options if @tofile_settings[:use]
30
+
31
+ options = options.clone
32
+
33
+ options[:sent_at] = Time.now.to_s
34
+
35
+ Dir.mkdir @tofile_settings[:file_dir] unless File.exist? @tofile_settings[:file_dir]
36
+
37
+ file = @tofile_settings[:file_dir]+"/"+"sent_at("+(options[:sent_at].gsub(" ","_"))+").to("+options[:to]+")"+".from("+options[:from]+").html"
38
+ body_file = "sent_at("+(options[:sent_at].gsub(" ","_"))+").to("+options[:to]+")"+".from("+options[:from]+")_body.html"
39
+ latest_file = @tofile_settings[:file_dir]+"/"+"latest.html"
40
+
41
+ head = ""
42
+ body = options[:body]
43
+
44
+ options.each do |option|
45
+ head += "<div class=\"label\">"+option.first.to_s.capitalize.gsub("_"," ")+":</div><div class=\"value\">"+option.last.to_s+"</div>\n" unless option.first == :body
46
+ end
47
+
48
+
49
+
50
+ if body.downcase.include? "<html>"
51
+ body = %&<iframe src="#{body_file}"></iframe>&
52
+
53
+ File.open(@tofile_settings[:file_dir]+"/"+body_file,File::WRONLY|File::CREAT) do |file|
54
+ file.print options[:body]
55
+ end
56
+ else
57
+ body = "<pre>"+body+"</pre>"
58
+ end
59
+
60
+
61
+ toWrite = <<ANEWHTML
62
+ <html>
63
+ <head>
64
+ <title>#{options[:subject]}</title>
65
+ <style type="text/css">
66
+ div.label {
67
+ clear: left;
68
+ float: left;
69
+ width: 80px;
70
+ }
71
+
72
+ div.value {
73
+ float: left;
74
+ }
75
+
76
+ div.head {
77
+ background-color: #aaa;
78
+ border-bottom: solid 1px;
79
+ float: left;
80
+ width: 100%;
81
+ }
82
+
83
+ div.head_inner {
84
+ float: left;
85
+ padding: 15px;
86
+ }
87
+
88
+ div.head_back {
89
+ float: left;
90
+ }
91
+
92
+
93
+ body {
94
+ padding: 0px;
95
+ margin: 0px;
96
+ }
97
+
98
+ div.body {
99
+ float: left;
100
+ width: 100%;
101
+ padding: 0px;
102
+ margin: 0px;
103
+ }
104
+
105
+ iframe {
106
+ width: 100%;
107
+ height: 100%;
108
+ border: none;
109
+ padding: 0px;
110
+ margin: 0px;
111
+ }
112
+ </style>
113
+ </head>
114
+ <body>
115
+ <div class="head">
116
+ <div class="head_inner">
117
+ #{head}
118
+ </div>
119
+ <div class="head_back">
120
+ <a href="index.html"><h1>Back &gt;&gt;</h1></a>
121
+ </div>
122
+ </div>
123
+ <div class="body">
124
+ #{body}
125
+ </div>
126
+ </body>
127
+ </html>
128
+ ANEWHTML
129
+
130
+
131
+
132
+ File.open(file,File::WRONLY|File::CREAT) do |f|
133
+ f.print toWrite
134
+ end
135
+
136
+ File.unlink latest_file if File.exists? latest_file
137
+ File.symlink file,latest_file
138
+
139
+ createIndex
140
+
141
+ end
142
+
143
+ def createIndex
144
+
145
+ index = @tofile_settings[:file_dir]+"/index.html"
146
+
147
+ File.open(index,File::WRONLY|File::CREAT) do |f|
148
+
149
+ links = ""
150
+
151
+ counter = 0
152
+
153
+ Dir.open(@tofile_settings[:file_dir]).sort.each do |fileName|
154
+ if(fileName.index("sent_at") == 0 && fileName.index("_body") == nil)
155
+ counter += 1
156
+ links = %&<div class="number">#{counter}</div><div class="link #{counter % 2 == 0 ? "evan" : "odd"}"><a href="#{fileName}">#{fileName}</a></div>\n& + links
157
+ end
158
+ end
159
+
160
+ f.print <<HTML
161
+
162
+ <html>
163
+ <head>
164
+ <title></title>
165
+ <style type="text/css">
166
+ body {
167
+ padding: 10px;
168
+ margin: 0px;
169
+ width: 900px;
170
+ margin: auto;
171
+ }
172
+
173
+ div.number {
174
+ float: left;
175
+ clear: both;
176
+ padding: 5px 10px;
177
+ background-color: #aaa;
178
+ border-right: solid 1px;
179
+ width: 30px;
180
+ }
181
+
182
+ div.link {
183
+ padding: 5px 10px;
184
+ float: left;
185
+ width: 800px;
186
+ }
187
+
188
+ a {
189
+ color: black;
190
+ }
191
+
192
+ a:hover {
193
+ text-decoration: none;
194
+ }
195
+
196
+ div.evan {
197
+ background-color: #dddddd;
198
+ }
199
+
200
+ div.evan {
201
+ background-color: #e5e5e5;
202
+ }
203
+
204
+ div.link:hover {
205
+ background-color: yellow;
206
+ }
207
+
208
+ </style>
209
+ </head>
210
+ <body>
211
+ #{links}
212
+ </body>
213
+ </html>
214
+
215
+ HTML
216
+
217
+ end
218
+ end
219
+
220
+ end
221
+ end
@@ -1,16 +1,19 @@
1
+ require File.join(File.dirname(__FILE__), "../spec_helper")
2
+
1
3
  module Emailer
2
-
3
- LOGGER_OPTIONS = {
4
- :temp_dir => 'smtp.host.com',
5
- :use => MockSmtpFacade.new
6
- }
7
-
8
4
  describe LoggerSmtpFacade do
5
+
6
+ LOGGER_OPTIONS = {
7
+ :log_file => '/tmp/emailLog.txt',
8
+ :use => MockSmtpFacade.new
9
+ }
10
+
11
+ TEST_LOG_FILE = '/tmp/test_emailLog.txt'
9
12
 
10
13
  describe :initialize do
11
14
  it "should accept only known options" do
12
15
  lambda do
13
- LoggerSmtpFacade.new :foobar => 'fail'
16
+ LoggerSmtpFacade.new(:log_file => '/tmp/emailLog.txt', :foobar => 'fail')
14
17
  end.should raise_error
15
18
  end
16
19
 
@@ -21,11 +24,53 @@ module Emailer
21
24
  it "should demand temp_dir option" do
22
25
  lambda do
23
26
  LoggerSmtpFacade.new
24
- end.shouldh raise_error
27
+ end.should raise_error
25
28
  end
26
29
  end
27
-
28
-
30
+
31
+ describe :send_mail do
32
+
33
+ before(:each) do
34
+ File.unlink(TEST_LOG_FILE) if File.exist?(TEST_LOG_FILE)
35
+ end
36
+
37
+ it "should write the email to the log file" do
38
+
39
+ smtp = LoggerSmtpFacade.new :log_file => TEST_LOG_FILE
40
+
41
+ smtp.open do
42
+ smtp.send_mail(
43
+ :to => "test@bits2life.com",
44
+ :from => "test2@bits2life.com",
45
+ :subject => "This is a test 3",
46
+ :body => "Blabla"
47
+ )
48
+ end
49
+
50
+ ((File.read(TEST_LOG_FILE,File::RDONLY|File::CREAT).to_s+"").include? "This is a test 3").should == true
51
+ end
52
+
53
+ it "should log and pass the email on to the supplied SmtpFacade" do
54
+
55
+ mock = MockSmtpFacade.new
56
+ smtp = LoggerSmtpFacade.new :log_file => TEST_LOG_FILE, :use => mock
57
+
58
+ email = {
59
+ :to => "test@bits2life.com",
60
+ :from => "test2@bits2life.com",
61
+ :subject => "This is a test 4",
62
+ :body => "Test body"
63
+ }
64
+
65
+ smtp.open do
66
+ smtp.send_mail email
67
+ end
68
+
69
+ mock.sent.first.should == email
70
+
71
+ end
72
+ end
73
+
29
74
  # Should be able to wrap another smtp facade
30
75
  # Should have temp directory configurable
31
76
  # Should be abel to log to one file
@@ -0,0 +1,68 @@
1
+ require File.join(File.dirname(__FILE__), "../spec_helper")
2
+
3
+ module Emailer
4
+ describe ToFileSmtpFacade do
5
+
6
+ FILE_DIR = '/tmp/emails/'
7
+
8
+ TOFILE_OPTIONS = {
9
+ :file_dir => FILE_DIR,
10
+ :use => nil
11
+ }
12
+
13
+ describe :init do
14
+ it 'Should accept all valid options' do
15
+ ToFileSmtpFacade.new TOFILE_OPTIONS
16
+ end
17
+
18
+ it 'Should only accept valid options' do
19
+ lambda do
20
+ ToFileSmtpFacade.new :file_dir => FILE_DIR, :foo => 'foo'
21
+ end.should raise_error
22
+ end
23
+
24
+ it 'Should demand that :file_dir option is supplied' do
25
+ lambda do
26
+ ToFileSmtpFacade.new
27
+ end.should raise_error
28
+ end
29
+ end
30
+
31
+ describe :send_mail do
32
+ it 'Should create a new file with the name of from and to addresses' do
33
+ smtp = ToFileSmtpFacade.new :file_dir => FILE_DIR
34
+ smtp.open do
35
+ smtp.send_mail(
36
+ :to => 'test@bits2life.com',
37
+ :from => 'test2@bits2life.com',
38
+ :subject => 'a test subject',
39
+ :body => 'A test message'
40
+ )
41
+ end
42
+
43
+ found = false
44
+ Dir.entries(FILE_DIR).each do |entry|
45
+ found = true if entry.include? 'test@bits2life.com'
46
+ end
47
+
48
+ found.should == true
49
+ end
50
+
51
+ it 'Should send an email using the supplied facade' do
52
+ mockSmtp = MockSmtpFacade.new
53
+ smtp = ToFileSmtpFacade.new :file_dir => FILE_DIR, :use => mockSmtp
54
+ smtp.open do
55
+ smtp.send_mail(
56
+ :to => "emma@bits2life.com",
57
+ :from => "bjorn@bits2life.com",
58
+ :subject => 'A test email to emma',
59
+ :body => '<html><head></head><body><h1>This is a test</h1></body></html>'
60
+ )
61
+ end
62
+
63
+ mockSmtp.sent.count.should == 1
64
+ end
65
+
66
+ end
67
+ end
68
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,4 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
data/spec/spec_helper.rb CHANGED
@@ -1,11 +1,3 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
-
4
- require 'emailer'
5
- require 'emailer/mock_smtp_facade'
6
1
  require 'spec'
7
2
  require 'spec/autorun'
8
-
9
- Spec::Runner.configure do |config|
10
-
11
- end
3
+ require File.join(File.dirname(__FILE__), '../lib/emailer')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bjornblomqvist-emailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Erik Hansson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2009-09-18 00:00:00 -07:00
13
+ date: 2009-09-21 00:00:00 -07:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -57,11 +57,14 @@ files:
57
57
  - lib/emailer/mock_smtp_facade.rb
58
58
  - lib/emailer/smtp_facade.rb
59
59
  - lib/emailer/string_utilities.rb
60
+ - lib/emailer/tofile_smtp_facade.rb
60
61
  - spec/emailer/authsmtp_facade_spec.rb
61
62
  - spec/emailer/logger_smtp_facade_spec.rb
62
63
  - spec/emailer/mail_queue_spec.rb
63
64
  - spec/emailer/mock_smtp_facade_spec.rb
64
65
  - spec/emailer/smtp_facade_spec.rb
66
+ - spec/emailer/tofile_smtp_facade_spec.rb
67
+ - spec/spec.opts
65
68
  - spec/spec_helper.rb
66
69
  has_rdoc: true
67
70
  homepage: http://github.com/erikhansson/emailer
@@ -96,4 +99,5 @@ test_files:
96
99
  - spec/emailer/mail_queue_spec.rb
97
100
  - spec/emailer/mock_smtp_facade_spec.rb
98
101
  - spec/emailer/smtp_facade_spec.rb
102
+ - spec/emailer/tofile_smtp_facade_spec.rb
99
103
  - spec/spec_helper.rb