Class: Pdfh::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfh/main.rb

Overview

Main functionality. This class is intended to manage the pdf documents

Class Method Summary collapse

Class Method Details

.start(argv:) ⇒ void

This method returns an undefined value.

Parameters:

  • argv (Array<String>)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/pdfh/main.rb', line 9

def start(argv:)
  arg_options = Services::OptParser.new(argv: argv).parse_argv
  options = RunOptions.new(**arg_options)

  # Initialize the global logger
  Pdfh.logger = Console.new(options.verbose?)
  Pdfh.logger.print_options(arg_options)

  settings = Services::SettingsBuilder.call
  Pdfh.logger.debug "Destination path: #{settings.base_path.colorize(:light_blue)}"

  files = Services::DirectoryScanner.new(settings.lookup_dirs).scan
  matcher = Services::DocumentMatcher.new(settings.document_types)

  files.each do |file_path|
    Pdfh.logger.info "Working on: #{file_path.colorize(:green)}" if Pdfh.logger.verbose?
    text = Services::PdfTextExtractor.call(file_path)

    documents = matcher.match(file_path, text)
    next Pdfh.logger.debug "No document type match found for #{file_path.colorize(:yellow)}" if documents.empty?

    unless documents.one?
      matches = documents.map { _1.type.name.inspect }.join(", ")
      next Pdfh.logger.warn_print "Skipping #{file_path.inspect} as multiple matches found: #{matches}."
    end

    Services::DocumentManager.new(documents.first, base_path: settings.base_path, dry_run: options.dry?).call
  end

  nil
rescue SettingsIOError => e
  Pdfh.logger.error_print(e.message, exit_app: false)
  exit(1)
rescue StandardError => e
  Pdfh.logger.backtrace_print(e) if Pdfh.logger.verbose?
  Pdfh.logger.error_print(e.message)
end