class SimpleTextExtract::TextExtractor

Attributes

file[R]

Public Class Methods

new(filename: nil, raw: nil, filepath: nil, tempfile: nil) click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 7
def initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil)
  @file = get_file(filename: filename, raw: raw, filepath: filepath, tempfile: tempfile)
end

Public Instance Methods

to_s() click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 11
def to_s
  @to_s ||= extract.to_s
end

Private Instance Methods

cleanup() click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 39
def cleanup
  return unless file.instance_of?(Tempfile)

  file.close
  file.unlink
end
extract() click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 27
def extract
  return unless file

  begin
    FormatExtractorFactory.call(file).extract
  rescue StandardError
    nil
  ensure
    cleanup
  end
end
get_file(filename:, raw:, filepath:, tempfile:) click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 17
def get_file(filename:, raw:, filepath:, tempfile:)
  if tempfile&.class == Tempfile
    tempfile
  elsif !filename.nil? && !raw.nil?
    write_tempfile(filename: filename.to_s, raw: raw)
  elsif !filepath.nil? && File.exist?(filepath)
    File.new(filepath)
  end
end
write_tempfile(filename:, raw:) click to toggle source
# File lib/simple_text_extract/text_extractor.rb, line 46
def write_tempfile(filename:, raw:)
  filename = filename.split(".").yield_self { |parts| [parts[0], ".#{parts[1]}"] }
  file = Tempfile.new(filename)
  raw = String.new(raw, encoding: Encoding::UTF_8)

  file.write(raw)
  file.tap(&:rewind)
end