Permalink
Browse files

Refactored to decouple and make easier reading.

  • Loading branch information...
1 parent b32a4e4 commit 16272048aa318c327fb1b42a1ce7cff832ba646f @jonmagic committed Oct 13, 2011
Showing with 44 additions and 28 deletions.
  1. +2 −13 lib/pdf_uploader.rb
  2. +5 −6 lib/process_pdf.rb
  3. +30 −0 spec/pdf_uploader_spec.rb
  4. +7 −9 spec/process_pdf_spec.rb
View
@@ -15,23 +15,12 @@ def store_dir
end
def grim
- @grim ||= Grim.reap(cache_path)
- end
-
- def page_count
- @page_count ||= begin
- cache_stored_file! unless cached?
- grim.count
- end
+ cache_stored_file! unless cached?
+ Grim.reap(cache_path)
end
def create_preview
cache_stored_file! unless cached?
grim[0].save(File.join(store_dir, 'preview.jpg'))
end
-
- def extract_text(index)
- cache_stored_file! unless cached?
- grim[index].text
- end
end
View
@@ -1,14 +1,13 @@
class ProcessPdf
def self.perform(document_id)
document = Document.find!(document_id)
+ pdf = document.pdf
- page_count = document.pdf.page_count
+ if pdf.grim.count > 0
+ pdf.create_preview
- if page_count > 0
- document.pdf.create_preview
-
- 0.upto(page_count - 1).each do |index|
- document.page_contents << document.pdf.grim[index].text
+ pdf.grim.each do |page|
+ document.page_contents << page.text
end
document.save!
View
@@ -0,0 +1,30 @@
+require 'spec_helper'
+
+describe PdfUploader do
+ let(:document) { Document.create(:pdf => pdf_fixture('onepage.pdf')) }
+
+ describe "#cache_dir" do
+ it "returns correct path" do
+ document.pdf.cache_dir.should == "#{PdfArchive.root}/tmp/cache/#{document.id}"
+ end
+ end
+
+ describe "#store_dir" do
+ it "returns correct path" do
+ document.pdf.store_dir.should == "#{PdfArchive.root}/tmp/documents/#{document.id}"
+ end
+ end
+
+ describe "#grim" do
+ it "returns an instance of Grim::Pdf" do
+ document.pdf.grim.class.should == Grim::Pdf
+ end
+ end
+
+ describe "#create_preview" do
+ it "creates a preview image" do
+ document.pdf.create_preview
+ File.exists?(File.join(tmp_dir, 'documents', document.id, 'preview.jpg')).should be_true
+ end
+ end
+end
View
@@ -4,21 +4,19 @@
let(:document) { Document.create(:pdf => pdf_fixture('onepage.pdf')) }
describe ".perform" do
- before(:all) do
+ it "calls create_preview on pdf" do
+ PdfUploader.any_instance.should_receive(:create_preview)
ProcessPdf.perform(document.id)
- document.reload
end
- it "creates a preview image" do
- File.exists?(File.join(tmp_dir, 'documents', document.id, 'preview.jpg')).should be_true
- end
-
- it "extracts and saves pdf text to document" do
- document.page_contents.length.should > 0
+ it "extracts text from pages" do
+ Grim::Page.any_instance.should_receive(:text)
+ ProcessPdf.perform(document.id)
end
it "creates search terms for hunt" do
- document.searches['default'].length.should > 0
+ ProcessPdf.perform(document.id)
+ document.reload.searches['default'].length.should > 0
end
end
end

0 comments on commit 1627204

Please sign in to comment.