notes-structured-text-strip-bodies 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Trampoline Systems Ltd
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = notes-structured-text-strip-bodies
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to notes-structured-text-strip-bodies
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
9
+ * Fork the project
10
+ * Start a feature/bugfix branch
11
+ * Commit and push until you are happy with your contribution
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2011 Trampoline Systems Ltd. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |gem|
6
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
7
+ gem.name = "notes-structured-text-strip-bodies"
8
+ gem.homepage = "http://github.com/trampoline/notes-structured-text-strip-bodies"
9
+ gem.license = "MIT"
10
+ gem.summary = %Q{strip message bodies from Lotus Notes Structured Text exports}
11
+ gem.description = %Q{a command-line utility for removing message body text from Lotus Notes Structured Text export files}
12
+ gem.email = "craig@trampolinesystems.com"
13
+ gem.authors = ["mccraigmccraig"]
14
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
15
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
16
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
17
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
18
+ gem.add_development_dependency "rspec", "~> 1.3.0"
19
+ gem.add_development_dependency "jeweler", "~> 1.5.2"
20
+ gem.add_development_dependency "rcov", ">= 0"
21
+ end
22
+ Jeweler::RubygemsDotOrgTasks.new
23
+
24
+ require 'spec/rake/spectask'
25
+ Spec::Rake::SpecTask.new(:spec) do |spec|
26
+ spec.libs << 'lib' << 'spec'
27
+ spec.spec_files = FileList['spec/**/*_spec.rb']
28
+ end
29
+
30
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
31
+ spec.libs << 'lib' << 'spec'
32
+ spec.pattern = 'spec/**/*_spec.rb'
33
+ spec.rcov = true
34
+ end
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "notes-structured-text-strip-bodies #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path("../../lib", __FILE__)
3
+ require 'notes_structured_text_strip_bodies'
4
+ require 'optparse'
5
+ require 'logger'
6
+
7
+ options={}
8
+
9
+ USAGE = "Usage: #{File.basename(__FILE__)} <output_dir> <input_files> [<input_file>]* "
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage: #{File.basename(__FILE__)} <output_dir> <input_files> [<input_file>]* "
13
+
14
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
15
+ options[:verbose] = v
16
+ end
17
+ end.parse!
18
+
19
+ raise USAGE if ARGV.length<2
20
+
21
+ output_dir = ARGV[0]
22
+ input_files = ARGV[1..-1]
23
+ raise "<output_dir>: #{output_dir} must be a directory\n#{USAGE}" if !File.directory?(output_dir)
24
+
25
+ NotesStructuredTextStripBodies.logger = Logger.new($stderr)
26
+ if options[:verbose]
27
+ NotesStructuredTextStripBodies.logger.level=Logger::DEBUG
28
+ else
29
+ NotesStructuredTextStripBodies.logger.level=Logger::INFO
30
+ end
31
+ NotesStructuredTextStripBodies.strip_files(output_dir, input_files, options)
@@ -0,0 +1,50 @@
1
+ module NotesStructuredTextStripBodies
2
+ class << self
3
+ attr_accessor :logger
4
+ end
5
+
6
+ module_function
7
+
8
+ def log
9
+ yield logger if logger
10
+ end
11
+
12
+ def strip_files(output_dir, input_files, options={})
13
+ log{|logger| logger.info("stripping to output directory: #{output_dir}")}
14
+ input_files = [*input_files]
15
+ input_files.each do |input_file|
16
+ strip_file(output_dir, input_file)
17
+ end
18
+ end
19
+
20
+ def strip_file(output_dir, input_file, options={})
21
+ output_file = File.join(output_dir, File.basename(input_file))
22
+ raise "<input_file>: #{input_file} does not exist or is not a regular file" if !File.file?(input_file)
23
+ File.open(input_file, "r") do |input|
24
+ File.open(output_file, "w") do |output|
25
+ log{|logger| logger.debug("stripping: #{input_file} => #{output_file}")}
26
+ strip(output, input)
27
+ end
28
+ end
29
+ end
30
+
31
+ def read_block(input)
32
+ return nil if input.eof?
33
+ block = []
34
+ begin
35
+ l = input.readline.chomp
36
+ block << l if l.length>0
37
+ end while !input.eof? && l != ""
38
+ block
39
+ end
40
+
41
+ def is_header_block?(block)
42
+ !!block.find{|l| l =~ /^\$MessageID: /}
43
+ end
44
+
45
+ def strip(output, input)
46
+ while block=read_block(input)
47
+ block.each{|l| output << l << "\n"} if is_header_block?(block)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,113 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'stringio'
3
+
4
+ describe NotesStructuredTextStripBodies do
5
+ describe "strip_files" do
6
+ it "should call strip_file once per input_file with one input file" do
7
+ output_dir = Object.new
8
+ input_file = Object.new
9
+ mock(NotesStructuredTextStripBodies).strip_file(output_dir, input_file)
10
+ NotesStructuredTextStripBodies.strip_files(output_dir, input_file)
11
+ end
12
+
13
+ it "should call strip_file once per input_file with multiple input files" do
14
+ output_dir = Object.new
15
+ input_files = [Object.new, Object.new]
16
+ mock(NotesStructuredTextStripBodies).strip_file(output_dir, input_files[0])
17
+ mock(NotesStructuredTextStripBodies).strip_file(output_dir, input_files[1])
18
+ NotesStructuredTextStripBodies.strip_files(output_dir, input_files)
19
+ end
20
+ end
21
+
22
+ describe "strip_file" do
23
+ it "should open a new output file for writing, the input file for reading and call strip" do
24
+ output_dir = "/foo/bar"
25
+ input_file = "baz/boo.txt"
26
+
27
+ logdev = Object.new
28
+ stub(NotesStructuredTextStripBodies).logdev{logdev}
29
+ logger = Object.new
30
+ stub(NotesStructuredTextStripBodies).logger{logger}
31
+
32
+ output_stream = Object.new
33
+ mock(File).open("/foo/bar/boo.txt", "w") do |f, mode, block|
34
+ block.call(output_stream)
35
+ end
36
+
37
+ input_stream = Object.new
38
+ mock(File).open("baz/boo.txt", "r") do |f, mode, block|
39
+ block.call(input_stream)
40
+ end
41
+
42
+ mock(logger).debug(anything){|msg|
43
+ msg.should =~ %r{/foo/bar/boo.txt}
44
+ msg.should =~ %r{baz/boo.txt}
45
+ }
46
+ mock(NotesStructuredTextStripBodies).strip(output_stream, input_stream)
47
+
48
+ NotesStructuredTextStripBodies.strip_file(output_dir, input_file)
49
+ end
50
+ end
51
+
52
+ describe "readblock" do
53
+ it "should return lines read from a stream until the first empty line" do
54
+ input = <<-EOF
55
+ foo
56
+ bar
57
+
58
+ baz
59
+ boo
60
+ EOF
61
+ io = StringIO.new(input)
62
+ NotesStructuredTextStripBodies.read_block(io).should == ["foo", "bar"]
63
+ NotesStructuredTextStripBodies.read_block(io).should == ["baz", "boo"]
64
+ end
65
+
66
+ it "should return nil if the input stream is at EOF" do
67
+ io = StringIO.new("foo\nbar")
68
+ NotesStructuredTextStripBodies.read_block(io).should == ["foo", "bar"]
69
+ NotesStructuredTextStripBodies.read_block(io).should == nil
70
+ end
71
+ end
72
+
73
+ describe "is_header_block?" do
74
+ it "should return true if the block contains a line which start with '$MessageID: ' " do
75
+ NotesStructuredTextStripBodies.is_header_block?( ["foo", "$MessageID: bar", "baz"] ).should == true
76
+ end
77
+
78
+ it "should return false if there are no lines starting with '$MessageID: ' in the block" do
79
+ NotesStructuredTextStripBodies.is_header_block?( ["foo", "bar", "baz"] ).should == false
80
+ end
81
+ end
82
+
83
+ describe "strip" do
84
+ it "should write each line of each header block to output" do
85
+ output = Object.new
86
+ input = Object.new
87
+
88
+ block1 = [Object.new, Object.new, Object.new]
89
+ block2 = [Object.new, Object.new, Object.new]
90
+ block3 = [Object.new, Object.new]
91
+
92
+ blocks = [block3, block2, block1]
93
+ stub(NotesStructuredTextStripBodies).read_block(input){
94
+ raise "no more blocks" if !blocks
95
+ blocks = nil if blocks.empty?
96
+ blocks.pop if blocks
97
+ }
98
+
99
+ mock(NotesStructuredTextStripBodies).is_header_block?(block1){true}
100
+ mock(NotesStructuredTextStripBodies).is_header_block?(block2){false}
101
+ mock(NotesStructuredTextStripBodies).is_header_block?(block3){true}
102
+
103
+ mock(output).<<(block1[0]){output}
104
+ mock(output).<<("\n").times(5){output}
105
+ mock(output).<<(block1[1]){output}
106
+ mock(output).<<(block1[2]){output}
107
+ mock(output).<<(block3[0]){output}
108
+ mock(output).<<(block3[1]){output}
109
+
110
+ NotesStructuredTextStripBodies.strip(output, input)
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rubygems'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'rr'
7
+ require 'notes_structured_text_strip_bodies'
8
+
9
+ Spec::Runner.configure do |config|
10
+ config.mock_with RR::Adapters::Rspec
11
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: notes-structured-text-strip-bodies
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - mccraigmccraig
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-18 00:00:00 +01:00
19
+ default_executable: notes_structured_text_strip_bodies
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 1
32
+ - 3
33
+ - 0
34
+ version: 1.3.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: jeweler
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 7
46
+ segments:
47
+ - 1
48
+ - 5
49
+ - 2
50
+ version: 1.5.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: rcov
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: a command-line utility for removing message body text from Lotus Notes Structured Text export files
68
+ email: craig@trampolinesystems.com
69
+ executables:
70
+ - notes_structured_text_strip_bodies
71
+ extensions: []
72
+
73
+ extra_rdoc_files:
74
+ - LICENSE.txt
75
+ - README.rdoc
76
+ files:
77
+ - .document
78
+ - .rspec
79
+ - LICENSE.txt
80
+ - README.rdoc
81
+ - Rakefile
82
+ - VERSION
83
+ - bin/notes_structured_text_strip_bodies
84
+ - lib/notes_structured_text_strip_bodies.rb
85
+ - spec/notes_structured_text_strip_bodies_spec.rb
86
+ - spec/spec_helper.rb
87
+ has_rdoc: true
88
+ homepage: http://github.com/trampoline/notes-structured-text-strip-bodies
89
+ licenses:
90
+ - MIT
91
+ post_install_message:
92
+ rdoc_options: []
93
+
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 0
104
+ version: "0"
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ requirements: []
115
+
116
+ rubyforge_project:
117
+ rubygems_version: 1.6.2
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: strip message bodies from Lotus Notes Structured Text exports
121
+ test_files:
122
+ - spec/notes_structured_text_strip_bodies_spec.rb
123
+ - spec/spec_helper.rb