Class: SimpleTextExtract::Extract

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_text_extract/extract.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil) ⇒ Extract

Returns a new instance of Extract.



25
26
27
28
# File 'lib/simple_text_extract/extract.rb', line 25

def initialize(filename: nil, raw: nil, filepath: nil, tempfile: nil)
  @file = get_file(filename:, raw:, filepath:, tempfile:)
  @formatter = self.class.formatter(File.extname(file)) if file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



23
24
25
# File 'lib/simple_text_extract/extract.rb', line 23

def file
  @file
end

#formatterObject (readonly)

Returns the value of attribute formatter.



23
24
25
# File 'lib/simple_text_extract/extract.rb', line 23

def formatter
  @formatter
end

Class Method Details

.formatter(path) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/simple_text_extract/extract.rb', line 4

def self.formatter(path)
  case path
  when /.zip$/i
    :zip
  when /(.txt$|.csv$)/i
    :plain
  when /.pdf$/i
    :pdf
  when /.docx$/i
    :docx
  when /.doc$/i
    :doc
  when /.xlsx$/i
    :xlsx
  when /.xls$/i
    :xls
  end
end

Instance Method Details

#to_sObject



30
31
32
# File 'lib/simple_text_extract/extract.rb', line 30

def to_s
  @to_s ||= extract.to_s.scrub.gsub(/[^\S\n]+/, " ").gsub(/\s?\n\s+/, "\n").strip
end