pony 0.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 +72 -0
  2. data/lib/pony.rb +43 -0
  3. data/spec/base.rb +4 -0
  4. data/spec/pony_spec.rb +75 -0
  5. metadata +56 -0
@@ -0,0 +1,72 @@
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.1"
33
+ name = "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 = "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
40
+ s.author = "Adam Wiggins"
41
+ s.email = "adam@heroku.com"
42
+ s.homepage = "http://github.com/adamwiggins/pony"
43
+ s.rubyforge_project = "pony"
44
+
45
+ s.platform = Gem::Platform::RUBY
46
+ s.has_rdoc = false
47
+
48
+ s.files = %w(Rakefile) + Dir.glob("{lib,spec}/**/*")
49
+
50
+ s.require_path = "lib"
51
+ end
52
+
53
+ Rake::GemPackageTask.new(spec) do |p|
54
+ p.need_tar = true if RUBY_PLATFORM !~ /mswin/
55
+ end
56
+
57
+ task :install => [ :package ] do
58
+ sh %{sudo gem install pkg/#{name}-#{version}.gem}
59
+ end
60
+
61
+ task :uninstall => [ :clean ] do
62
+ sh %{sudo gem uninstall #{name}}
63
+ end
64
+
65
+ Rake::TestTask.new do |t|
66
+ t.libs << "spec"
67
+ t.test_files = FileList['spec/*_spec.rb']
68
+ t.verbose = true
69
+ end
70
+
71
+ CLEAN.include [ 'pkg', '*.gem', '.config' ]
72
+
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'net/smtp'
3
+ require 'tmail'
4
+
5
+ module Pony
6
+ def self.mail(options)
7
+ raise(ArgumentError, ":to is required") unless options[:to]
8
+ transport build_tmail(options)
9
+ end
10
+
11
+ def self.build_tmail(options)
12
+ mail = TMail::Mail.new
13
+ mail.to = options[:to]
14
+ mail.from = options[:from] || 'pony@unknown'
15
+ mail.subject = options[:subject]
16
+ mail.body = options[:body] || ""
17
+ mail
18
+ end
19
+
20
+ def self.sendmail_binary
21
+ "/usr/sbin/sendmail"
22
+ end
23
+
24
+ def self.transport(tmail)
25
+ if File.exists? sendmail_binary
26
+ transport_via_sendmail(tmail)
27
+ else
28
+ transport_via_smtp(tmail)
29
+ end
30
+ end
31
+
32
+ def self.transport_via_sendmail(tmail)
33
+ IO.popen("#{sendmail_binary} #{tmail.to}", "w") do |pipe|
34
+ pipe.write tmail.to_s
35
+ end
36
+ end
37
+
38
+ def self.transport_via_smtp(tmail)
39
+ Net::SMTP.start('localhost') do |smtp|
40
+ smtp.sendmail(tmail.to_s, tmail.from, tmail.to)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'spec'
3
+
4
+ require File.dirname(__FILE__) + '/../lib/pony'
@@ -0,0 +1,75 @@
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
+ Pony.stub!(:sendmail_binary).and_return(__FILE__)
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('/usr/sbin/sendmail to', '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
+ 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'))
73
+ end
74
+ end
75
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pony
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - Adam Wiggins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-10-29 00:00:00 -07: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/base.rb
28
+ - spec/pony_spec.rb
29
+ has_rdoc: false
30
+ homepage: http://github.com/adamwiggins/pony
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: "0"
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project: pony
51
+ rubygems_version: 1.2.0
52
+ signing_key:
53
+ specification_version: 2
54
+ summary: "Send email in one command: Pony.mail(:to => 'someone@example.com', :body => 'hello')"
55
+ test_files: []
56
+