mailthis 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 608767c6e27a04598a5587e1780bd19eebf3c4ef
4
+ data.tar.gz: 662844c65e2222e5511d10da12c178b78602c9cc
5
+ SHA512:
6
+ metadata.gz: 8bf6873b478690277c6e3cf6fc37ae1d3e9259211be70f7a3dc0b4acabde8ec1ef5193da24240c3720afd66015962bd20449e31bb238ab980834332e5f3f8b94
7
+ data.tar.gz: f0c55b73b2ecbde6ed842d8d86caa2c410dfe58392a086b9e2cf781b6e32848c0dd4d4f85b8ec3cf1963fb92d72faeb0d1dfdda774898d98ef66c08b96178e3f
data/Gemfile CHANGED
@@ -2,5 +2,8 @@ source "https://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
- gem 'rake'
6
- gem 'pry'
5
+ gem 'rake', "~> 10.4.0"
6
+ gem 'pry', "~> 0.9.0"
7
+
8
+ # mail dependencies that are 1.8.7 compatible
9
+ gem 'mime-types', "=1.25.1"
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2009-Present Kelly Redding
1
+ Copyright (c) 2009-Present Kelly Redding and Collin Redding
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -43,7 +43,7 @@ GMAIL = Mailthis.mailer do
43
43
  logger Logger.new("log/email.log") # (optional) default: no logger, no logging
44
44
  end
45
45
 
46
- msg = Mail.new
46
+ msg = Mailthis::Message.new
47
47
  msg.from = 'bob@example.com', # (optional) default: mailer #from
48
48
  msg.reply_to = 'bob@me.com', # (optional) default: self #from
49
49
  msg.to = "you@example.com",
@@ -55,6 +55,19 @@ msg.body = "a message body"
55
55
  GMAIL.deliver(msg)
