From d85d89ae05d360ed8f5964cc4f4ae701c0e7fc9a Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 18:01:06 -0400 Subject: [PATCH 01/15] added gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1377554 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.swp From 054c8d4b076ab6cbb3d93a7c6a6c6a0c31902b28 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 18:04:25 -0400 Subject: [PATCH 02/15] first spec and gemfile --- Gemfile | 3 +++ spec/macbeth_analyzer_spec.rb | 5 +++++ 2 files changed, 8 insertions(+) create mode 100644 Gemfile create mode 100644 spec/macbeth_analyzer_spec.rb diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..e99640c --- /dev/null +++ b/Gemfile @@ -0,0 +1,3 @@ +source 'https://rubygems.org + +gem 'rspec' diff --git a/spec/macbeth_analyzer_spec.rb b/spec/macbeth_analyzer_spec.rb new file mode 100644 index 0000000..e8670ac --- /dev/null +++ b/spec/macbeth_analyzer_spec.rb @@ -0,0 +1,5 @@ +describe 'displays number of total lines spoken by characters in Macbeth' do + it 'prints the number with the name' do + pending 'test for rspec' + end +end From 0a0d0b0937eda18942be95b70aa801b9f8497d7c Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 22:05:06 -0400 Subject: [PATCH 03/15] initial spec --- lib/macbeth_analyzer.rb | 9 +++++++++ spec/macbeth_analyzer_spec.rb | 9 ++++++++- spec/spec_helper.rb | 23 +++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 lib/macbeth_analyzer.rb create mode 100644 spec/spec_helper.rb diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb new file mode 100644 index 0000000..117c08b --- /dev/null +++ b/lib/macbeth_analyzer.rb @@ -0,0 +1,9 @@ +class MacbethAnalyzer + def initialize(output) + @output = output + end + + def analyze + @output.puts "12 Macbeth\n4 Banquo\n3 Duncan" + end +end diff --git a/spec/macbeth_analyzer_spec.rb b/spec/macbeth_analyzer_spec.rb index e8670ac..b1674c0 100644 --- a/spec/macbeth_analyzer_spec.rb +++ b/spec/macbeth_analyzer_spec.rb @@ -1,5 +1,12 @@ +require 'spec_helper' +require_relative '../lib/macbeth_analyzer' + describe 'displays number of total lines spoken by characters in Macbeth' do it 'prints the number with the name' do - pending 'test for rspec' + output = StringIO.new + analyzer = MacbethAnalyzer.new(output) + analyzer.analyze + output.seek(0) + expect(output.read.chomp).to eq "12 Macbeth\n4 Banquo\n3 Duncan" end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..6b4fc65 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,23 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# Require this file using `require "spec_helper"` to ensure that it is only +# loaded once. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # Limit the spec run to only specs with the focus metadata. If no specs have + # the filtering metadata and `run_all_when_everything_filtered = true` then + # all specs will run. + #config.filter_run :focus + + # Run all specs when none match the provided filter. This works well in + # conjunction with `config.filter_run :focus`, as it will run the entire + # suite when no specs have `:filter` metadata. + #config.run_all_when_everything_filtered = true + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + #config.order = 'random' +end From b396efb4464699e0278f34e521207a89b17f791c Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 22:08:47 -0400 Subject: [PATCH 04/15] refactor to what it might actually look like --- lib/macbeth_analyzer.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index 117c08b..f754547 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -4,6 +4,8 @@ def initialize(output) end def analyze - @output.puts "12 Macbeth\n4 Banquo\n3 Duncan" + @output.puts "12 Macbeth" + @output.puts "4 Banquo" + @output.puts "3 Duncan" end end From 072d10f35a63239ca8364aa4fab9dc3716083b90 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 22:11:49 -0400 Subject: [PATCH 05/15] added .rspec to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1377554..84e21c9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.swp +*.rspec From 18f7e9602e1d3d5ac75aa8af66a5d3f15adfd782 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 22:12:04 -0400 Subject: [PATCH 06/15] removed the output type and pushed it into the analyzer --- lib/macbeth_analyzer.rb | 5 ++++- spec/macbeth_analyzer_spec.rb | 8 +++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index f754547..76f6645 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -1,5 +1,5 @@ class MacbethAnalyzer - def initialize(output) + def initialize(output = StringIO.new) @output = output end @@ -7,5 +7,8 @@ def analyze @output.puts "12 Macbeth" @output.puts "4 Banquo" @output.puts "3 Duncan" + + @output.seek(0) + @output.read.chomp end end diff --git a/spec/macbeth_analyzer_spec.rb b/spec/macbeth_analyzer_spec.rb index b1674c0..e3abc3b 100644 --- a/spec/macbeth_analyzer_spec.rb +++ b/spec/macbeth_analyzer_spec.rb @@ -3,10 +3,8 @@ describe 'displays number of total lines spoken by characters in Macbeth' do it 'prints the number with the name' do - output = StringIO.new - analyzer = MacbethAnalyzer.new(output) - analyzer.analyze - output.seek(0) - expect(output.read.chomp).to eq "12 Macbeth\n4 Banquo\n3 Duncan" + analyzer = MacbethAnalyzer.new + results = analyzer.analyze + expect(results).to eq "12 Macbeth\n4 Banquo\n3 Duncan" end end From a5d5a7695581f6f1c4a8090b4e2218e6ac20feaa Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Fri, 18 Apr 2014 22:32:27 -0400 Subject: [PATCH 07/15] refactored out the output to a class --- lib/macbeth_analyzer.rb | 13 ++++++------- lib/terminal_output.rb | 14 ++++++++++++++ spec/terminal_output_spec.rb | 20 ++++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) create mode 100644 lib/terminal_output.rb create mode 100644 spec/terminal_output_spec.rb diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index 76f6645..4688ba5 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -1,14 +1,13 @@ class MacbethAnalyzer - def initialize(output = StringIO.new) - @output = output + def initialize() + @output = TerminalOutput.new end def analyze - @output.puts "12 Macbeth" - @output.puts "4 Banquo" - @output.puts "3 Duncan" + @output.add_new_line "12 Macbeth" + @output.add_new_line "4 Banquo" + @output.add_new_line "3 Duncan" - @output.seek(0) - @output.read.chomp + @output.print end end diff --git a/lib/terminal_output.rb b/lib/terminal_output.rb new file mode 100644 index 0000000..f245f26 --- /dev/null +++ b/lib/terminal_output.rb @@ -0,0 +1,14 @@ +class TerminalOutput + def initialize + @buffer = StringIO.new + end + + def add_new_line(text) + @buffer.puts text + end + + def print + @buffer.seek(0) + @buffer.read.chomp + end +end diff --git a/spec/terminal_output_spec.rb b/spec/terminal_output_spec.rb new file mode 100644 index 0000000..8942589 --- /dev/null +++ b/spec/terminal_output_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' +require_relative '../lib/terminal_output' + +describe '#add_new_line' do + it 'adds a new line to the printed output on the console' do + output = TerminalOutput.new + output.add_new_line "test" + output.add_new_line "print" + expect(output.print).to eq "test\nprint" + end +end + +describe '#print' do + it 'displays what has been added to the buffer' do + output = TerminalOutput.new + output.add_new_line "foo" + output.add_new_line "bar" + expect(output.print).to eq "foo\nbar" + end +end From 0fc273ae2adc61989543f55bafb7a5971ec23ce3 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 10:35:40 -0400 Subject: [PATCH 08/15] now scans a source file to read the characters and lines --- lib/macbeth_analyzer.rb | 28 ++++++++-- spec/macbeth_analyzer_spec.rb | 7 ++- spec/sample_data.xml | 101 ++++++++++++++++++++++++++++++++++ 3 files changed, 129 insertions(+), 7 deletions(-) create mode 100644 spec/sample_data.xml diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index 4688ba5..9a8decc 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -1,13 +1,33 @@ +require 'terminal_output' +require 'nokogiri' + class MacbethAnalyzer - def initialize() + def initialize(source) + @speakers = Hash.new + @xml_doc = Nokogiri::XML(source) @output = TerminalOutput.new + end def analyze - @output.add_new_line "12 Macbeth" - @output.add_new_line "4 Banquo" - @output.add_new_line "3 Duncan" + parse_speakers + + # sort it so that they are in descending order by key (line count) + @speakers = Hash[@speakers.sort_by { |key, value| value }.reverse] + + @speakers.each do |key, value| + @output.add_new_line "#{value} #{key.capitalize}" + end @output.print end + + private + + def parse_speakers + @xml_doc.xpath("//SPEAKER").each do |node| + siblings = @xml_doc.search("SPEAKER[text()='#{node.content}'] ~ *").map &:text + @speakers[node.content] = siblings.length + end + end end diff --git a/spec/macbeth_analyzer_spec.rb b/spec/macbeth_analyzer_spec.rb index e3abc3b..011461f 100644 --- a/spec/macbeth_analyzer_spec.rb +++ b/spec/macbeth_analyzer_spec.rb @@ -2,9 +2,10 @@ require_relative '../lib/macbeth_analyzer' describe 'displays number of total lines spoken by characters in Macbeth' do - it 'prints the number with the name' do - analyzer = MacbethAnalyzer.new + it 'prints the correct number of lines for the characters with the specified document' do + source = File.read('spec/sample_data.xml') + analyzer = MacbethAnalyzer.new(source) results = analyzer.analyze - expect(results).to eq "12 Macbeth\n4 Banquo\n3 Duncan" + expect(results).to eq "11 Macbeth\n5 Malcolm\n2 All" end end diff --git a/spec/sample_data.xml b/spec/sample_data.xml new file mode 100644 index 0000000..647818e --- /dev/null +++ b/spec/sample_data.xml @@ -0,0 +1,101 @@ + + + +The Tragedy of Macbeth + + +

