Sha256: 1da94f45112257d2d023c8f3aa3f7d6301641646e479968eb177f75cb719ef6c
Contents?: true
Size: 1.26 KB
Versions: 3
Compression:
Stored size: 1.26 KB
Contents
# frozen_string_literal: true module SimpleTextExtract class TextExtractor attr_reader :file def initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil) @file = get_file(filename: filename, raw: raw, filepath: filepath, tempfile: tempfile) end def to_s @to_s ||= extract.to_s end private 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 def extract return unless file begin FormatExtractorFactory.call(file).extract rescue StandardError nil ensure cleanup end end def cleanup return unless file.instance_of?(Tempfile) file.close file.unlink end 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 end end
Version data entries
3 entries across 3 versions & 1 rubygems