Class: Maglev::AttachmentExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/maglev/attachment_extractor.rb

Instance Method Summary collapse

Constructor Details

#initialize(allowed_content_types: Maglev.configuration.attachment_allowed_content_types, max_bytes: Maglev.configuration.attachment_max_bytes, max_characters: Maglev.configuration.attachment_max_characters) ⇒ AttachmentExtractor

Returns a new instance of AttachmentExtractor.



10
11
12
13
14
15
16
# File 'lib/maglev/attachment_extractor.rb', line 10

def initialize(allowed_content_types: Maglev.configuration.attachment_allowed_content_types,
  max_bytes: Maglev.configuration.attachment_max_bytes,
  max_characters: Maglev.configuration.attachment_max_characters)
  @allowed_content_types = allowed_content_types
  @max_bytes = max_bytes
  @max_characters = max_characters
end

Instance Method Details

#extract(blob, source_name:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/maglev/attachment_extractor.rb', line 18

def extract(blob, source_name:)
  source_identifier = source_identifier(source_name, blob)
   = (blob)
  return skipped(source_identifier, "unsupported_content_type", ) unless allowed?(blob)
  return skipped(source_identifier, "size_limit", ) if too_large?(blob)

  text = extract_text(blob)
  text, truncated = truncate(text)
  ExtractedDocument.extracted(
    source_identifier: source_identifier,
    text: text,
    metadata: .merge(strategy: "deterministic", truncated: truncated)
  )
end