Class: Pdfh::Services::DocumentMatcher

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

Overview

Matches a PDF file against settings and builds a Document if valid

Instance Method Summary collapse

Constructor Details

#initialize(document_types) ⇒ DocumentMatcher

Parameters:



9
10
11
# File 'lib/pdfh/services/document_matcher.rb', line 9

def initialize(document_types)
  @document_types = document_types
end

Instance Method Details

#match(file, text) ⇒ Array<Document>

Parameters:

  • file (String)

    Path to the PDF file

  • text (String)

    Extracted text from the PDF

Returns:



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pdfh/services/document_matcher.rb', line 16

def match(file, text)
  @document_types.each_with_object([]) do |type, matches|
    # Try to match the document type by ID (content)
    next unless type.re_id.match?(text)

    Pdfh.logger.debug "Matched document type: #{type.name}"

    # Try to match the date in the text
    date_match = type.re_date.match(text)
    unless date_match
      Pdfh.logger.debug "No date match found for #{type.name}"
      next
    end

    # Extract date captures (handles both named and positional captures)
    date_captures = extract_date_captures(date_match)

    matches << Document.new(file, type, text, date_captures)
  end
end