56
56
  ```
57
57
 
58
+ ### Disable sending mail
59
+
60
+ You can disable actually sending mail (in tests, non-production envs, etc) by setting the `MAILTHIS_DISABLE_SEND` environment variable. For example:
61
+
62
+ ```ruby
63
+ if !production?
64
+ # disable actually delivering emails when not in production
65
+ ENV['MAILTHIS_DISABLE_SEND'] = 'y'
66
+ end
67
+ ```
68
+
69
+ Just set the env var to *any* not-nil value and mailthis will not send any mail. It will do everything else, however, including logging the mail it would have sent.
70
+
58
71
  ## Installation
59
72
 
60
73
  Add this line to your application's Gemfile:
@@ -1,41 +1,60 @@
1
1
  require 'ns-options/proxy'
2
- require 'mail'
3
2
  require 'mailthis/exceptions'
3
+ require 'mailthis/message'
4
4
  require 'mailthis/outgoing_email'
5
5
 
6
6
  module Mailthis
7
7
 
8
- class Mailer
9
- include NsOptions::Proxy
10
-
11
- option :smtp_helo, String, :required => true
12
- option :smtp_server, String, :required => true
13
- option :smtp_port, Integer, :required => true
14
- option :smtp_user, String, :required => true
15
- option :smtp_pw, String, :required => true
16
- option :smtp_auth, String, :required => true, :default => proc{ "login" }
17
-
18
- option :from, String, :required => true
19
- option :logger, :required => true, :default => proc{ NullLogger.new }
20
-
21
- def initialize(values=nil, &block)
22
- # this is defaulted here because we want to use the Configuration instance
23
- # `smtp_user`. If we define a proc above, we will be using the Configuration
24
- # class `smtp_user`, which will not update the option as expected.
25
- super((values || {}).merge(:from => proc{ self.smtp_user }))
26
- self.define(&block)
8
+ module Mailer
9
+
10
+ def self.new(*args, &block)
11
+ if !ENV['MAILTHIS_TEST_MODE']
12
+ MailthisMailer.new(*args, &block)
13
+ else
14
+ TestMailer.new(*args, &block)
15
+ end
27
16
  end
28
17
 
29
- def validate!
30
- raise(MailerError, "missing required settings") if !valid?
31
- self # for chaining
18
+ def self.included(klass)
19
+ klass.class_eval do
20
+ include NsOptions::Proxy
21
+ include InstanceMethods
22
+
23
+ option :smtp_helo, String, :required => true
24
+ option :smtp_server, String, :required => true
25
+ option :smtp_port, Integer, :required => true
26
+ option :smtp_user, String, :required => true
27
+ option :smtp_pw, String, :required => true
28
+ option :smtp_auth, String, :required => true, :default => proc{ "login" }
29
+
30
+ option :from, String, :required => true
31
+ option :logger, :required => true, :default => proc{ NullLogger.new }
32
+
33
+ end
32
34
  end
33
35
 
34
- def deliver(message = nil, &block)
35
- (message || ::Mail.new).tap do |msg|
36
- msg.instance_eval(&block) if block
37
- OutgoingEmail.new(self, msg).deliver
36
+ module InstanceMethods
37
+
38
+ def initialize(values = nil, &block)
39
+ # this is defaulted here because we want to use the Configuration instance
40
+ # `smtp_user`. If we define a proc above, we will be using the Configuration
41
+ # class `smtp_user`, which will not update the option as expected.
42
+ super((values || {}).merge(:from => proc{ self.smtp_user }))
43
+ self.define(&block)
38
44
  end
45
+
46
+ def validate!
47
+ raise(MailerError, "missing required settings") if !valid?
48
+ self # for chaining
49
+ end
50
+
51
+ def deliver(message = nil, &block)
52
+ (message || ::Mailthis::Message.new).tap do |msg|
53
+ msg.instance_eval(&block) if block
54
+ OutgoingEmail.new(self, msg).deliver
55
+ end
56
+ end
57
+
39
58
  end
40
59
 
41
60
  class NullLogger
@@ -47,4 +66,31 @@ module Mailthis
47
66
 
48
67
  end
49
68
 
69
+ class MailthisMailer
70
+ include Mailer
71
+
72
+ end
73
+
74
+ class TestMailer
75
+ include Mailer
76
+
77
+ attr_reader :delivered_messages
78
+
79
+ def initialize(*args, &block)
80
+ super
81
+ @delivered_messages = []
82
+ end
83
+
84
+ def deliver(*args, &block)
85
+ super(*args, &block).tap do |msg|
86
+ @delivered_messages << msg
87
+ end
88
+ end
89
+
90
+ def reset
91
+ self.delivered_messages.clear
92
+ end
93
+
94
+ end
95
+
50
96
  end
@@ -0,0 +1,33 @@
1
+ require 'mail'
2
+
3
+ module Mailthis
4
+
5
+ class Message
6
+
7
+ def initialize
8
+ @mail = ::Mail.new
9
+ end
10
+
11
+ def from(*args); @mail.from(*args); end
12
+ def reply_to(*args); @mail.reply_to(*args); end
13
+ def to(*args); @mail.to(*args); end
14
+ def cc(*args); @mail.cc(*args); end
15
+ def bcc(*args); @mail.bcc(*args); end
16
+ def subject(*args); @mail.subject(*args); end
17
+ def body(*args); @mail.body(*args); end
18
+
19
+ def from=(value); @mail.from = value; end
20
+ def reply_to=(value); @mail.reply_to = value; end
21
+ def to=(value); @mail.to = value; end
22
+ def cc=(value); @mail.cc = value; end
23
+ def bcc=(value); @mail.bcc = value; end
24
+ def subject=(value); @mail.subject = value; end
25
+ def body=(value); @mail.body = value; end
26
+
27
+ def to_s
28
+ @mail.to_s
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -1,5 +1,5 @@
1
- require 'mail'
2
1
  require 'mailthis/exceptions'
2
+ require 'mailthis/message'
3
3
  require 'mailthis/net_smtp_tls'
4
4
 
5
5
  module Mailthis
@@ -16,23 +16,21 @@ module Mailthis
16
16
  attr_reader :mailer, :message
17
17
 
18
18
  def initialize(mailer, message)
19
+ raise Mailthis::MessageError, "invalid message" if !valid_message?(message)
20
+
19
21
  @mailer, @message = mailer, message
20
- @message.from ||= @mailer.from if @message.respond_to?(:from=)
21
- @message.reply_to ||= @message.from if @message.respond_to?(:reply_to=)
22
+ @message.from ||= @mailer.from
23
+ @message.reply_to ||= @message.from
22
24
  end
23
25
 
24
26
  def validate!
25
- if !valid_message?
26
- raise Mailthis::MessageError, "invalid message"
27
- end
28
-
29
27
  REQUIRED_FIELDS.each do |field|
30
- if @message.send(field).nil?
28
+ if !field_present?(@message, field)
31
29
  raise Mailthis::MessageError, "missing `#{field}` field"
