Class: OoxmlParser::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/ooxml_parser/common_parser/parser.rb

Overview

Basic class for OoxmlParser

Class Method Summary collapse

Class Method Details

.parse(path_to_file, password: nil) ⇒ CommonDocumentStructure

Base method to parse document of any type

Parameters:

  • path_to_file (String)

    file

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ooxml_parser/common_parser/parser.rb', line 27

def parse(path_to_file, password: nil)
  file = OoxmlFile.new(path_to_file)
  file = file.decrypt(password) if password
  Parser.parse_format(file) do |yielded_file|
    format = yielded_file.format_by_folders
    case format
    when :docx
      DocumentStructure.new(unpacked_folder: yielded_file.path_to_folder).parse
    when :xlsx
      XLSXWorkbook.new(unpacked_folder: yielded_file.path_to_folder).parse
    when :pptx
      Presentation.new(unpacked_folder: yielded_file.path_to_folder).parse
    else
      warn "#{path_to_file} is a simple zip file without OOXML content"
    end
  end
end

.parse_format(file) ⇒ CommonDocumentStructure

Base method to yield parse document of any type

Parameters:

Returns:



13
14
15
16
17
18
19
20
21
22
# File 'lib/ooxml_parser/common_parser/parser.rb', line 13

def parse_format(file)
  return nil if EncryptionChecker.new(file.path).encrypted?

  file.copy_file_and_rename_to_zip
  file.unzip
  model = yield(file)
  model.file_path = file.path if model
  FileUtils.rm_rf(file.path_to_folder)
  model
end