class Plaintext::Resolver

Constants

HANDLERS

Attributes

cached_file_handlers[RW]
max_plaintext_bytes[RW]

maximum length of returned plain text in bytes. Default: 4MB

Public Class Methods

file_handlers() click to toggle source
# File lib/plaintext/resolver.rb, line 22
def file_handlers
  return self.cached_file_handlers if self.cached_file_handlers.present?
  self.cached_file_handlers = HANDLERS.map(&:new)
end
new(file, content_type = nil) click to toggle source
# File lib/plaintext/resolver.rb, line 28
def initialize(file, content_type = nil)
  @file = file
  @content_type = content_type
  @max_plaintext_bytes = 4_194_304 # 4 megabytes
end

Public Instance Methods

text() click to toggle source

Returns the extracted fulltext or nil if no matching handler was found for the file type.

# File lib/plaintext/resolver.rb, line 37
def text
  if handler = find_handler and
      text = handler.text(@file, max_size: max_plaintext_bytes)

    text.gsub!(/\s+/m, ' ')
    text.strip!
    text.mb_chars.compose.limit(max_plaintext_bytes).to_s
  end
end

Private Instance Methods

find_handler() click to toggle source
# File lib/plaintext/resolver.rb, line 49
def find_handler
  self.class.file_handlers.detect { |h| h.accept? @content_type }
end