Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

spaces to tabs

  • Loading branch information...
commit e93a99148fe51d1d1f96f57e9e625692ec364cce 1 parent e2604b9
@adamwiggins authored
Showing with 174 additions and 174 deletions.
  1. +57 −57 lib/pony.rb
  2. +117 −117 spec/pony_spec.rb
View
114 lib/pony.rb
@@ -1,72 +1,72 @@
require 'rubygems'
require 'net/smtp'
begin
- require 'tmail'
+ require 'tmail'
rescue LoadError
- require 'actionmailer'
+ require 'actionmailer'
end
module Pony
- def self.mail(options)
- raise(ArgumentError, ":to is required") unless options[:to]
+ def self.mail(options)
+ raise(ArgumentError, ":to is required") unless options[:to]
- via = options.delete(:via)
- if via.nil?
- transport build_tmail(options)
- else
- if via_options.include?(via.to_s)
- send("transport_via_#{via}", build_tmail(options), options)
- else
- raise(ArgumentError, ":via must be either smtp or sendmail")
- end
- end
- end
+ via = options.delete(:via)
+ if via.nil?
+ transport build_tmail(options)
+ else
+ if via_options.include?(via.to_s)
+ send("transport_via_#{via}", build_tmail(options), options)
+ else
+ raise(ArgumentError, ":via must be either smtp or sendmail")
+ end
+ end
+ end
- def self.build_tmail(options)
- mail = TMail::Mail.new
- mail.to = options[:to]
- mail.from = options[:from] || 'pony@unknown'
- mail.subject = options[:subject]
- mail.body = options[:body] || ""
- mail
- end
+ def self.build_tmail(options)
+ mail = TMail::Mail.new
+ mail.to = options[:to]
+ mail.from = options[:from] || 'pony@unknown'
+ mail.subject = options[:subject]
+ mail.body = options[:body] || ""
+ mail
+ end
- def self.sendmail_binary
- @sendmail_binary ||= `which sendmail`.chomp
- end
+ def self.sendmail_binary
+ @sendmail_binary ||= `which sendmail`.chomp
+ end
- def self.transport(tmail)
- if File.executable? sendmail_binary
- transport_via_sendmail(tmail)
- else
- transport_via_smtp(tmail)
- end
- end
+ def self.transport(tmail)
+ if File.executable? sendmail_binary
+ transport_via_sendmail(tmail)
+ else
+ transport_via_smtp(tmail)
+ end
+ end
- def self.via_options
- %w(sendmail smtp)
- end
+ def self.via_options
+ %w(sendmail smtp)
+ end
- def self.transport_via_sendmail(tmail, options={})
- IO.popen('-', 'w+') do |pipe|
- if pipe
- pipe.write(tmail.to_s)
- else
- exec(sendmail_binary, *tmail.to)
- end
- end
- end
+ def self.transport_via_sendmail(tmail, options={})
+ IO.popen('-', 'w+') do |pipe|
+ if pipe
+ pipe.write(tmail.to_s)
+ else
+ exec(sendmail_binary, *tmail.to)
+ end
+ end
+ end
- def self.transport_via_smtp(tmail, options={:smtp => {}})
- default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
- o = default_options[:smtp].merge(options[:smtp])
- smtp = Net::SMTP.new(o[:host], o[:port])
- if o.include?(:auth)
- smtp.start(o[:domain], o[:user], o[:password], o[:auth])
- else
- smtp.start(o[:domain])
- end
- smtp.send_message tmail.to_s, tmail.from, tmail.to
- smtp.finish
- end
+ def self.transport_via_smtp(tmail, options={:smtp => {}})
+ default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
+ o = default_options[:smtp].merge(options[:smtp])
+ smtp = Net::SMTP.new(o[:host], o[:port])
+ if o.include?(:auth)
+ smtp.start(o[:domain], o[:user], o[:password], o[:auth])
+ else
+ smtp.start(o[:domain])
+ end
+ smtp.send_message tmail.to_s, tmail.from, tmail.to
+ smtp.finish
+ end
end
View
234 spec/pony_spec.rb
@@ -1,122 +1,122 @@
require File.dirname(__FILE__) + '/base'
describe Pony do
- it "sends mail" do
- Pony.should_receive(:transport) do |tmail|
- tmail.to.should == [ 'joe@example.com' ]
- tmail.from.should == [ 'sender@example.com' ]
- tmail.subject.should == 'hi'
- tmail.body.should == 'Hello, Joe.'
- end
- Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
- end
-
- it "requires :to param" do
- Pony.stub!(:transport)
- lambda { Pony.mail({}) }.should raise_error(ArgumentError)
- end
-
- it "doesn't require any other param" do
- Pony.stub!(:transport)
- lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
- end
-
- ####################
-
- describe "builds a TMail object with field:" do
- it "to" do
- Pony.build_tmail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
- end
-
- it "from" do
- Pony.build_tmail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
- end
-
- it "from (default)" do
- Pony.build_tmail({}).from.should == [ 'pony@unknown' ]
- end
-
- it "subject" do
- Pony.build_tmail(:subject => 'hello').subject.should == 'hello'
- end
-
- it "body" do
- Pony.build_tmail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
- end
- end
-
- describe "transport" do
- it "transports via the sendmail binary if it exists" do
- File.stub!(:executable?).and_return(true)
- Pony.should_receive(:transport_via_sendmail).with(:tmail)
- Pony.transport(:tmail)
- end
-
- it "transports via smtp if no sendmail binary" do
- Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
- Pony.should_receive(:transport_via_smtp).with(:tmail)
- Pony.transport(:tmail)
- end
-
- it "transports mail via /usr/sbin/sendmail binary" do
- pipe = mock('sendmail pipe')
- IO.should_receive(:popen).with('-',"w+").and_yield(pipe)
- pipe.should_receive(:write).with('message')
- Pony.transport_via_sendmail(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
- end
-
- describe "SMTP transport" do
- before do
- @smtp = mock('net::smtp object')
- @smtp.stub!(:start)
- @smtp.stub!(:send_message)
- @smtp.stub!(:finish)
- Net::SMTP.stub!(:new).and_return(@smtp)
- end
-
- it "defaults to localhost as the SMTP server" do
- Net::SMTP.should_receive(:new).with('localhost', '25').and_return(@smtp)
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
- end
-
- it "uses SMTP authorization when auth key is provided" do
- o = { :smtp => { :user => 'user', :password => 'password', :auth => 'plain'}}
- @smtp.should_receive(:start).with('localhost.localdomain', 'user', 'password', 'plain')
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'), o)
- end
-
- it "starts the job" do
- @smtp.should_receive(:start)
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
- end
-
- it "sends a tmail message" do
- @smtp.should_receive(:send_message)
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
- end
-
- it "finishes the job" do
- @smtp.should_receive(:finish)
- Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
- end
-
- end
- end
-
- describe ":via option should over-ride the default transport mechanism" do
- it "should send via sendmail if :via => sendmail" do
- Pony.should_receive(:transport_via_sendmail)
- Pony.mail(:to => 'joe@example.com', :via => :sendmail)
- end
-
- it "should send via smtp if :via => smtp" do
- Pony.should_receive(:transport_via_smtp)
- Pony.mail(:to => 'joe@example.com', :via => :smtp)
- end
-
- it "should raise an error if via is neither smtp nor sendmail" do
- lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
- end
- end
+ it "sends mail" do
+ Pony.should_receive(:transport) do |tmail|
+ tmail.to.should == [ 'joe@example.com' ]
+ tmail.from.should == [ 'sender@example.com' ]
+ tmail.subject.should == 'hi'
+ tmail.body.should == 'Hello, Joe.'
+ end
+ Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
+ end
+
+ it "requires :to param" do
+ Pony.stub!(:transport)
+ lambda { Pony.mail({}) }.should raise_error(ArgumentError)
+ end
+
+ it "doesn't require any other param" do
+ Pony.stub!(:transport)
+ lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
+ end
+
+ ####################
+
+ describe "builds a TMail object with field:" do
+ it "to" do
+ Pony.build_tmail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
+ end
+
+ it "from" do
+ Pony.build_tmail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
+ end
+
+ it "from (default)" do
+ Pony.build_tmail({}).from.should == [ 'pony@unknown' ]
+ end
+
+ it "subject" do
+ Pony.build_tmail(:subject => 'hello').subject.should == 'hello'
+ end
+
+ it "body" do
+ Pony.build_tmail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
+ end
+ end
+
+ describe "transport" do
+ it "transports via the sendmail binary if it exists" do
+ File.stub!(:executable?).and_return(true)
+ Pony.should_receive(:transport_via_sendmail).with(:tmail)
+ Pony.transport(:tmail)
+ end
+
+ it "transports via smtp if no sendmail binary" do
+ Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
+ Pony.should_receive(:transport_via_smtp).with(:tmail)
+ Pony.transport(:tmail)
+ end
+
+ it "transports mail via /usr/sbin/sendmail binary" do
+ pipe = mock('sendmail pipe')
+ IO.should_receive(:popen).with('-',"w+").and_yield(pipe)
+ pipe.should_receive(:write).with('message')
+ Pony.transport_via_sendmail(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
+ end
+
+ describe "SMTP transport" do
+ before do
+ @smtp = mock('net::smtp object')
+ @smtp.stub!(:start)
+ @smtp.stub!(:send_message)
+ @smtp.stub!(:finish)
+ Net::SMTP.stub!(:new).and_return(@smtp)
+ end
+
+ it "defaults to localhost as the SMTP server" do
+ Net::SMTP.should_receive(:new).with('localhost', '25').and_return(@smtp)
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
+ end
+
+ it "uses SMTP authorization when auth key is provided" do
+ o = { :smtp => { :user => 'user', :password => 'password', :auth => 'plain'}}
+ @smtp.should_receive(:start).with('localhost.localdomain', 'user', 'password', 'plain')
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'), o)
+ end
+
+ it "starts the job" do
+ @smtp.should_receive(:start)
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
+ end
+
+ it "sends a tmail message" do
+ @smtp.should_receive(:send_message)
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
+ end
+
+ it "finishes the job" do
+ @smtp.should_receive(:finish)
+ Pony.transport_via_smtp(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
+ end
+
+ end
+ end
+
+ describe ":via option should over-ride the default transport mechanism" do
+ it "should send via sendmail if :via => sendmail" do
+ Pony.should_receive(:transport_via_sendmail)
+ Pony.mail(:to => 'joe@example.com', :via => :sendmail)
+ end
+
+ it "should send via smtp if :via => smtp" do
+ Pony.should_receive(:transport_via_smtp)
+ Pony.mail(:to => 'joe@example.com', :via => :smtp)
+ end
+
+ it "should raise an error if via is neither smtp nor sendmail" do
+ lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
+ end
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.