Class: Tsumetogi::Scratcher

Inherits:
Object
  • Object
show all
Defined in:
lib/tsumetogi/scratcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(pdf_path, config = nil) ⇒ Scratcher

Returns a new instance of Scratcher.



6
7
8
9
10
11
# File 'lib/tsumetogi/scratcher.rb', line 6

def initialize(pdf_path, config = nil)
  @pdf_path = pdf_path
  @config = config || Tsumetogi::Config.new
  @images_dir = @config.images_dir
  @images_dir ||= "#{File.dirname(@pdf_path)}/#{File.basename(@pdf_path, ".*")}-images"
end

Instance Method Details

#scratchObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/tsumetogi/scratcher.rb', line 13

def scratch
  tmp_dir = Dir.mktmpdir
  Tsumetogi.logger.debug "scratching #{File.basename(@pdf_path)} into #{tmp_dir}"

  references = fetch_reference_images(@images_dir)
  targets = slice_pages(tmp_dir)
  matcher = DpMatcher.new(references, targets, @config.diff_strategy).match

  ref_pages = scan_reference_pages(references)
  tar_pages = assign_target_pages(matcher.back_trace, ref_pages)

  if File.exists?(@images_dir)
    timestamp = Time.now.strftime("%Y%m%d_%H%M%S")
    backup_dir = "#{@images_dir}_#{timestamp}.bak"
    FileUtils.move(@images_dir, backup_dir)
  end

  move_images(targets, tar_pages, @images_dir)

ensure
  FileUtils.remove_entry_secure tmp_dir
end