Text placed in the public domain by Moby Lexical Tools, 1992.

+

SGML markup by Jon Bosak, 1992-1994.

+

XML version by Jon Bosak, 1996-1998.

+

This work may be freely copied and distributed worldwide.

+
+ + + +Dramatis Personae + +DUNCAN, king of Scotland. + + +MALCOLM +DONALBAIN +his sons. + + + + +MACBETH +BANQUO +generals of the king's army. + + + + +MACDUFF +LENNOX +ROSS +MENTEITH +ANGUS +CAITHNESS +noblemen of Scotland. + + +FLEANCE, son to Banquo. +SIWARD, Earl of Northumberland, general of the English forces. +YOUNG SIWARD, his son. +SEYTON, an officer attending on Macbeth. +Boy, son to Macduff. +An English Doctor. +A Scotch Doctor. +A Soldier. +A Porter. +An Old Man. +LADY MACBETH +LADY MACDUFF +Gentlewoman attending on Lady Macbeth. +HECATE +Three Witches. +Apparitions. +Lords, Gentlemen, Officers, Soldiers, Murderers, Attendants, and Messengers. + + +SCENE Scotland: England. + +MACBETH + +ACT I + +MACBETH +Into the air; and what seem'd corporal melted +As breath into the wind. Would they had stay'd! + + + +ALL +Fair is foul, and foul is fair: +Hover through the fog and filthy air. + + + +MALCOLM +This is the sergeant +Who like a good and hardy soldier fought +'Gainst my captivity. Hail, brave friend! +Say to the king the knowledge of the broil +As thou didst leave it. + + + +MACBETH +Stay, you imperfect speakers, tell me more: +By Sinel's death I know I am thane of Glamis; +But how of Cawdor? the thane of Cawdor lives, +A prosperous gentleman; and to be king +Stands not within the prospect of belief, +No more than to be Cawdor. Say from whence +You owe this strange intelligence? or why +Upon this blasted heath you stop our way +With such prophetic greeting? Speak, I charge you. + + +
From 0fb74e212538f418538337fe5015e0bcfcbe36fa Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 10:45:06 -0400 Subject: [PATCH 09/15] added nokogiri to gem file --- Gemfile | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile b/Gemfile index e99640c..a57eed4 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org gem 'rspec' +gem 'nokogiri' From 2f25de9c0dc73818acdfa9fcb76c7455e461d396 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 10:49:27 -0400 Subject: [PATCH 10/15] extracted a parser class for the data --- lib/macbeth_analyzer.rb | 20 +++++--------------- lib/shakespeare_parser.rb | 18 ++++++++++++++++++ spec/shakepeare_parser_spec.rb | 12 ++++++++++++ 3 files changed, 35 insertions(+), 15 deletions(-) create mode 100644 lib/shakespeare_parser.rb create mode 100644 spec/shakepeare_parser_spec.rb diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index 9a8decc..3cffbe0 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -1,33 +1,23 @@ require 'terminal_output' +require 'shakespeare_parser' require 'nokogiri' class MacbethAnalyzer def initialize(source) - @speakers = Hash.new - @xml_doc = Nokogiri::XML(source) + @parser = ShakespeareParser.new(source) @output = TerminalOutput.new - end def analyze - parse_speakers + speakers = @parser.speaker_line_count # sort it so that they are in descending order by key (line count) - @speakers = Hash[@speakers.sort_by { |key, value| value }.reverse] + speakers = Hash[speakers.sort_by { |key, value| value }.reverse] - @speakers.each do |key, value| + speakers.each do |key, value| @output.add_new_line "#{value} #{key.capitalize}" end @output.print end - - private - - def parse_speakers - @xml_doc.xpath("//SPEAKER").each do |node| - siblings = @xml_doc.search("SPEAKER[text()='#{node.content}'] ~ *").map &:text - @speakers[node.content] = siblings.length - end - end end diff --git a/lib/shakespeare_parser.rb b/lib/shakespeare_parser.rb new file mode 100644 index 0000000..3a4d8f2 --- /dev/null +++ b/lib/shakespeare_parser.rb @@ -0,0 +1,18 @@ +require 'nokogiri' + +class ShakespeareParser + def initialize(xml_source_string) + @xml_doc = Nokogiri::XML(xml_source_string) + end + + def speaker_line_count + speakers = Hash.new + + @xml_doc.xpath("//SPEAKER").each do |node| + siblings = @xml_doc.search("SPEAKER[text()='#{node.content}'] ~ *").map &:text + speakers[node.content] = siblings.length + end + + speakers + end +end diff --git a/spec/shakepeare_parser_spec.rb b/spec/shakepeare_parser_spec.rb new file mode 100644 index 0000000..bb116e2 --- /dev/null +++ b/spec/shakepeare_parser_spec.rb @@ -0,0 +1,12 @@ +require 'spec_helper' +require_relative '../lib/shakespeare_parser' + +describe '.speaker_line_count' do + it 'creates a hash of characters and their line numbers' do + source = File.read('spec/sample_data.xml') + parser = ShakespeareParser.new(source) + result = parser.speaker_line_count + expect(result['MACBETH']).to eq 11 + expect(result['MALCOLM']).to eq 5 + end +end From 27930c9e0a9d6b03c2d9ccdb889e0cfd9a7d042f Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 11:16:30 -0400 Subject: [PATCH 11/15] extracted out formatting of the output --- lib/macbeth_analyzer.rb | 8 ++------ lib/output_formatter.rb | 13 +++++++++++++ spec/output_formatter_spec.rb | 13 +++++++++++++ 3 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 lib/output_formatter.rb create mode 100644 spec/output_formatter_spec.rb diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb index 3cffbe0..6d85e28 100644 --- a/lib/macbeth_analyzer.rb +++ b/lib/macbeth_analyzer.rb @@ -6,17 +6,13 @@ class MacbethAnalyzer def initialize(source) @parser = ShakespeareParser.new(source) @output = TerminalOutput.new + @formatter = OutputFormatter.new(@output) end def analyze speakers = @parser.speaker_line_count - # sort it so that they are in descending order by key (line count) - speakers = Hash[speakers.sort_by { |key, value| value }.reverse] - - speakers.each do |key, value| - @output.add_new_line "#{value} #{key.capitalize}" - end + @formatter.print_values_descending(speakers) @output.print end diff --git a/lib/output_formatter.rb b/lib/output_formatter.rb new file mode 100644 index 0000000..ac55688 --- /dev/null +++ b/lib/output_formatter.rb @@ -0,0 +1,13 @@ +class OutputFormatter + def initialize(output) + @output = output + end + + def print_values_descending(hash) # sort it so that they are in descending order by key (line count) + sorted_hash = Hash[hash.sort_by { |key, value| value }.reverse] + + sorted_hash.each do |key, value| + @output.add_new_line "#{value} #{key.capitalize}" + end + end +end diff --git a/spec/output_formatter_spec.rb b/spec/output_formatter_spec.rb new file mode 100644 index 0000000..28dbdf2 --- /dev/null +++ b/spec/output_formatter_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +require_relative '../lib/terminal_output' +require_relative '../lib/output_formatter' + +describe '.print_values_descending' do + it 'fills output in correct order' do + output_buffer = TerminalOutput.new + formatter = OutputFormatter.new(output_buffer) + h = Hash["mark" => 100, "steve" => 12, "kevin" => 200] + formatter.print_values_descending(h) + expect(output_buffer.print).to eq "200 Kevin\n100 Mark\n12 Steve" + end +end From 9769b54a3f0d1fe2f8ba8f92651ba8a45f27c79b Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 14:07:07 -0400 Subject: [PATCH 12/15] Added spec for malformed xml and renamed --- lib/shakespeare_parser.rb | 2 +- .../{shakepeare_parser_spec.rb => shakespeare_parser_spec.rb} | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) rename spec/{shakepeare_parser_spec.rb => shakespeare_parser_spec.rb} (77%) diff --git a/lib/shakespeare_parser.rb b/lib/shakespeare_parser.rb index 3a4d8f2..a371809 100644 --- a/lib/shakespeare_parser.rb +++ b/lib/shakespeare_parser.rb @@ -2,7 +2,7 @@ class ShakespeareParser def initialize(xml_source_string) - @xml_doc = Nokogiri::XML(xml_source_string) + @xml_doc = Nokogiri::XML(xml_source_string) { |config| config.strict } end def speaker_line_count diff --git a/spec/shakepeare_parser_spec.rb b/spec/shakespeare_parser_spec.rb similarity index 77% rename from spec/shakepeare_parser_spec.rb rename to spec/shakespeare_parser_spec.rb index bb116e2..2858b6d 100644 --- a/spec/shakepeare_parser_spec.rb +++ b/spec/shakespeare_parser_spec.rb @@ -9,4 +9,8 @@ expect(result['MACBETH']).to eq 11 expect(result['MALCOLM']).to eq 5 end + + it 'throws an error for malformed xml' do + expect{ShakespeareParser.new("asdas")}.to raise_error + end end From a1e19edc58ceabc1f6dd83d9410cc476cfe98106 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 14:08:15 -0400 Subject: [PATCH 13/15] extracted shakespeare analysis class, renamed macbeth analyzer, added default file --- lib/macbeth_analyzer.rb | 19 ------------------- lib/macbeth_line_analyzer.rb | 23 +++++++++++++++++++++++ lib/shakespeare_line_analysis.rb | 19 +++++++++++++++++++ spec/macbeth_analyzer_spec.rb | 11 ----------- spec/macbeth_line_analyzer_spec.rb | 21 +++++++++++++++++++++ spec/shakespeare_line_analysis_spec.rb | 13 +++++++++++++ 6 files changed, 76 insertions(+), 30 deletions(-) delete mode 100644 lib/macbeth_analyzer.rb create mode 100644 lib/macbeth_line_analyzer.rb create mode 100644 lib/shakespeare_line_analysis.rb delete mode 100644 spec/macbeth_analyzer_spec.rb create mode 100644 spec/macbeth_line_analyzer_spec.rb create mode 100644 spec/shakespeare_line_analysis_spec.rb diff --git a/lib/macbeth_analyzer.rb b/lib/macbeth_analyzer.rb deleted file mode 100644 index 6d85e28..0000000 --- a/lib/macbeth_analyzer.rb +++ /dev/null @@ -1,19 +0,0 @@ -require 'terminal_output' -require 'shakespeare_parser' -require 'nokogiri' - -class MacbethAnalyzer - def initialize(source) - @parser = ShakespeareParser.new(source) - @output = TerminalOutput.new - @formatter = OutputFormatter.new(@output) - end - - def analyze - speakers = @parser.speaker_line_count - - @formatter.print_values_descending(speakers) - - @output.print - end -end diff --git a/lib/macbeth_line_analyzer.rb b/lib/macbeth_line_analyzer.rb new file mode 100644 index 0000000..d67c51f --- /dev/null +++ b/lib/macbeth_line_analyzer.rb @@ -0,0 +1,23 @@ +require 'open-uri' +require 'shakespeare_line_analysis' +require 'terminal_output' + +# This is the entry class for the analysis +# it is doing a bit too much, but, being the entry point, +# something has to have the knowledge of what to send where +class MacbethLineAnalyzer + WEB_SOURCE_LOCATION = "http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml" + + def initialize(source = nil) + source ||= open(WEB_SOURCE_LOCATION) + @analyzer = ShakespeareLineAnalysis.new(source, TerminalOutput.new) + end + + def analyze + @analyzer.print_character_line_count + end + + def default_source_location + WEB_SOURCE_LOCATION + end +end diff --git a/lib/shakespeare_line_analysis.rb b/lib/shakespeare_line_analysis.rb new file mode 100644 index 0000000..fa40eaa --- /dev/null +++ b/lib/shakespeare_line_analysis.rb @@ -0,0 +1,19 @@ +require 'shakespeare_parser' +require 'output_formatter' + +class ShakespeareLineAnalysis + def initialize(source, output) + @source = source + @output = output + end + + def print_character_line_count + parser = ShakespeareParser.new(@source) + speakers = parser.speaker_line_count + + formatter = OutputFormatter.new(@output) + formatter.print_values_descending(speakers) + + @output.print + end +end diff --git a/spec/macbeth_analyzer_spec.rb b/spec/macbeth_analyzer_spec.rb deleted file mode 100644 index 011461f..0000000 --- a/spec/macbeth_analyzer_spec.rb +++ /dev/null @@ -1,11 +0,0 @@ -require 'spec_helper' -require_relative '../lib/macbeth_analyzer' - -describe 'displays number of total lines spoken by characters in Macbeth' do - it 'prints the correct number of lines for the characters with the specified document' do - source = File.read('spec/sample_data.xml') - analyzer = MacbethAnalyzer.new(source) - results = analyzer.analyze - expect(results).to eq "11 Macbeth\n5 Malcolm\n2 All" - end -end diff --git a/spec/macbeth_line_analyzer_spec.rb b/spec/macbeth_line_analyzer_spec.rb new file mode 100644 index 0000000..8d060d2 --- /dev/null +++ b/spec/macbeth_line_analyzer_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' +require_relative '../lib/macbeth_line_analyzer' + +describe 'displays number of total lines spoken by characters in Macbeth' do + it 'prints the correct number of lines for the characters with the specified document' do + source = File.read('spec/sample_data.xml') + analyzer = MacbethLineAnalyzer.new(source) + results = analyzer.analyze + expect(results).to eq "11 Macbeth\n5 Malcolm\n2 All" + end + + it 'has a default source pointing to a web location' do + analyzer = MacbethLineAnalyzer.new + expect(analyzer.default_source_location).to eq "http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml" + end + + it 'returns an error for malformed xml input' do + analyzer = MacbethLineAnalyzer.new("asdsada") + expect{analyzer.analyze}.to raise_error + end +end diff --git a/spec/shakespeare_line_analysis_spec.rb b/spec/shakespeare_line_analysis_spec.rb new file mode 100644 index 0000000..b227f9a --- /dev/null +++ b/spec/shakespeare_line_analysis_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' +require_relative '../lib/terminal_output' +require_relative '../lib/shakespeare_line_analysis' + +describe '#print_character_line_count' do + it 'prints the number of lines for each character' do + source = File.open('spec/sample_data.xml') + output = TerminalOutput.new + analyzer = ShakespeareLineAnalysis.new(source, output) + results = analyzer.print_character_line_count + expect(results).to eq "11 Macbeth\n5 Malcolm\n2 All" + end +end From 0c620fb95dc4b14872acb5edbac17dcdc181e223 Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 14:30:33 -0400 Subject: [PATCH 14/15] added relative loading --- lib/macbeth_line_analyzer.rb | 6 +++--- lib/shakespeare_line_analysis.rb | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/macbeth_line_analyzer.rb b/lib/macbeth_line_analyzer.rb index d67c51f..5f9c504 100644 --- a/lib/macbeth_line_analyzer.rb +++ b/lib/macbeth_line_analyzer.rb @@ -1,6 +1,6 @@ require 'open-uri' -require 'shakespeare_line_analysis' -require 'terminal_output' +require_relative 'shakespeare_line_analysis' +require_relative 'terminal_output' # This is the entry class for the analysis # it is doing a bit too much, but, being the entry point, @@ -9,7 +9,7 @@ class MacbethLineAnalyzer WEB_SOURCE_LOCATION = "http://www.ibiblio.org/xml/examples/shakespeare/macbeth.xml" def initialize(source = nil) - source ||= open(WEB_SOURCE_LOCATION) + source ||= File.read(open(WEB_SOURCE_LOCATION)) @analyzer = ShakespeareLineAnalysis.new(source, TerminalOutput.new) end diff --git a/lib/shakespeare_line_analysis.rb b/lib/shakespeare_line_analysis.rb index fa40eaa..b7942cd 100644 --- a/lib/shakespeare_line_analysis.rb +++ b/lib/shakespeare_line_analysis.rb @@ -1,5 +1,5 @@ -require 'shakespeare_parser' -require 'output_formatter' +require_relative 'shakespeare_parser' +require_relative 'output_formatter' class ShakespeareLineAnalysis def initialize(source, output) From 5cf15b860a11e471aee9b0a889436f16124e5d0c Mon Sep 17 00:00:00 2001 From: MunkiPhD Date: Sat, 19 Apr 2014 14:32:18 -0400 Subject: [PATCH 15/15] Added ruby file to actually execute the script --- bin/macbeth_analyzer.rb | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 bin/macbeth_analyzer.rb diff --git a/bin/macbeth_analyzer.rb b/bin/macbeth_analyzer.rb new file mode 100644 index 0000000..69bbf84 --- /dev/null +++ b/bin/macbeth_analyzer.rb @@ -0,0 +1,3 @@ +require_relative '../lib/macbeth_line_analyzer' + +puts MacbethLineAnalyzer.new().analyze