pony 0.1 → 0.2

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 +2 -1
  2. data/lib/pony.rb +28 -6
  3. data/spec/pony_spec.rb +19 -2
  4. metadata +14 -5
data/Rakefile CHANGED
@@ -29,7 +29,7 @@ require 'rake/clean'
29
29
  require 'rake/gempackagetask'
30
30
  require 'fileutils'
31
31
 
32
- version = "0.1"
32
+ version = "0.2"
33
33
  name = "pony"
34
34
 
35
35
  spec = Gem::Specification.new do |s|
@@ -48,6 +48,7 @@ spec = Gem::Specification.new do |s|
48
48
  s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
49
49
 
50
50
  s.require_path = "lib"
51
+ s.add_dependency( 'tmail', '~> 1.0' )
51
52
  end
52
53
 
53
54
  Rake::GemPackageTask.new(spec) do |p|
@@ -1,11 +1,25 @@
1
1
  require 'rubygems'
2
2
  require 'net/smtp'
3
- require 'tmail'
3
+ begin
4
+ require 'tmail'
5
+ rescue LoadError
6
+ require 'actionmailer'
7
+ end
4
8
 
5
9
  module Pony
6
10
  def self.mail(options)
7
11
  raise(ArgumentError, ":to is required") unless options[:to]
8
- transport build_tmail(options)
12
+
13
+ via = options.delete(:via)
14
+ if via.nil?
15
+ transport build_tmail(options)
16
+ else
17
+ if via_options.include?(via.to_s)
18
+ send("transport_via_#{via}", build_tmail(options))
19
+ else
20
+ raise(ArgumentError, ":via must be either smtp or sendmail")
21
+ end
22
+ end
9
23
  end
10
24
 
11
25
  def self.build_tmail(options)
@@ -18,20 +32,28 @@ module Pony
18
32
  end
19
33
 
20
34
  def self.sendmail_binary
21
- "/usr/sbin/sendmail"
35
+ @sendmail_binary ||= `which sendmail`.chomp
22
36
  end
23
37
 
24
38
  def self.transport(tmail)
25
- if File.exists? sendmail_binary
39
+ if File.executable? sendmail_binary
26
40
  transport_via_sendmail(tmail)
27
41
  else
28
42
  transport_via_smtp(tmail)
29
43
  end
30
44
  end
31
45
 
46
+ def self.via_options
47
+ %w(sendmail smtp)
48
+ end
49
+
32
50
  def self.transport_via_sendmail(tmail)
33
- IO.popen("#{sendmail_binary} #{tmail.to}", "w") do |pipe|
34
- pipe.write tmail.to_s
51
+ IO.popen('-') do |pipe|
52
+ if pipe
53
+ pipe.write(tmail.to_s)
54
+ else
55
+ exec(sendmail_binary, tmail.to)
56
+ end
35
57
  end
36
58
  end
37
59
 
@@ -47,7 +47,7 @@ describe Pony do
47
47
 
48
48
  describe "transport" do
49
49
  it "transports via the sendmail binary if it exists" do
50
- Pony.stub!(:sendmail_binary).and_return(__FILE__)
50
+ File.stub!(:executable?).and_return(true)
51
51
  Pony.should_receive(:transport_via_sendmail).with(:tmail)
52
52
  Pony.transport(:tmail)
53
53
  end
@@ -60,7 +60,7 @@ 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('/usr/sbin/sendmail to', 'w').and_yield(pipe)
63
+ IO.should_receive(:popen).with('-').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
@@ -72,4 +72,21 @@ describe Pony do
72
72
  Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
73
73
  end
74
74
  end
75
+
76
+ describe ":via option should over-ride the default transport mechanism" do
77
+ it "should send via sendmail if :via => sendmail" do
78
+ Pony.should_receive(:transport_via_sendmail)
79
+ Pony.mail(:to => 'joe@example.com', :via => :sendmail)
80
+ end
81
+
82
+ it "should send via smtp if :via => smtp" do
83
+ Pony.should_receive(:transport_via_smtp)
84
+ Pony.mail(:to => 'joe@example.com', :via => :smtp)
85
+ end
86
+
87
+ it "should raise an error if via is neither smtp nor sendmail" do
88
+ lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
89
+ end
90
+ end
91
+
75
92
  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.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-10-29 00:00:00 -07:00
12
+ date: 2009-01-13 00:00:00 -08:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: tmail
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
16
25
  description: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
17
26
  email: adam@heroku.com
18
27
  executables: []
@@ -48,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
48
57
  requirements: []
49
58
 
50
59
  rubyforge_project: pony
51
- rubygems_version: 1.2.0
60
+ rubygems_version: 1.3.1
52
61
  signing_key:
53
62
  specification_version: 2
54
63
  summary: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"