32
30
  end
33
31
  end
34
32
 
35
- if !address_exists?
33
+ if !fields_present?(@message, ADDRESS_FIELDS)
36
34
  raise Mailthis::MessageError, "no #{ADDRESS_FIELDS.join('/')} specified"
37
35
  end
38
36
  end
@@ -42,21 +40,22 @@ module Mailthis
42
40
  @mailer.validate!
43
41
  deliver_smtp if ENV['MAILTHIS_DISABLE_SEND'].nil?
44
42
 
45
- log_message # and return it
43
+ log_message(@message)
44
+ @message
46
45
  end
47
46
 
48
47
  private
49
48
 
50
- def valid_message?
51
- (REQUIRED_FIELDS + ADDRESS_FIELDS + [:to_s]).inject(true) do |invalid, meth|
52
- invalid && @message.respond_to?(meth)
53
- end
49
+ def valid_message?(message)
50
+ message.kind_of?(Mailthis::Message)
54
51
  end
55
52
 
56
- def address_exists?
57
- ADDRESS_FIELDS.inject(false) do |exists, field|
58
- exists || (!@message.send(field).nil? && !@message.send(field).empty?)
59
- end
53
+ def fields_present?(message, fields)
54
+ fields.inject(false){ |present, f| present || field_present?(message, f) }
55
+ end
56
+
57
+ def field_present?(message, field)
58
+ !message.send(field).nil? && !message.send(field).empty?
60
59
  end
61
60
 
62
61
  def deliver_smtp
@@ -78,15 +77,16 @@ module Mailthis
78
77
  end
79
78
  end
80
79
 
81
- def log_message
82
- @message.tap do |msg|
83
- log "Sent '#{msg.subject}' to #{msg.to ? msg.to.join(', ') : "''"}"
80
+ def log_message(msg)
81
+ log "Sent '#{msg.subject}' to #{msg.to ? msg.to.join(', ') : "''"}"
82
+ log debug_log_entry(msg), :debug
83
+ end
84
84
 
85
- log "\n"\
86
- "==============================================================\n"\
87
- "#{msg}\n"\
88
- "==============================================================\n", :debug
89
- end
85
+ def debug_log_entry(msg)
86
+ "\n"\
87
+ "==============================================================\n"\
88
+ "#{msg}\n"\
89
+ "==============================================================\n"
90
90
  end
91
91
 
92
92
  def log(msg, level = nil)
@@ -1,3 +1,3 @@
1
1
  module Mailthis
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/mailthis.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'mailthis/version'
2
2
  require 'mailthis/exceptions'
3
+ require 'mailthis/message'
3
4
  require 'mailthis/mailer'
4
5
 
5
6
  module Mailthis
data/mailthis.gemspec CHANGED
@@ -6,11 +6,11 @@ require "mailthis/version"
6
6
  Gem::Specification.new do |gem|
7
7
  gem.name = "mailthis"
8
8
  gem.version = Mailthis::VERSION
9
- gem.authors = ["Kelly Redding"]
10
- gem.email = ["kelly@kellyredding.com"]
11
- gem.description = %q{a simple mailer for ruby}
12
- gem.summary = %q{a simple mailer for ruby}
13
- gem.homepage = "http://github.com/kellyredding/mailthis"
9
+ gem.authors = ["Kelly Redding", "Collin Redding"]
10
+ gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
+ gem.description = %q{mailer for ruby}
12
+ gem.summary = %q{mailer for ruby}
13
+ gem.homepage = "http://github.com/redding/mailthis"
14
14
  gem.license = 'MIT'
15
15
 
16
16
  gem.files = `git ls-files`.split($/)
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
- gem.add_development_dependency("assert", ["~> 2.0"])
21
+ gem.add_development_dependency("assert", ["~> 2.14"])
22
22
 
23
23
  gem.add_dependency("ns-options", ["~> 1.1", ">= 1.1.4"])
