pony 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Rakefile +1 -1
  2. data/lib/pony.rb +14 -7
  3. data/spec/pony_spec.rb +39 -9
  4. metadata +2 -2
data/Rakefile CHANGED
@@ -29,7 +29,7 @@ require 'rake/clean'
29
29
  require 'rake/gempackagetask'
30
30
  require 'fileutils'
31
31
 
32
- version = "0.2"
32
+ version = "0.3"
33
33
  name = "pony"
34
34
 
35
35
  spec = Gem::Specification.new do |s|
@@ -15,7 +15,7 @@ module Pony
15
15
  transport build_tmail(options)
16
16
  else
17
17
  if via_options.include?(via.to_s)
18
- send("transport_via_#{via}", build_tmail(options))
18
+ send("transport_via_#{via}", build_tmail(options), options)
19
19
  else
20
20
  raise(ArgumentError, ":via must be either smtp or sendmail")
21
21
  end
@@ -47,19 +47,26 @@ module Pony
47
47
  %w(sendmail smtp)
48
48
  end
49
49
 
50
- def self.transport_via_sendmail(tmail)
51
- IO.popen('-') do |pipe|
50
+ def self.transport_via_sendmail(tmail, options={})
51
+ IO.popen('-', 'w+') do |pipe|
52
52
  if pipe
53
53
  pipe.write(tmail.to_s)
54
54
  else
55
- exec(sendmail_binary, tmail.to)
55
+ exec(sendmail_binary, *tmail.to)
56
56
  end
57
57
  end
58
58
  end
59
59
 
60
- def self.transport_via_smtp(tmail)
61
- Net::SMTP.start('localhost') do |smtp|
62
- smtp.sendmail(tmail.to_s, tmail.from, tmail.to)
60
+ def self.transport_via_smtp(tmail, options={:smtp => {}})
61
+ default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
62
+ o = default_options[:smtp].merge(options[:smtp])
63
+ smtp = Net::SMTP.new(o[:host], o[:port])
64
+ if o.include?(:auth)
65
+ smtp.start(o[:domain], o[:user], o[:password], o[:auth])
66
+ else
67
+ smtp.start(o[:domain])
63
68
  end
69
+ smtp.send_message tmail.to_s, tmail.from, tmail.to
70
+ smtp.finish
64
71
  end
65
72
  end
@@ -60,19 +60,49 @@ describe Pony do
60
60
 
61
61
  it "transports mail via /usr/sbin/sendmail binary" do
62
62
  pipe = mock('sendmail pipe')
63
- IO.should_receive(:popen).with('-').and_yield(pipe)
63
+ IO.should_receive(:popen).with('-',"w+").and_yield(pipe)
64
64
  pipe.should_receive(:write).with('message')
65
65
  Pony.transport_via_sendmail(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
66
66
  end
67
67
 
68
- it "transports mail via Net::SMTP connecting to localhost" do
69
- smtp = mock('net::smtp object')
70
- Net::SMTP.should_receive(:start).with('localhost').and_yield(smtp)
71
- smtp.should_receive(:sendmail).with('message', 'from', 'to')
72
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
68
+ describe "SMTP transport" do
69
+ before do
70
+ @smtp = mock('net::smtp object')
71
+ @smtp.stub!(:start)
72
+ @smtp.stub!(:send_message)
73
+ @smtp.stub!(:finish)
74
+ Net::SMTP.stub!(:new).and_return(@smtp)
75
+ end
76
+
77
+ it "defaults to localhost as the SMTP server" do
78
+ Net::SMTP.should_receive(:new).with('localhost', '25').and_return(@smtp)
79
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
80
+ end
81
+
82
+ it "uses SMTP authorization when auth key is provided" do
83
+ o = { :smtp => { :user => 'user', :password => 'password', :auth => 'plain'}}
84
+ @smtp.should_receive(:start).with('localhost.localdomain', 'user', 'password', 'plain')
85
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'), o)
86
+ end
87
+
88
+ it "starts the job" do
89
+ @smtp.should_receive(:start)
90
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
91
+ end
92
+
93
+ it "sends a tmail message" do
94
+ @smtp.should_receive(:send_message)
95
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
96
+ end
97
+
98
+ it "finishes the job" do
99
+ @smtp.should_receive(:finish)
100
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
101
+ end
102
+
73
103
  end
74
104
  end
75
-
105
+
76
106
  describe ":via option should over-ride the default transport mechanism" do
77
107
  it "should send via sendmail if :via => sendmail" do
78
108
  Pony.should_receive(:transport_via_sendmail)
@@ -83,10 +113,10 @@ describe Pony do
83
113
  Pony.should_receive(:transport_via_smtp)
84
114
  Pony.mail(:to => 'joe@example.com', :via => :smtp)
85
115
  end
86
-
116
+
87
117
  it "should raise an error if via is neither smtp nor sendmail" do
88
118
  lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
89
119
  end
90
120
  end
91
-
121
+
92
122
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pony
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: "0.3"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-13 00:00:00 -08:00
12
+ date: 2009-02-17 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency