command_line_email 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -2,10 +2,6 @@
2
2
 
3
3
  Send email from the Linux or Mac command line. Attach specific file(s) or all files in a directory. Specify email body as CLI string or load from a file.
4
4
 
5
- ## WARNING - NOT YET FULLY IMPLEMENTED
6
-
7
- This program still needs some work before I finish gemifying it. I have not yet created the bin/ directory file and currently run this by calling lib/command_line_email.rb directly.
8
-
9
5
  ## Installation
10
6
 
11
7
  Install command_line_email as:
@@ -26,13 +22,13 @@ This gem is useful for anyone who wishes to send email from a server using only
26
22
 
27
23
  ## Usage
28
24
 
29
- Once set up (see "Configuration File" below), you can send an email using various options (see "Command Line Options" below). Here's one example:
25
+ Once set up (see "Configuration File" below), you can send an email using "clemail" plus various options (see "Command Line Options" below). Here's one example:
30
26
 
31
- ruby lib/command_line_email.rb -t "me" -t "other_me" -s "Test subject line" -b "My body text"
27
+ clemail -t "me@myaddress.com" -t "my_friend@her_address.com" -s "Subject line text" -b "First body paragraph" -b "Second body paragraph"
32
28
 
33
29
  ### Configuration File
34
30
 
35
- The first time you run command_line_email, it will create a YAML file at ~/.command_line_email.yml. You edit this file to set your email configuration and, optionally, create mailing lists. A configuration file looks something like this:
31
+ The first time you run command_line_email, it will create a YAML file at ~/.command_line_email.yml (NOT YET IMPLEMENTED). You edit this file to set your email configuration and, optionally, create mailing lists. A configuration file looks something like this:
36
32
 
37
33
  ---
38
34
  :connection:
@@ -69,7 +65,7 @@ The following command line options are available:
69
65
 
70
66
  -c (--cc) takes the same values as -t/--to and sets the email's "carbon copy" recipients
71
67
 
72
- -f (--file) takes a string representing the path to a file you wish to attach to the email. This flag can be used multiple times, each with a different file.
68
+ -F (--file) takes a string representing the path to a file you wish to attach to the email. This flag can be used multiple times, each with a different file.
73
69
 
74
70
  -d (--directory) takes a string with a directory path. If provided with the -f/--file option, it is presumed that all files are in the directory specified by the -d/--directory option. If no -f/--file option is present, ALL files within the -d/--directory directory will be attached.
75
71
 
@@ -13,6 +13,8 @@ Gem::Specification.new do |gem|
13
13
  gem.homepage = "https://github.com/JamesLavin/command_line_email"
14
14
 
15
15
  gem.files = `git ls-files`.split($/)
16
+ gem.add_development_dependency 'rspec'
17
+ gem.add_development_dependency 'fakefs'
16
18
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
19
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
20
  gem.require_paths = ["lib"]
@@ -0,0 +1,65 @@
1
+ module CommandLineEmail
2
+
3
+ class CleOptparse
4
+
5
+ def self.parse(args, user_config)
6
+
7
+ mail_attrs = {to: []}
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+
11
+ opts.on("-t TO","--to TO") do |to|
12
+ mail_attrs[:to] = to_string_to_mailing_list(user_config, to, mail_attrs[:to])
13
+ end
14
+
15
+ opts.on("-f FROM","--from FROM") do |from|
16
+ mail_attrs[:from] = from
17
+ end
18
+
19
+ opts.on("-b BODY","--body BODY") do |body|
20
+ mail_attrs[:body] ||= ""
21
+ mail_attrs[:body] << body + "\n"
22
+ end
23
+
24
+ opts.on("-s SUBJECT","--subject SUBJECT") do |subject|
25
+ mail_attrs[:subject] = subject
26
+ end
27
+
28
+ opts.on("-c CC","--cc CC") do |cc|
29
+ if user_config.mailing_lists.has_key? cc.to_sym
30
+ mail_attrs[:cc] = user_config.mailing_lists[cc.to_sym]
31
+ else
32
+ mail_attrs[:cc] = cc
33
+ end
34
+ end
35
+
36
+ opts.on("-F FILE","--file FILE") do |file|
37
+ mail_attrs[:files] ||= []
38
+ mail_attrs[:files] << file
39
+ end
40
+
41
+ opts.on("-d DIRECTORY","--directory DIRECTORY") do |directory|
42
+ mail_attrs[:directory] = directory
43
+ end
44
+
45
+ end
46
+
47
+ option_parser.parse!(args)
48
+
49
+ mail_attrs
50
+
51
+ end # parse()
52
+
53
+ def self.to_string_to_mailing_list(user_config, to, list=[])
54
+ # takes optional list in case you want to add to an existing list
55
+ if user_config.mailing_lists.has_key? to.to_sym
56
+ Array(user_config.mailing_lists[to.to_sym]).each { |address| list << address }
57
+ else
58
+ list << to
59
+ end
60
+ list
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -3,99 +3,18 @@
3
3
  require 'mail'
4
4
  require 'yaml'
5
5
  require 'optparse'
6
- require 'send_mail_setup'
6
+ require 'send_email_setup'
7
+ require 'cle_optparse'
8
+ require 'deliver_email'
7
9
 
8
- user_config = CommandLineEmail::SendMailSetup.new
10
+ user_config = CommandLineEmail::SendEmailSetup.new
9
11
 
10
12
  Mail.defaults do
11
13
  delivery_method :smtp, user_config.mail_options
12
14
  end
13
15
 
14
- class CleOptparse
15
-
16
- def self.parse(args, user_config)
17
-
18
- mail_attrs = {to: []}
19
-
20
- option_parser = OptionParser.new do |opts|
21
-
22
- opts.on("-t TO","--to TO") do |to|
23
- mail_attrs[:to] = to_string_to_mailing_list(user_config, to, mail_attrs[:to])
24
- end
25
-
26
- opts.on("-f FROM","--from FROM") do |from|
27
- mail_attrs[:from] = from
28
- end
29
-
30
- opts.on("-b BODY","--body BODY") do |body|
31
- mail_attrs[:body] ||= ""
32
- mail_attrs[:body] << body + "\n"
33
- end
34
-
35
- opts.on("-s SUBJECT","--subject SUBJECT") do |subject|
36
- mail_attrs[:subject] = subject
37
- end
38
-
39
- opts.on("-c CC","--cc CC") do |cc|
40
- if user_config.mailing_lists.has_key? cc.to_sym
41
- mail_attrs[:cc] = user_config.mailing_lists[cc.to_sym]
42
- else
43
- mail_attrs[:cc] = cc
44
- end
45
- end
46
-
47
- opts.on("-f FILE","--add-file FILE") do |file|
48
- mail_attrs[:files] ||= []
49
- mail_attrs[:files] << file
50
- end
51
-
52
- opts.on("-d DIRECTORY","--directory DIRECTORY") do |directory|
53
- mail_attrs[:directory] = directory
54
- end
55
-
56
- end
57
-
58
- option_parser.parse!(args)
59
-
60
- mail_attrs
61
-
62
- end # parse()
63
-
64
- def self.to_string_to_mailing_list(user_config, to, list=[])
65
- # takes optional list in case you want to add to an existing list
66
- if user_config.mailing_lists.has_key? to.to_sym
67
- Array(user_config.mailing_lists[to.to_sym]).each { |address| list << address }
68
- else
69
- list << to
70
- end
71
- list
72
- end
73
-
74
- end # class CleOptparse
75
-
76
- mail_attrs = CleOptparse.parse(ARGV, user_config)
16
+ mail_attrs = CommandLineEmail::CleOptparse.parse(ARGV, user_config)
77
17
 
78
18
  puts mail_attrs
79
19
 
