minimail 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/minimail.rb CHANGED
@@ -1,5 +1,5 @@
1
- require "minimail/version"
2
- require "minimail/mail"
1
+ require "lib/minimail/version"
2
+ require "lib/minimail/mail"
3
3
 
4
4
  module Minimail
5
- end
5
+ end
@@ -0,0 +1,70 @@
1
+ module Minimail
2
+ class Mail
3
+ attr_accessor :subject, :recipients, :body, :attachments
4
+
5
+ def initialize(params = {})
6
+ mail_program?
7
+
8
+ @recipients = params[:recipients] || []
9
+ @subject = params[:subject] || ""
10
+ @body = params[:body] || ""
11
+ @attachments = if encode_program? && params[:attachments]
12
+ process_attachments( [params[:attachments]].flatten )
13
+ end
14
+ end
15
+
16
+ def subject(subject = nil)
17
+ @subject = subject
18
+ end
19
+
20
+ def recipients(recipients = nil)
21
+ @recipients = recipients
22
+ end
23
+
24
+ def body(body = nil)
25
+ @body = body
26
+ end
27
+
28
+ def deliver
29
+ return unless valid?
30
+ if @attachments
31
+ IO.popen("echo #{@body} | (#{@attachments}) | #{mail_command} -s '#{@subject}' #{@recipients}")
32
+ else
33
+ IO.popen("echo #{@body} | #{mail_command} -s '#{@subject}' #{@recipients}")
34
+ end
35
+ end
36
+
37
+ def mail_command
38
+ return '/usr/bin/mail' if command?('/usr/bin/mail')
39
+ return '/bin/mail' if command?('/bin/mail')
40
+ end
41
+
42
+ def valid?
43
+ @recipients.empty? ? false : true
44
+ end
45
+
46
+ def draft(&block)
47
+ instance_eval(&block)
48
+ end
49
+
50
+ private
51
+ def process_attachments(files)
52
+ return unless encode_program?
53
+ return "Error reading /tmp dir" unless File.directory?('/tmp')
54
+ files.map{|f| "/usr/bin/uuencode #{f} /tmp/#{File.basename(f)}"}.join(';')
55
+ end
56
+
57
+ def encode_program?
58
+ command?('/usr/bin/uuencode')
59
+ end
60
+
61
+ def mail_program?
62
+ command?('/usr/bin/mail') || command?('/bin/mail') ? true : "No mail program"
63
+ end
64
+
65
+ def command?(command)
66
+ system("which #{command} > /dev/null 2>&1")
67
+ end
68
+
69
+ end
70
+ end
@@ -1,3 +1,3 @@
1
1
  module Minimail
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/minimail.gemspec CHANGED
@@ -1,6 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path("../lib", __FILE__)
3
- require "minimail/version"
2
+ require File.expand_path('../lib/minimail', __FILE__)
4
3
 
5
4
  Gem::Specification.new do |s|
6
5
  s.name = "minimail"
@@ -11,14 +10,11 @@ Gem::Specification.new do |s|
11
10
  s.summary = ""
12
11
  s.description = ""
13
12
 
14
- s.rubyforge_project = "minimail"
15
-
16
13
  s.files = `git ls-files`.split("\n")
17
14
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
15
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
16
  s.require_paths = ["lib"]
20
17
 
21
18
  # specify any dependencies here; for example:
22
- # s.add_development_dependency "rspec"
23
- # s.add_runtime_dependency "rest-client"
19
+ s.add_development_dependency "minitest"
24
20
  end