24
24
  gem.add_dependency("mail", ["~> 2.5"])
data/test/helper.rb CHANGED
@@ -6,6 +6,7 @@ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
6
6
 
7
7
  # require pry for debugging (`binding.pry`)
8
8
  require 'pry'
9
+ require 'test/support/factory'
9
10
 
10
11
  # disable actually delivering emails for testing purposes
11
- ENV['MAILTHIS_DISABLE_SEND'] = 'yes'
12
+ ENV['MAILTHIS_DISABLE_SEND'] = 'y'
@@ -1,4 +1,7 @@
1
+ require 'assert/factory'
2
+
1
3
  module Factory
4
+ extend Assert::Factory
2
5
 
3
6
  def self.mailer
4
7
  require 'mailthis/mailer'
@@ -14,13 +17,13 @@ module Factory
14
17
  end
15
18
 
16
19
  def self.message(settings = nil)
17
- require 'mail'
18
- message = Mail.new do
19
- to 'you@example.com'
20
- subject 'a message'
21
- end
20
+ require 'mailthis/message'
21
+ message = Mailthis::Message.new
22
+ message.to 'you@example.com'
23
+ message.subject 'a message'
24
+
22
25
  (settings || {}).inject(message) do |msg, (setting, value)|
23
- msg[setting] = value
26
+ msg.send("#{setting}=", value)
24
27
  msg
25
28
  end
26
29
  end
@@ -1,15 +1,44 @@
1
1
  require 'assert'
2
2
  require 'mailthis/mailer'
3
3
 
4
- require 'test/support/factory'
5
4
  require 'mailthis/exceptions'
5
+ require 'mailthis/message'
6
6
 
7
- class Mailthis::Mailer
7
+ module Mailthis::Mailer
8
8
 
9
9
  class UnitTests < Assert::Context
10
10
  desc "Mailthis::Mailer"
11
11
  setup do
12
- @mailer = Mailthis::Mailer.new
12
+ @current_test_mode = ENV['MAILTHIS_TEST_MODE']
13
+ ENV['MAILTHIS_TEST_MODE'] = 'yes'
14
+ end
15
+ teardown do
16
+ ENV['MAILTHIS_TEST_MODE'] = @current_test_mode
17
+ end
18
+ subject{ Mailthis::Mailer }
19
+
20
+ should have_imeths :new
21
+
22
+ should "return a mailthis mailer using `new`" do
23
+ ENV.delete('MAILTHIS_TEST_MODE')
24
+ mailer = subject.new
25
+ assert_instance_of Mailthis::MailthisMailer, mailer
26
+ end
27
+
28
+ should "return a test mailer using `new` in test mode" do
29
+ mailer = subject.new
30
+ assert_instance_of Mailthis::TestMailer, mailer
31
+ end
32
+
33
+ end
34
+
35
+ class InitTests < UnitTests
36
+ desc "when init"
37
+ setup do
38
+ @mailer_class = Class.new do
39
+ include Mailthis::Mailer
40
+ end
41
+ @mailer = @mailer_class.new
13
42
  end
14
43
  subject{ @mailer }
15
44
 
@@ -19,11 +48,11 @@ class Mailthis::Mailer
19
48
  should have_imeths :validate!, :deliver
20
49
 
21
50
  should "know its smtp settings" do
