Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--color
--drb
--format documentation
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source 'https://rubygems.org'
ruby '2.0.0'

gem "rspec", "~> 2.14.1"
gem "nokogiri", "~> 1.6.1"
gem "debugger", "~> 1.6.3"
gem 'webmock'
gem 'shoulda-matchers'
54 changes: 54 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
GEM
remote: https://rubygems.org/
specs:
activesupport (4.0.1)
i18n (~> 0.6, >= 0.6.4)
minitest (~> 4.2)
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.5)
atomic (1.1.14)
columnize (0.3.6)
crack (0.4.1)
safe_yaml (~> 0.9.0)
debugger (1.6.3)
columnize (>= 0.3.1)
debugger-linecache (~> 1.2.0)
debugger-ruby_core_source (~> 1.2.4)
debugger-linecache (1.2.0)
debugger-ruby_core_source (1.2.4)
diff-lcs (1.2.5)
i18n (0.6.9)
mini_portile (0.5.2)
minitest (4.7.5)
multi_json (1.8.2)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.7)
rspec-expectations (2.14.4)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.4)
safe_yaml (0.9.7)
shoulda-matchers (2.4.0)
activesupport (>= 3.0.0)
thread_safe (0.1.3)
atomic
tzinfo (0.3.38)
webmock (1.16.1)
addressable (>= 2.2.7)
crack (>= 0.3.2)

PLATFORMS
ruby

DEPENDENCIES
debugger (~> 1.6.3)
nokogiri (~> 1.6.1)
rspec (~> 2.14.1)
shoulda-matchers
webmock
11 changes: 11 additions & 0 deletions lib/content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require 'nokogiri'
class Content
attr_accessor :data,:source
def initialize(source)
@source = source
@data = get_content_from_source
end

def get_content_from_source
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can raise an exception here in case this method is called from an instance of Content class directly.

end
8 changes: 8 additions & 0 deletions lib/file_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'content'
require 'nokogiri'
class FileContent < Content
def get_content_from_source
f = File.open(@source)
Nokogiri::XML(f)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Nokogiri::XML(open()) works for both opening a local file and reading remote file.

end
end
8 changes: 8 additions & 0 deletions lib/http_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'open-uri'
require_relative 'content'
require 'nokogiri'
class HttpContent < Content
def get_content_from_source
Nokogiri::XML(open(@source))
end
end
71 changes: 71 additions & 0 deletions lib/shakespeare_analyzer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require_relative 'speech'
class ShakespeareAnalyzer
attr_accessor :doc,:result
def initialize(doc)
@doc = doc
@result = {}
end

def analyze
play = get_play
acts = get_acts_from_play(play)
acts.each do |act|
add_act_to_result(act)
end
end

def add_act_to_result(act)
scenes = act.css("SCENE")
scenes.each do |scene|
add_scene_to_result(scene)
end
end

def add_scene_to_result(scene)
speeches = get_speeches_from_scene(scene)
speeches.each do |speech|
add_speech_to_result(speech)
end
end

def get_play
@doc.css("PLAY")
end

def get_acts_from_play(play)
play.css("ACT")
end

def get_speeches_from_scene(scene)
scene.css("SPEECH")
end

def add_speech_to_result(speech)
speech = Speech.new(speech)
speaker = speech.speaker
lines = speech.lines
add_to_result(speaker,lines)
end

def add_to_result(speaker,lines)
if @result[speaker].nil?
@result[speaker] = lines
else
@result[speaker] = (@result[speaker] + lines)
end
end

def sort_result
@result.sort_by { |name, lines| lines }.reverse
end

def format_result
sort_result
end

def print_result
format_result.each do |name,lines|
puts "#{lines} #{name.downcase.capitalize}"
end
end
end
13 changes: 13 additions & 0 deletions lib/speech.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Speech
def initialize(speech)
@speech = speech
end

def speaker
@speech.css("SPEAKER").text
end

def lines
@speech.css("LINE").count
end
end
7 changes: 7 additions & 0 deletions lib/string_content.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'nokogiri'
require 'content'
class StringContent < Content
def get_content_from_source
Nokogiri::XML(@source)
end
end
Loading