80
- def grab_text_from_filename_if_file_exists(filename_or_text)
81
- if File.exists?(filename_or_text)
82
- File.open(filename_or_text,'r').read
83
- else
84
- filename_or_text
85
- end
86
- end
87
-
88
- mail = Mail.new do
89
- from mail_attrs[:from] || user_config.defaults[:from]
90
- to mail_attrs[:to] || user_config.defaults[:to]
91
- cc mail_attrs[:cc] || user_config.defaults[:cc] || nil
92
- subject mail_attrs[:subject] || user_config.defaults[:subject] || ''
93
- body mail_attrs[:body] ? grab_text_from_filename_if_file_exists(mail_attrs[:body]) : ''
94
- if mail_attrs[:files]
95
- attach_selected(mail_attrs[:files], mail_attrs[:directory] || '')
96
- elsif mail_attrs[:directory]
97
- attach_all_from_directory(mail_attrs[:directory])
98
- end
99
- end
100
-
101
- mail.deliver!
20
+ CommandLineEmail::DeliverEmail.deliver(user_config, mail_attrs)
@@ -0,0 +1,36 @@
1
+ module CommandLineEmail
2
+
3
+ class DeliverEmail
4
+
5
+ def self.deliver(user_config, mail_attrs)
6
+
7
+ mail = Mail.new do
8
+ from mail_attrs[:from] || user_config.defaults[:from]
9
+ to mail_attrs[:to] || user_config.defaults[:to]
10
+ cc mail_attrs[:cc] || user_config.defaults[:cc] || nil
11
+ subject mail_attrs[:subject] || user_config.defaults[:subject] || ''
12
+ body mail_attrs[:body] ? grab_text_from_filename_if_file_exists(mail_attrs[:body]) : ''
13
+ if mail_attrs[:files]
14
+ attach_selected(mail_attrs[:files], mail_attrs[:directory] || '')
15
+ elsif mail_attrs[:directory]
16
+ attach_all_from_directory(mail_attrs[:directory])
17
+ end
18
+ end
19
+
20
+ mail.deliver!
21
+
22
+ end
23
+
24
+ private
25
+
26
+ def self.grab_text_from_filename_if_file_exists(filename_or_text)
27
+ if File.exists?(filename_or_text)
28
+ File.open(filename_or_text,'r').read
29
+ else
30
+ filename_or_text
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module CommandLineEmail
4
4
 
5
- class SendMailSetup
5
+ class SendEmailSetup
6
6
 
7
7
  class ConfigFileNotFound < StandardError
8
8
  def initialize(msg)
@@ -14,7 +14,8 @@ module CommandLineEmail
14
14
  attr_reader :mail_options, :mail_config_file
15
15
 
16
16
  def initialize(config_file = nil)
17
- @mail_config_file = config_file || File.expand_path('~/.command_line_email.yml')
17
+ set_mail_config_file(config_file)
18
+ ensure_config_file_exists
18
19
  set_mail_options
19
20
  end
20
21
 
@@ -28,14 +29,26 @@ module CommandLineEmail
28
29
 
29
30
  private
30
31
 
32
+ def ensure_config_file_exists
33
+ # nil to use default msg; [] to avoid printing ugly backtrace
34
+ raise(ConfigFileNotFound, nil, []) unless config_file_exists?
35
+ end
36
+
37
+ def set_mail_config_file(config_file)
38
+ config_file ||= '~/.command_line_email.yml'
39
+ @mail_config_file = File.expand_path(config_file)
40
+ end
41
+
42
+ def config_file_exists?
43
+ File.exists? mail_config_file
44
+ end
45
+
31
46
  def mail_config
32
- @mail_config ||= load_mail_config_file(mail_config_file)
47
+ @mail_config ||= load_mail_config(mail_config_file)
33
48
  end
34
49
 
35
- def load_mail_config_file(mail_config_file)
50
+ def load_mail_config(mail_config_file)
36
51
  YAML::load(File.open(mail_config_file))
37
- rescue
38
- raise ConfigFileNotFound, nil, [] # turn off ugly backtrace
39
52
  end
40
53
 
