Narnach-pony 0.3.1

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 +73 -0
  2. data/lib/pony.rb +77 -0
  3. data/spec/base.rb +4 -0
  4. data/spec/pony_spec.rb +122 -0
  5. metadata +77 -0
@@ -0,0 +1,73 @@
1
+ require 'rake'
2
+ require 'spec/rake/spectask'
3
+
4
+ desc "Run all specs"
5
+ Spec::Rake::SpecTask.new('spec') do |t|
6
+ t.spec_files = FileList['spec/*_spec.rb']
7
+ end
8
+
9
+ desc "Print specdocs"
10
+ Spec::Rake::SpecTask.new(:doc) do |t|
11
+ t.spec_opts = ["--format", "specdoc", "--dry-run"]
12
+ t.spec_files = FileList['spec/*_spec.rb']
13
+ end
14
+
15
+ desc "Run all examples with RCov"
16
+ Spec::Rake::SpecTask.new('rcov') do |t|
17
+ t.spec_files = FileList['spec/*_spec.rb']
18
+ t.rcov = true
19
+ t.rcov_opts = ['--exclude', 'examples']
20
+ end
21
+
22
+ task :default => :spec
23
+
24
+ ######################################################
25
+
26
+ require 'rake'
27
+ require 'rake/testtask'
28
+ require 'rake/clean'
29
+ require 'rake/gempackagetask'
30
+ require 'fileutils'
31
+
32
+ version = "0.3.1"
33
+ name = "Narnach-pony"
34
+
35
+ spec = Gem::Specification.new do |s|
36
+ s.name = name
37
+ s.version = version
38
+ s.summary = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
39
+ s.description = "My fork of Adam Wiggins' Pony project."
40
+ s.author = "Wes Oldenbeuving"
41
+ s.email = "wes@narnach.com"
42
+ s.homepage = "http://github.com/Narnach/pony"
43
+
44
+ s.platform = Gem::Platform::RUBY
45
+ s.has_rdoc = false
46
+
47
+ s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
48
+
49
+ s.require_path = "lib"
50
+ s.add_dependency( 'tmail', '~> 1.0' )
51
+ s.add_dependency( 'smtp_tls', '>= 1.0.3')
52
+ end
53
+
54
+ Rake::GemPackageTask.new(spec) do |p|
55
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
56
+ end
57
+
58
+ task :install => [ :package ] do
59
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
60
+ end
61
+
62
+ task :uninstall => [ :clean ] do
63
+ sh %{sudo gem uninstall #{name}}
64
+ end
65
+
66
+ Rake::TestTask.new do |t|
67
+ t.libs << "spec"
68
+ t.test_files = FileList['spec/*_spec.rb']
69
+ t.verbose = true
70
+ end
71
+
72
+ CLEAN.include [ 'pkg', '*.gem', '.config' ]
73
+
@@ -0,0 +1,77 @@
1
+ require 'rubygems'
2
+ require 'net/smtp'
3
+ begin
4
+ require 'tmail'
5
+ rescue LoadError
6
+ require 'actionmailer'
7
+ end
8
+ begin
9
+ require 'smtp_tls'
10
+ rescue LoadError
11
+ end
12
+
13
+ module Pony
14
+ def self.mail(options)
15
+ raise(ArgumentError, ":to is required") unless options[:to]
16
+
17
+ via = options.delete(:via)
18
+ if via.nil?
19
+ transport build_tmail(options)
20
+ else
21
+ if via_options.include?(via.to_s)
22
+ send("transport_via_#{via}", build_tmail(options), options)
23
+ else
24
+ raise(ArgumentError, ":via must be either smtp or sendmail")
25
+ end
26
+ end
27
+ end
28
+
29
+ def self.build_tmail(options)
30
+ mail = TMail::Mail.new
31
+ mail.to = options[:to]
32
+ mail.from = options[:from] || 'pony@unknown'
33
+ mail.subject = options[:subject]
34
+ mail.body = options[:body] || ""
35
+ mail
36
+ end
37
+
38
+ def self.sendmail_binary
39
+ @sendmail_binary ||= `which sendmail`.chomp
40
+ end
41
+
42
+ def self.transport(tmail)
43
+ if File.executable? sendmail_binary
44
+ transport_via_sendmail(tmail)
45
+ else
46
+ transport_via_smtp(tmail)
47
+ end
48
+ end
49
+
50
+ def self.via_options
51
+ %w(sendmail smtp)
52
+ end
53
+
54
+ def self.transport_via_sendmail(tmail, options={})
55
+ IO.popen('-', 'w+') do |pipe|
56
+ if pipe
57
+ pipe.write(tmail.to_s)
58
+ else
59
+ exec(sendmail_binary, *tmail.to)
60
+ end
61
+ end
62
+ end
63
+
64
+ def self.transport_via_smtp(tmail, options={:smtp => {}})
65
+ default_options = {:smtp => { :host => 'localhost', :port => '25', :domain => 'localhost.localdomain' }}
66
+ o = default_options[:smtp].merge(options[:smtp])
67
+ smtp = Net::SMTP.new(o[:host], o[:port])
68
+ if o.include?(:auth)
69
+ smtp.enable_starttls
70
+ smtp.start(o[:domain], o[:user], o[:password], o[:auth])
71
+ else
72
+ smtp.start(o[:domain])
73
+ end
74
+ smtp.send_message tmail.to_s, tmail.from, tmail.to
75
+ smtp.finish
76
+ end
77
+ 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,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: Narnach-pony
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.1
5
+ platform: ruby
6
+ authors:
7
+ - Wes Oldenbeuving
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-21 00:00:00 +02:00
13
+ default_executable:
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:
25
+ - !ruby/object:Gem::Dependency
26
+ name: smtp_tls
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.3
34
+ version:
35
+ description: My fork of Adam Wiggins' Pony project.
36
+ email: wes@narnach.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - Rakefile
45
+ - lib/pony.rb
46
+ - spec/base.rb
47
+ - spec/pony_spec.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/Narnach/pony
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.4
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
76
+ test_files: []
77
+