22
- { :smtp_helo => 'example.com',
23
- :smtp_server => 'smtp.example.com',
24
- :smtp_port => 25,
25
- :smtp_user => 'test@example.com',
26
- :smtp_pw => 'secret'
51
+ { :smtp_helo => Factory.string,
52
+ :smtp_server => Factory.string,
53
+ :smtp_port => Factory.integer,
54
+ :smtp_user => Factory.email,
55
+ :smtp_pw => Factory.string
27
56
  }.each do |setting, val|
28
57
  assert_nil subject.send(setting)
29
58
 
@@ -39,7 +68,7 @@ class Mailthis::Mailer
39
68
  assert_equal 'plain', subject.smtp_auth
40
69
  end
41
70
 
42
- should "use smtp_user as the from by default" do
71
+ should "use the smtp user as the from by default" do
43
72
  assert_nil subject.from
44
73
 
45
74
  subject.smtp_user 'user'
@@ -51,21 +80,65 @@ class Mailthis::Mailer
51
80
  assert_equal 'a-user', subject.from
52
81
  end
53
82
 
54
- should "know its logger" do
83
+ should "use a null logger by default" do
55
84
  assert_kind_of Mailthis::Mailer::NullLogger, subject.logger
56
85
  end
57
86
 
58
87
  end
59
88
 
89
+ class SendMailTests < InitTests
90
+ desc "and sending mail"
91
+ setup do
92
+ @message = Factory.message(:from => "me@example.com")
93
+ @mailer = Factory.mailer
94
+ @mailer.logger = Factory.logger(@out = "")
95
+
96
+ @sent_msg = @mailer.deliver(@message)
97
+ end
98
+
99
+ should "return the message that was sent" do
100
+ assert_same @message, @sent_msg
101
+ end
102
+
103
+ should "log that the message was sent" do
104
+ assert_not_empty @out
105
+ end
106
+
107
+ should "build the message from the given block" do
108
+ built_msg = @mailer.deliver do
109
+ from 'me@example.com'
110
+ to 'you@example.com'
111
+ subject 'a message'
112
+ end
113
+
114
+ assert_kind_of Mailthis::Message, built_msg
115
+ assert_equal ['me@example.com'], built_msg.from
116
+ assert_equal ['you@example.com'], built_msg.to
117
+ assert_equal 'a message', built_msg.subject
118
+ end
119
+
120
+ should "task a message and apply the given block" do
121
+ built_msg = @mailer.deliver(Factory.message) do
122
+ from 'me@example.com'
123
+ end
124
+
125
+ assert_kind_of Mailthis::Message, built_msg
126
+ assert_equal ['me@example.com'], built_msg.from
127
+ assert_equal ['you@example.com'], built_msg.to
128
+ assert_equal 'a message', built_msg.subject
129
+ end
130
+
131
+ end
132
+
60
133
  class ValidationTests < UnitTests
61
134
  desc "when validating"
62
135
  setup do
63
136
  @invalid = Mailthis::Mailer.new do
64
- smtp_helo "example.com"
65
- smtp_server "smtp.example.com"
66
- smtp_port 25
67
- smtp_user "test@example.com"
68
- smtp_pw "secret"
137
+ smtp_helo Factory.string
138
+ smtp_server Factory.string
139
+ smtp_port Factory.integer
140
+ smtp_user Factory.email
141
+ smtp_pw Factory.string
69
142
  smtp_auth :plain
70
143
  end
71
144
  end
@@ -135,46 +208,60 @@ class Mailthis::Mailer
135
208
 
136
209
  end
137
210
 
138
- class SendMailTests < UnitTests
139
- desc "when sending mail"
211
+ class MailthisMailerTests < UnitTests
212
+ desc "MailthisMailer"
140
213
  setup do
141
- @message = Factory.message(:from => "me@example.com")
142
- @mailer = Factory.mailer
143
- @mailer.logger = Factory.logger(@out = "")
214
+ @mailer_class = Mailthis::MailthisMailer
215
+ end
216
+ subject{ @mailer_class }
144
217
 
145
- @sent_msg = @mailer.deliver(@message)
218
+ should "include the Mailer mixin" do
219
+ assert_includes Mailthis::Mailer, subject
146
220
  end
147
221
 
148
- should "return the message that was sent" do
149
- assert_same @message, @sent_msg
222
+ end
223
+
224
+ class TestMailerTests < UnitTests
225
+ desc "TestMailer"
226
+ setup do
227
+ @mailer_class = Mailthis::TestMailer
150
228
  end
229
+ subject{ @mailer_class }
151
230
 
152
- should "log that the message was sent" do
153
- assert_not_empty @out
231
+ should "include the Mailer mixin" do
232
+ assert_includes Mailthis::Mailer, subject
154
233
  end
155
234
 
156
- should "build the message from the given block" do
157
- built_msg = @mailer.deliver do
158
- from 'me@example.com'
159
- to 'you@example.com'
160
- subject 'a message'
161
- end
235
+ end
162
236
 
163
- assert_kind_of ::Mail::Message, built_msg
164
- assert_equal ['me@example.com'], built_msg.from
165
- assert_equal ['you@example.com'], built_msg.to
166
- assert_equal 'a message', built_msg.subject
237
+ class TestMailerInitTests < TestMailerTests
238
+ desc "when init"
239
+ setup do
240
+ @message = Factory.message
241
+ @mailer = Factory.mailer # b/c in test mode, this is a test mailer
167
242
  end
243
+ subject{ @mailer }
168
244
 
169
- should "task a message and apply the given block" do
170
- built_msg = @mailer.deliver(Factory.message) do
171
- from 'me@example.com'
172
- end
245
+ should have_readers :delivered_messages
246
+ should have_imeths :reset
173
247
 
174
- assert_kind_of ::Mail::Message, built_msg
175
- assert_equal ['me@example.com'], built_msg.from
176
- assert_equal ['you@example.com'], built_msg.to
177
- assert_equal 'a message', built_msg.subject
248
+ should "not have any delivered messages by default" do
249
+ assert_empty subject.delivered_messages
250
+ end
251
+
252
+ should "add messages to its delivered messages when delivering them" do
253
+ subject.deliver(@message)
254
+
255
+ assert_equal 1, subject.delivered_messages.size
256
+ assert_same @message, subject.delivered_messages.last
257
+ end
258
+
259
+ should "clear its delivered messages on reset" do
260
+ subject.deliver(@message)
261
+ assert_equal 1, subject.delivered_messages.size
262
+
263
+ subject.reset
264
+ assert_empty subject.delivered_messages
178
265
  end
179
266
 
180
267
  end
@@ -10,25 +10,33 @@ module Mailthis
10
10
  desc "Mailthis"
11
11
  subject{ Mailthis }
12
12
 
13
- should have_imeth :mailer
13
+ should have_imeths :mailer
14
14
 
15
15
  should "build a mailer with given args" do
16
+ helo = Factory.string
17
+ server = Factory.string
18
+ port = Factory.integer
19
+ user = Factory.email
20
+ pw = Factory.string
21
+ auth = [:plain, :login].choice
22
+
16
23
  mailer = Mailthis.mailer do
17
- smtp_helo "example.com"
18
- smtp_server "smtp.example.com"
19
- smtp_port 25
20
- smtp_user "test@example.com"
21
- smtp_pw "secret"
22
- smtp_auth :plain
24
+ smtp_helo helo
25
+ smtp_server server
26
+ smtp_port port
27
+ smtp_user user
28
+ smtp_pw pw
29
+ smtp_auth auth
23
30
  end
24
31
 
25
32
  assert_kind_of Mailer, mailer
26
- assert_equal "example.com", mailer.smtp_helo
27
- assert_equal "smtp.example.com", mailer.smtp_server
28
- assert_equal 25, mailer.smtp_port
29
- assert_equal "test@example.com", mailer.smtp_user
30
- assert_equal "secret", mailer.smtp_pw
31
- assert_equal 'plain', mailer.smtp_auth
33
+
34
+ assert_equal helo, mailer.smtp_helo
35
+ assert_equal server, mailer.smtp_server
36
+ assert_equal port, mailer.smtp_port
37
+ assert_equal user, mailer.smtp_user
38
+ assert_equal pw, mailer.smtp_pw
39
+ assert_equal auth.to_s, mailer.smtp_auth
32
40
  end
33
41
 
34
42
  should "complain if building a mailer with missing settings" do
@@ -0,0 +1,82 @@
1
+ require 'assert'
2
+ require 'mailthis/message'
3
+
4
+ require 'mail'
5
+
6
+ class Mailthis::Message
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "Mailthis::Message"
10
+ setup do
11
+ @from = Factory.email
12
+ @reply_to = Factory.email
13
+ @to = Factory.email
14
+ @cc = Factory.email
15
+ @bcc = Factory.email
16
+ @subject = Factory.string
17
+ @body = Factory.text
18
+
19
+ @mail = ::Mail.new
20
+ Assert.stub(::Mail, :new){ @mail }
21
+
22
+ @message = Mailthis::Message.new
23
+ end
24
+ subject{ @message }
25
+
26
+ should have_imeths :from, :reply_to, :to, :cc, :bcc, :subject, :body
27
+ should have_imeths :from=, :reply_to=, :to=, :cc=, :bcc=, :subject=, :body=
28
+ should have_imeths :to_s
29
+
30
+ should "use Mail's default attrs" do
31
+ assert_equal @mail.from, subject.from
32
+ assert_equal @mail.reply_to, subject.reply_to
33
+ assert_equal @mail.to, subject.to
34
+ assert_equal @mail.cc, subject.cc
35
+ assert_equal @mail.bcc, subject.bcc
36
+ assert_equal @mail.subject, subject.subject
37
+ assert_equal @mail.body.to_s, subject.body.to_s
38
+ end
39
+
40
+ should "write attrs with traditional writers" do
41
+ subject.from = @from
42
+ subject.reply_to = @reply_to
43
+ subject.to = @to
44
+ subject.cc = @cc
45
+ subject.bcc = @bcc
46
+ subject.subject = @subject
47
+ subject.body = @body
48
+
49
+ assert_equal [@from], subject.from
50
+ assert_equal [@reply_to], subject.reply_to
51
+ assert_equal [@to], subject.to
52
+ assert_equal [@cc], subject.cc
53
+ assert_equal [@bcc], subject.bcc
54
+ assert_equal @subject, subject.subject
55
+ assert_equal @body, subject.body.to_s
56
+ end
57
+
58
+ should "write attrs using DSL methods" do
59
+ subject.from @from
60
+ subject.reply_to @reply_to
61
+ subject.to @to
62
+ subject.cc @cc
63
+ subject.bcc @bcc
64
+ subject.subject @subject
65
+ subject.body @body
66
+
67
+ assert_equal [@from], subject.from
68
+ assert_equal [@reply_to], subject.reply_to
69
+ assert_equal [@to], subject.to
70
+ assert_equal [@cc], subject.cc
71
+ assert_equal [@bcc], subject.bcc
72
+ assert_equal @subject, subject.subject
73
+ assert_equal @body, subject.body.to_s
74
+ end
75
+
76
+ should "know its string representation" do
77
+ assert_equal @mail.to_s, subject.to_s
78
+ end
79
+
80
+ end
81
+
82
+ end
@@ -1,7 +1,6 @@
1
1
  require 'assert'
2
2
  require 'mailthis/outgoing_email'
3
3
 
4
- require 'test/support/factory'
5
4
  require 'mailthis/exceptions'
6
5
 
7
6
  class Mailthis::OutgoingEmail
@@ -35,22 +34,17 @@ class Mailthis::OutgoingEmail
35
34
  assert_equal subject.message.from, subject.message.reply_to
36
35
  end
37
36
 
38
- should "complain if delivering with an invalid mailer" do
39
- @mailer.smtp_server = nil
40
- assert_not @mailer.valid?
41
-
42
- assert_raises(Mailthis::MailerError) do
43
- subject.deliver
37
+ should "complain if given an invalid message" do
38
+ assert_raises(Mailthis::MessageError) do
39
+ Mailthis::OutgoingEmail.new(@mailer, 'invalid-msg')
44
40
  end
45
41
  end
46
42
 
47
- should "complain if delivering an invalid message" do
48
- msg = Factory.message
49
- msg.to = nil
43
+ should "complain if delivering with an invalid mailer" do
44
+ @mailer.smtp_server = nil
45
+ assert_not @mailer.valid?
50
46
 
51
- assert_raises(Mailthis::MessageError) do
52
- Mailthis::OutgoingEmail.new(@mailer, msg).deliver
53
- end
47
+ assert_raises(Mailthis::MailerError){ subject.deliver }
54
48
  end
55
49
 
56
50
  should "log when delivering a message" do
@@ -84,13 +78,14 @@ class Mailthis::OutgoingEmail
84
78
  should "complain if the message is missing required fields" do
85
79
  msg = Factory.message
86
80
  msg.subject = nil
81
+
87
82
  assert_invalid_with msg
88
83
  end
89
84
 
90
85
  should "complain if the message is not addressed to anyone" do
91
86
  msg = Factory.message
92
-
93
87
  msg.to = msg.cc = msg.bcc = nil
88
+
94
89
  assert_invalid_with msg
95
90
  end
96
91
 
metadata CHANGED
@@ -1,86 +1,73 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mailthis
3
- version: !ruby/object:Gem::Version
4
- hash: 27
5
- prerelease:
6
- segments:
7
- - 0
8
- - 1
9
- - 0
10
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
11
5
  platform: ruby
12
- authors:
6
+ authors:
13
7
  - Kelly Redding
8
+ - Collin Redding
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2013-07-22 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2015-07-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: assert
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 2
31
- - 0
32
- version: "2.0"
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.14'
33
21
  type: :development
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: ns-options
37
22
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ~>
42
- - !ruby/object:Gem::Version
43
- hash: 13
44
- segments:
45
- - 1
46
- - 1
47
- version: "1.1"
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '2.14'
28
+ - !ruby/object:Gem::Dependency
29
+ name: ns-options
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.1'
48
35
  - - ">="
49
- - !ruby/object:Gem::Version
50
- hash: 27
51
- segments:
52
- - 1
53
- - 1
54
- - 4
36
+ - !ruby/object:Gem::Version
55
37
  version: 1.1.4
56
38
  type: :runtime
57
- version_requirements: *id002
58
- - !ruby/object:Gem::Dependency
59
- name: mail
60
39
  prerelease: false
61
- requirement: &id003 !ruby/object:Gem::Requirement
62
- none: false
63
- requirements:
64
- - - ~>
65
- - !ruby/object:Gem::Version
66
- hash: 9
67
- segments:
68
- - 2
69
- - 5
70
- version: "2.5"
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '1.1'
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.1.4
48
+ - !ruby/object:Gem::Dependency
49
+ name: mail
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.5'
71
55
  type: :runtime
72
- version_requirements: *id003
73
- description: a simple mailer for ruby
74
- email:
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.5'
62
+ description: mailer for ruby
63
+ email:
75
64
  - kelly@kellyredding.com
65
+ - collin.redding@me.com
76
66
  executables: []
77
-
78
67
  extensions: []
79
-
80
68
  extra_rdoc_files: []
81
-
82
- files:
83
- - .gitignore
69
+ files:
70
+ - ".gitignore"
84
71
  - Gemfile
85
72
  - LICENSE.txt
86
73
  - README.md
@@ -88,6 +75,7 @@ files:
88
75
  - lib/mailthis.rb
89
76
  - lib/mailthis/exceptions.rb
90
77
  - lib/mailthis/mailer.rb
78
+ - lib/mailthis/message.rb
91
79
  - lib/mailthis/net_smtp_tls.rb
92
80
  - lib/mailthis/outgoing_email.rb
93
81
  - lib/mailthis/version.rb
@@ -97,44 +85,37 @@ files:
97
85
  - test/support/factory.rb
98
86
  - test/unit/mailer_tests.rb
99
87
  - test/unit/mailthis_tests.rb
88
+ - test/unit/message_tests.rb
100
89
  - test/unit/outgoing_email_tests.rb
101
90
  - tmp/.gitkeep
102
- homepage: http://github.com/kellyredding/mailthis
103
- licenses:
91
+ homepage: http://github.com/redding/mailthis
92
+ licenses:
104
93
  - MIT
94
+ metadata: {}
105
95
  post_install_message:
106
96
  rdoc_options: []
107
-
108
- require_paths:
97
+ require_paths:
109
98
  - lib
110
- required_ruby_version: !ruby/object:Gem::Requirement
111
- none: false
112
- requirements:
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
113
101
  - - ">="
114
- - !ruby/object:Gem::Version
115
- hash: 3
116
- segments:
117
- - 0
118
- version: "0"
119
- required_rubygems_version: !ruby/object:Gem::Requirement
120
- none: false
121
- requirements:
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
122
106
  - - ">="
123
- - !ruby/object:Gem::Version
124
- hash: 3
125
- segments:
126
- - 0
127
- version: "0"
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
128
109
  requirements: []
129
-
130
110
  rubyforge_project:
131
- rubygems_version: 1.8.24
111
+ rubygems_version: 2.4.5
132
112
  signing_key:
133
- specification_version: 3
134
- summary: a simple mailer for ruby
135
- test_files:
113
+ specification_version: 4
114
+ summary: mailer for ruby
115
+ test_files:
136
116
  - test/helper.rb
137
117
  - test/support/factory.rb
138
118
  - test/unit/mailer_tests.rb
139
119
  - test/unit/mailthis_tests.rb
120
+ - test/unit/message_tests.rb
140
121
  - test/unit/outgoing_email_tests.rb