41
54
  def set_mail_options
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module CommandLineEmail
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
@@ -4,6 +4,7 @@ $LOAD_PATH.unshift("#{File.expand_path(File.dirname(__FILE__))}/command_line_ema
4
4
 
5
5
  require "mail"
6
6
  require "mail/message"
7
+ require "extruby/mail/message"
7
8
  require "command_line_email/version"
8
9
  require "command_line_email/send_mail_setup"
9
10
  require "command_line_email/command_line_email"
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+ require "cle_optparse"
3
+
4
+ describe "cle_optparse" do
5
+
6
+ include FakeFS::SpecHelpers
7
+
8
+ end
@@ -0,0 +1,39 @@
1
+ require "spec_helper"
2
+
3
+ describe "calling command_line_email" do
4
+
5
+ before do
6
+ $stdout = StringIO.new # suppress command line output
7
+ end
8
+
9
+ context "when config file not present" do
10
+
11
+ before do
12
+ CommandLineEmail::SendEmailSetup.any_instance.stub(:config_file_exists?).and_return(false)
13
+ end
14
+
15
+ it "should raise ConfigFileNotFound" do
16
+ expect { require "command_line_email" }.to raise_error(CommandLineEmail::SendEmailSetup::ConfigFileNotFound)
17
+ end
18
+
19
+ end
20
+
21
+ context "when config file present" do
22
+
23
+ before do
24
+ CommandLineEmail::SendEmailSetup.any_instance.stub(:config_file_exists?).and_return(true)
25
+ end
26
+
27
+ it "should not raise ConfigFileNotFound" do
28
+ expect { require "command_line_email" }.to_not raise_error(CommandLineEmail::SendEmailSetup::ConfigFileNotFound)
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ describe "other stuff" do
36
+
37
+ include FakeFS::SpecHelpers
38
+
39
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+ require "deliver_email"
3
+
4
+ describe "deliver_email" do
5
+
6
+ include FakeFS::SpecHelpers
7
+
8
+ end
@@ -0,0 +1,8 @@
1
+ require "spec_helper"
2
+ require "send_email_setup"
3
+
4
+ describe "send_email_setup" do
5
+
6
+ include FakeFS::SpecHelpers
7
+
8
+ end
@@ -0,0 +1,6 @@
1
+ require 'rspec'
2
+ require 'fakefs/spec_helpers'
3
+
4
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
5
+ $: << File.join(APP_ROOT, 'lib/command_line_email')
6
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: command_line_email
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,40 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-25 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-12-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: fakefs
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
14
46
  description: Send email from the Linux or Mac command line
15
47
  email:
16
48
  - command_line_email@futureresearch.com
@@ -27,10 +59,17 @@ files:
27
59
  - bin/clemail
28
60
  - command_line_email.gemspec
29
61
  - lib/command_line_email.rb
62
+ - lib/command_line_email/cle_optparse.rb
30
63
  - lib/command_line_email/command_line_email.rb
31
- - lib/command_line_email/send_mail_setup.rb
64
+ - lib/command_line_email/deliver_email.rb
65
+ - lib/command_line_email/send_email_setup.rb
32
66
  - lib/command_line_email/version.rb
33
67
  - lib/extruby/mail/message.rb
68
+ - spec/command_line_email/cle_optparse_spec.rb
69
+ - spec/command_line_email/command_line_email_spec.rb
70
+ - spec/command_line_email/deliver_email_spec.rb
71
+ - spec/command_line_email/send_email_setup_spec.rb
72
+ - spec/spec_helper.rb
34
73
  homepage: https://github.com/JamesLavin/command_line_email
35
74
  licenses: []
36
75
  post_install_message:
@@ -56,4 +95,10 @@ signing_key:
56
95
  specification_version: 3
57
96
  summary: Send email from the Linux or Mac command line. Can attach specific files
58
97
  or all files in a directory. Can load email body from a file.
59
- test_files: []
98
+ test_files:
99
+ - spec/command_line_email/cle_optparse_spec.rb
100
+ - spec/command_line_email/command_line_email_spec.rb
101
+ - spec/command_line_email/deliver_email_spec.rb
102
+ - spec/command_line_email/send_email_setup_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: