pirj-pony 0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/Rakefile +54 -0
  2. data/lib/pony.rb +73 -0
  3. data/spec/base.rb +4 -0
  4. data/spec/pony_spec.rb +122 -0
  5. metadata +58 -0
@@ -0,0 +1,54 @@
1
+ require 'rake'
2
+
3
+
4
+ task :default => :spec
5
+
6
+ ######################################################
7
+
8
+ require 'rake'
9
+ require 'rake/testtask'
10
+ require 'rake/clean'
11
+ require 'rake/gempackagetask'
12
+ require 'fileutils'
13
+
14
+ version = "0.3"
15
+ name = "pony"
16
+
17
+ spec = Gem::Specification.new do |s|
18
+ s.name = 'pirj-pony'
19
+ s.version = version
20
+ s.summary = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
21
+ s.description = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
22
+ s.author = "Adam Wiggins"
23
+ s.email = "adam@heroku.com"
24
+ s.homepage = "http://github.com/adamwiggins/pony"
25
+ s.rubyforge_project = "pony"
26
+
27
+ s.platform = Gem::Platform::RUBY
28
+ s.has_rdoc = false
29
+
30
+ s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
31
+
32
+ s.require_path = "lib"
33
+ end
34
+
35
+ Rake::GemPackageTask.new(spec) do |p|
36
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
37
+ end
38
+
39
+ task :install => [ :package ] do
40
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
41
+ end
42
+
43
+ task :uninstall => [ :clean ] do
44
+ sh %{sudo gem uninstall #{name}}
45
+ end
46
+
47
+ Rake::TestTask.new do |t|
48
+ t.libs << "spec"
49
+ t.test_files = FileList['spec/*_spec.rb']
50
+ t.verbose = true
51
+ end
52
+
53
+ CLEAN.include [ 'pkg', '*.gem', '.config' ]
54
+
@@ -0,0 +1,73 @@
1
+ require 'rubygems'
2
+ require 'net/smtp'
3
+ begin
4
+ require 'tmail'
5
+ rescue LoadError
6
+ require 'actionmailer'
7
+ end
8
+
9
+ module Pony
10
+ def self.mail(options)
11
+ raise(ArgumentError, ":to is required") unless options[:to]
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), options)
19
+ else
20
+ raise(ArgumentError, ":via must be either smtp or sendmail")
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.build_tmail(options)
26
+ mail = TMail::Mail.new
27
+ mail.to = options[:to]
28
+ mail.from = options[:from] || 'pony@unknown'
29
+ mail.subject = options[:subject]
30
+ mail.body = options[:body] || ""
31
+ mail
32
+ end
33
+
34
+ def self.sendmail_binary
35
+ @sendmail_binary ||= `which sendmail`.chomp
36
+ end
37
+
38
+ def self.transport(tmail)
39
+ if File.executable? sendmail_binary
40
+ transport_via_sendmail(tmail)
41
+ else
42
+ transport_via_smtp(tmail)
43
+ end
44
+ end
45
+
46
+ def self.via_options
47
+ %w(sendmail smtp)
48
+ end
49
+
50
+ def self.transport_via_sendmail(tmail, options={})
51
+ IO.popen('-', 'w+') do |pipe|
52
+ if pipe
53
+ pipe.write(tmail.to_s)
54
+ else
55
+ exec(sendmail_binary, *tmail.to)
56
+ end
57
+ end
58
+ end
59
+
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
+ smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
65
+ if o.include?(:auth)
66
+ smtp.start(o[:domain], o[:user], o[:password], o[:auth])
67
+ else
68
+ smtp.start(o[:domain])
69
+ end
70
+ smtp.send_message tmail.to_s, tmail.from, tmail.to
71
+ smtp.finish
72
+ end
73
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/pony'
@@ -0,0 +1,122 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ describe Pony do
4
+ it "sends mail" do
5
+ Pony.should_receive(:transport) do |tmail|
6
+ tmail.to.should == [ 'joe@example.com' ]
7
+ tmail.from.should == [ 'sender@example.com' ]
8
+ tmail.subject.should == 'hi'
9
+ tmail.body.should == 'Hello, Joe.'
10
+ end
11
+ Pony.mail(:to => 'joe@example.com', :from => 'sender@example.com', :subject => 'hi', :body => 'Hello, Joe.')
12
+ end
13
+
14
+ it "requires :to param" do
15
+ Pony.stub!(:transport)
16
+ lambda { Pony.mail({}) }.should raise_error(ArgumentError)
17
+ end
18
+
19
+ it "doesn't require any other param" do
20
+ Pony.stub!(:transport)
21
+ lambda { Pony.mail(:to => 'joe@example.com') }.should_not raise_error
22
+ end
23
+
24
+ ####################
25
+
26
+ describe "builds a TMail object with field:" do
27
+ it "to" do
28
+ Pony.build_tmail(:to => 'joe@example.com').to.should == [ 'joe@example.com' ]
29
+ end
30
+
31
+ it "from" do
32
+ Pony.build_tmail(:from => 'joe@example.com').from.should == [ 'joe@example.com' ]
33
+ end
34
+
35
+ it "from (default)" do
36
+ Pony.build_tmail({}).from.should == [ 'pony@unknown' ]
37
+ end
38
+
39
+ it "subject" do
40
+ Pony.build_tmail(:subject => 'hello').subject.should == 'hello'
41
+ end
42
+
43
+ it "body" do
44
+ Pony.build_tmail(:body => 'What do you know, Joe?').body.should == 'What do you know, Joe?'
45
+ end
46
+ end
47
+
48
+ describe "transport" do
49
+ it "transports via the sendmail binary if it exists" do
50
+ File.stub!(:executable?).and_return(true)
51
+ Pony.should_receive(:transport_via_sendmail).with(:tmail)
52
+ Pony.transport(:tmail)
53
+ end
54
+
55
+ it "transports via smtp if no sendmail binary" do
56
+ Pony.stub!(:sendmail_binary).and_return('/does/not/exist')
57
+ Pony.should_receive(:transport_via_smtp).with(:tmail)
58
+ Pony.transport(:tmail)
59
+ end
60
+
61
+ it "transports mail via /usr/sbin/sendmail binary" do
62
+ pipe = mock('sendmail pipe')
63
+ IO.should_receive(:popen).with('-',"w+").and_yield(pipe)
64
+ pipe.should_receive(:write).with('message')
65
+ Pony.transport_via_sendmail(mock('tmail', :to => 'to', :from => 'from', :to_s => 'message'))
66
+ end
67
+
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
+
103
+ end
104
+ end
105
+
106
+ describe ":via option should over-ride the default transport mechanism" do
107
+ it "should send via sendmail if :via => sendmail" do
108
+ Pony.should_receive(:transport_via_sendmail)
109
+ Pony.mail(:to => 'joe@example.com', :via => :sendmail)
110
+ end
111
+
112
+ it "should send via smtp if :via => smtp" do
113
+ Pony.should_receive(:transport_via_smtp)
114
+ Pony.mail(:to => 'joe@example.com', :via => :smtp)
115
+ end
116
+
117
+ it "should raise an error if via is neither smtp nor sendmail" do
118
+ lambda { Pony.mail(:to => 'joe@plumber.com', :via => :pigeon) }.should raise_error(ArgumentError)
119
+ end
120
+ end
121
+
122
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pirj-pony
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Adam Wiggins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-27 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
17
+ email: adam@heroku.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - Rakefile
26
+ - lib/pony.rb
27
+ - spec/pony_spec.rb
28
+ - spec/base.rb
29
+ has_rdoc: true
30
+ homepage: http://github.com/adamwiggins/pony
31
+ licenses: []
32
+
33
+ post_install_message:
34
+ rdoc_options: []
35
+
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project: pony
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
57
+ test_files: []
58
+