Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Just a bit simpler code + specs #1

Merged
merged 1 commit into from

2 participants

@christoffer

(Don't worry, still 100% cc)

@chdorner chdorner merged commit 872b79c into from
@chdorner
Owner

:ok_hand:
Great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 16, 2012
  1. @christoffer
This page is out of date. Refresh to see the latest.
Showing with 22 additions and 108 deletions.
  1. +7 −11 lib/epubinfo/parser.rb
  2. +15 −97 spec/lib/epubinfo/parser_spec.rb
View
18 lib/epubinfo/parser.rb
@@ -13,29 +13,25 @@ def metadata_document
end
def drm_protected?
- load_epub if @zipfile.nil?
- @drm_protected ||= !!@zipfile.find_entry('META-INF/rights.xml')
+ @drm_protected ||= !!zip_file.find_entry('META-INF/rights.xml')
end
private
- def load_epub
- @zipfile = Zip::ZipFile.open(@path)
+ def zip_file
+ @zip_file ||= Zip::ZipFile.open(@path)
end
- def load_root_file
- load_epub if @zipfile.nil?
- @root_document = Nokogiri::XML(@zipfile.read('META-INF/container.xml'))
+ def root_document
+ @root_document ||= Nokogiri::XML(zip_file.read('META-INF/container.xml'))
end
def metadata_path
- load_root_file if @root_document.nil?
- @root_document.css('container rootfiles rootfile:first-child').attribute('full-path').content
+ root_document.css('container rootfiles rootfile:first-child').attribute('full-path').content
end
def load_metadata_file
- load_epub if @zipfile.nil?
- Nokogiri::XML(@zipfile.read(metadata_path))
+ Nokogiri::XML(zip_file.read(metadata_path))
end
end
end
View
112 spec/lib/epubinfo/parser_spec.rb
@@ -2,110 +2,28 @@
describe EPUBInfo::Parser do
let(:epub_path) { File.expand_path('spec/support/binary/metamorphosis_epub2.epub') }
- let(:parser) { EPUBInfo::Parser.parse(epub_path) }
+ subject { EPUBInfo::Parser.parse(epub_path) }
- describe '#parse' do
- it 'should assign path to @path' do
- parser # preload to avoid infinite loop
- EPUBInfo::Parser.stub(:new) { parser }
- EPUBInfo::Parser.parse(epub_path)
- parser.path.should == epub_path
- end
+ it 'should expose the path' do
+ subject.path.should == epub_path
end
- describe '#load_epub' do
- it 'assigns zipfile to @zipfile' do
- parser.send(:load_epub)
- parser.instance_variable_get(:@zipfile).should be_kind_of Zip::ZipFile
- end
+ it 'should expose the correct metadata document' do
+ subject.metadata_document.should be_kind_of Nokogiri::XML::Document
+ id_node = subject.metadata_document.xpath('//dc:identifier').first
+ id_node.content.should == 'http://www.gutenberg.org/ebooks/5200'
end
- describe '#load_root_file' do
- it 'should assign nokogiri document to @root_document' do
- parser.send(:load_epub)
- parser.send(:load_root_file)
- parser.instance_variable_get(:@root_document).should be_kind_of Nokogiri::XML::Document
- end
-
- it 'should call load_epub if @zipfile is nil' do
- zipfile = Zip::ZipFile.new(epub_path)
- zipfile.stub(:read) { '' }
- parser.should_receive(:load_epub) { parser.instance_variable_set(:@zipfile, zipfile); nil }
- parser.send(:load_root_file)
- end
- end
-
- describe '#metadata_path' do
- it 'should return path to metadata file' do
- parser.send(:metadata_path).should == '5200/content.opf'
- end
-
- it 'should call load_root_file if @root_document is nil' do
- xml_document = Nokogiri::XML::Document.new
- xml_document.stub_chain(:css, :attribute, :content)
- parser.should_receive(:load_root_file) { parser.instance_variable_set(:@root_document, xml_document); nil }
- parser.send(:metadata_path)
- end
- end
-
- describe '#metadata_document' do
- it 'should call load_metadata_file and assign to @metadata_document' do
- parser.should_receive(:load_metadata_file) { 'loaded' }
- parser.metadata_document
- parser.instance_variable_get(:@metadata_document).should == 'loaded'
- end
-
- it 'should not call load_metadata_file if @metadata_document is not nil' do
- parser.instance_variable_set(:@metadata_document, 'loaded')
- parser.should_not_receive(:load_metadata_file)
- parser.metadata_document
- end
+ it 'should return true for drm protected files' do
+ drm_path = 'spec/support/binary/metamorphosis_epub2_drm.epub'
+ parser = EPUBInfo::Parser.parse(drm_path)
+ parser.drm_protected?.should be_true
end
- describe '#drm_protected?' do
- it 'should call load_epub if nil' do
- zipfile_mock = mock(:find_entry => true)
- parser.should_receive(:load_epub) { parser.instance_variable_set(:@zipfile, zipfile_mock); nil }
- parser.drm_protected?
- parser.instance_variable_get(:@zipfile).should == zipfile_mock
- end
-
- context 'drm' do
- let(:epub_path) { File.expand_path('spec/support/binary/metamorphosis_epub2_drm.epub') }
-
- it 'should return true if rights.xml exists' do
- parser = EPUBInfo::Parser.parse(epub_path)
- parser.drm_protected?.should be_true
- end
- end
-
- context 'no drm' do
- let(:epub_path) { File.expand_path('spec/support/binary/metamorphosis_epub2.epub') }
-
- it 'should return false if rights.xml does not exist' do
- parser = EPUBInfo::Parser.parse(epub_path)
- parser.drm_protected?.should be_false
- end
- end
- end
-
- describe '#load_metadata_file' do
- it 'should return xml document' do
- parser.send(:load_root_file)
- parser.send(:load_metadata_file).should be_kind_of Nokogiri::XML::Document
- end
-
- it 'should call load_epub if @zipfile is nil' do
- zipfile = Zip::ZipFile.new(epub_path)
- zipfile.stub(:read) { '' }
- parser.should_receive(:load_epub) { parser.instance_variable_set(:@zipfile, zipfile); nil }
-
- xml_document = Nokogiri::XML::Document.new
- xml_document.stub_chain(:css, :attribute, :content)
- parser.should_receive(:load_root_file) { parser.instance_variable_set(:@root_document, xml_document); nil }
-
- parser.send(:load_metadata_file)
- end
+ it 'should return false for non-drm protected files' do
+ non_drm_path = 'spec/support/binary/metamorphosis_epub2.epub'
+ parser = EPUBInfo::Parser.parse(non_drm_path)
+ parser.drm_protected?.should be_false
end
end
Something went wrong with that request. Please try again.