data/readme.md ADDED
@@ -0,0 +1,14 @@
1
+ require 'minimail'
2
+
3
+ # Send an email
4
+ Minimail::Mail.new(:subject => "check it!", :recipients => "bob@example.com").deliver
5
+
6
+ # DSL style
7
+ m = Minimail::Mail.new
8
+ m.draft do
9
+ subject "check it!"
10
+ recipients "bob@example.com"
11
+ end
12
+ m.deliver
13
+
14
+ # check tests for examples that encode attachments
@@ -0,0 +1 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
@@ -0,0 +1,63 @@
1
+ require 'rubygems'
2
+ require 'minitest/autorun'
3
+ require 'base64'
4
+
5
+ $:.unshift File.expand_path("../../lib", __FILE__)
6
+ require 'minimail'
7
+
8
+ class Minimail::MailTest < MiniTest::Unit::TestCase
9
+ def test_that_a_minimail_can_be_initialized_with_typical_email_attributes
10
+ subject = "Check this out!"
11
+ recipients = "jane@example.com"
12
+ body = "You want to see this"
13
+ mail = Minimail::Mail.new(:subject => subject, :recipients => recipients, :body => body)
14
+ assert_equal subject, mail.instance_variable_get(:@subject)
15
+ assert_equal recipients, mail.instance_variable_get(:@recipients)
16
+ assert_equal body, mail.instance_variable_get(:@body)
17
+ end
18
+
19
+ def test_can_prepare_simple_mail_with_multiple_recipients
20
+ end
21
+
22
+ def test_can_prepare_a_minimail_with_a_single_attachment
23
+ subject = "Check this out!"
24
+ recipients = "jane@example.com"
25
+ body = "You want to see this"
26
+ attachment_filename = File.join(Dir.pwd, 'test', 'fake_attachment.txt')
27
+ mail = Minimail::Mail.new(:subject => subject, :recipients => recipients, :body => body, :attachments => attachment_filename)
28
+ assert_equal "/usr/bin/uuencode #{Dir.pwd}/test/fake_attachment.txt /tmp/fake_attachment.txt", mail.attachments
29
+ end
30
+
31
+ def test_can_prepare_a_minimail_with_multiple_attachments
32
+ subject = "Check this out!"
33
+ recipients = "jane@example.com"
34
+ body = "You want to see this"
35
+ files = [File.join(Dir.pwd, 'test', 'fake_attachment.txt'), File.join(Dir.pwd, 'test', 'fake_attachment.txt')]
36
+ mail = Minimail::Mail.new(:subject => subject, :recipients => recipients, :body => body, :attachments => files)
37
+ assert_equal "/usr/bin/uuencode #{Dir.pwd}/test/fake_attachment.txt /tmp/fake_attachment.txt;/usr/bin/uuencode #{Dir.pwd}/test/fake_attachment.txt /tmp/fake_attachment.txt", mail.attachments
38
+ end
39
+
40
+ def test_can_deliver_a_mail
41
+ recipients = "jane@example.com"
42
+ mail = Minimail::Mail.new(:recipients => recipients)
43
+ assert_equal IO, mail.deliver.class
44
+ end
45
+
46
+ def test_ensure_mail_is_not_valid_unless_recipient_is_specified
47
+ mail = Minimail::Mail.new()
48
+ assert_nil mail.deliver
49
+ assert !mail.valid?
50
+ end
51
+
52
+ def test_can_create_a_minimail_with_dsl_style
53
+ m = Minimail::Mail.new
54
+ m.draft do
55
+ subject "check it!"
56
+ recipients "bob@example.com"
57
+ body "cool stuff"
58
+ end
59
+ assert_equal "check it!", m.instance_variable_get(:@subject)
60
+ assert_equal "bob@example.com", m.instance_variable_get(:@recipients)
61
+ assert_equal "cool stuff", m.instance_variable_get(:@body)
62
+ end
63
+ end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minimail
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
4
  prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 0
9
- - 1
10
- version: 0.0.1
8
+ - 2
9
+ version: 0.0.2
11
10
  platform: ruby
12
11
  authors:
13
12
  - Andy Atkinson
@@ -15,10 +14,21 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2012-02-29 00:00:00 -05:00
17
+ date: 2012-05-19 00:00:00 -04:00
19
18
  default_executable:
20
- dependencies: []
21
-
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: minitest
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
22
32
  description: ""
23
33
  email:
24
34
  - andy@webandy.com
@@ -33,8 +43,12 @@ files:
33
43
  - Gemfile
34
44
  - Rakefile
35
45
  - lib/minimail.rb
46
+ - lib/minimail/mail.rb
36
47
  - lib/minimail/version.rb
37
48
  - minimail.gemspec
49
+ - readme.md
50
+ - test/fake_attachment.txt
51
+ - test/minimail_test.rb
38
52
  has_rdoc: true
39
53
  homepage: ""
40
54
  licenses: []
@@ -45,29 +59,26 @@ rdoc_options: []
45
59
  require_paths:
46
60
  - lib
47
61
  required_ruby_version: !ruby/object:Gem::Requirement
48
- none: false
49
62
  requirements:
50
63
  - - ">="
51
64
  - !ruby/object:Gem::Version
52
- hash: 3
53
65
  segments:
54
66
  - 0
55
67
  version: "0"
56
68
  required_rubygems_version: !ruby/object:Gem::Requirement
57
- none: false
58
69
  requirements:
59
70
  - - ">="
60
71
  - !ruby/object:Gem::Version
61
- hash: 3
62
72
  segments:
63
73
  - 0
64
74
  version: "0"
65
75
  requirements: []
66
76
 
67
- rubyforge_project: minimail
68
- rubygems_version: 1.3.7
77
+ rubyforge_project:
78
+ rubygems_version: 1.3.6
69
79
  signing_key:
70
80
  specification_version: 3
71
81
  summary: ""
72
- test_files: []
73
-
82
+ test_files:
83
+ - test/fake_attachment.txt
84
+ - test/minimail_test.rb