Skip to content
This repository has been archived by the owner on Dec 28, 2017. It is now read-only.

Commit

Permalink
moved message formatting to separate Class
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Cook committed Nov 17, 2011
1 parent c64ae34 commit 0e5fac7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 45 deletions.
24 changes: 1 addition & 23 deletions lib/epub_validator/epub_check.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
module EpubValidator
class EpubCheck
def self.process(filename)
checked = epubcheck(filename)
format_epubcheck_message(checked)
end

private

def self.epubcheck(epubfile)
def self.epubcheck(filename)
epubcheck_jar = File.expand_path(File.dirname(__FILE__) + '/../epubcheck-1-2/epubcheck-1.2.jar')
`java -jar #{epubcheck_jar} "#{epubfile}" 2>&1`
end

def self.format_epubcheck_message(message)
return ['Passed.'] if message.match('No errors or warnings detected')

m_array = message.split(/\n/)

# clean up all useless info
m_array.delete_if do |s|
s.empty? or
s.match('^Epubcheck Version.*') or
s.match('^Check finished.*')
end

m_array.unshift('FAILED!')
end
end
end
26 changes: 4 additions & 22 deletions spec/epub_validator/epub_check_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,12 @@

module EpubValidator
describe EpubCheck do
context "when it tries to process a missing file" do
it "should return a formatted I/O error message" do
message = "ERROR: test.epub: I/O error: test.epub (No such file or directory)"
formatted_message = ["FAILED!", "ERROR: test.epub: I/O error: test.epub (No such file or directory)"]
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(formatted_message)
end
end
context "when it processes a valid file" do
it "should return 'Passed.' message as an array" do
message = "Epubcheck Version 1.2\n\nNo errors or warnings detected\n"
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(['Passed.'])
end
end
context "when it processes an invalid file" do
it "should return error message as an array" do
context "when it processes a file" do
it "should return a message" do
message = "Epubcheck Version 1.2\n\nERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing\n\nCheck finished with warnings or errors!"
formatted_message = ["FAILED!", "ERROR: book.epub: resource OEBPS/stylesheets/handbookish.css is missing"]
EpubCheck.stub(:epubcheck).and_return(message)
ec = EpubCheck.process("test.epub")
ec.should eq(formatted_message)
ec = EpubCheck.epubcheck("test.epub")
ec.should eq(message)
end
end
end
Expand Down

0 comments on commit 0e5fac7

Please sign in to comment.