Sha256: 8fde76ce1c4faf39c5c357fb2d55eae98fa09d5991d5be7c76455974df30fe22

Contents?: true

Size: 1.5 KB

Versions: 20

Compression:

Stored size: 1.5 KB

Contents

require 'docx'
require 'ndr_support/safe_file'
require_relative 'office_file_helper'
require_relative 'registry'

module NdrImport
  # This is one of a collection of file handlers that deal with individual formats of data.
  # They can be instantiated directly or via the factory method Registry.tables
  module File
    # This class is a modern Word document file handler that returns a single table.
    # It only works on .docx documents
    class Docx < Base
      include OfficeFileHelper

      private

      def rows(&block)
        return enum_for(:rows) unless block

        send(@options['file_password'] ? :decrypted_path : :unencrypted_path) do |path|
          doc = ::Docx::Document.open(path)

          doc.paragraphs.each do |p|
            yield(p.to_s)
          end

          doc.zip.close
        end
      rescue StandardError => e
        raise("#{SafeFile.basename(@filename)} [#{e.class}: #{e.message}]")
      end

      # This method returns the path to the temporary, decrypted file
      def decrypted_path
        file_string = decrypted_file_string(@filename, @options['file_password'])
        Tempfile.create(['decrypted', '.docx'], encoding: file_string.encoding) do |file|
          file.write(file_string)
          file.close

          yield file.path
        end
      end

      # This method returns the safepath to the unencrypted docx file
      def unencrypted_path
        yield SafeFile.safepath_to_string(@filename)
      end
    end

    Registry.register(Docx, 'docx')
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
ndr_import-11.2.1 lib/ndr_import/file/docx.rb
ndr_import-11.2.0 lib/ndr_import/file/docx.rb
ndr_import-11.1.0 lib/ndr_import/file/docx.rb
ndr_import-11.0.2 lib/ndr_import/file/docx.rb
ndr_import-11.0.1 lib/ndr_import/file/docx.rb
ndr_import-11.0.0 lib/ndr_import/file/docx.rb
ndr_import-10.3.0 lib/ndr_import/file/docx.rb
ndr_import-10.2.0 lib/ndr_import/file/docx.rb
ndr_import-10.1.3 lib/ndr_import/file/docx.rb
ndr_import-10.1.2 lib/ndr_import/file/docx.rb
ndr_import-10.1.1 lib/ndr_import/file/docx.rb
ndr_import-10.1 lib/ndr_import/file/docx.rb
ndr_import-10.0 lib/ndr_import/file/docx.rb
ndr_import-9.1.0 lib/ndr_import/file/docx.rb
ndr_import-9.0.3 lib/ndr_import/file/docx.rb
ndr_import-9.0.2 lib/ndr_import/file/docx.rb
ndr_import-9.0.1 lib/ndr_import/file/docx.rb
ndr_import-9.0.0 lib/ndr_import/file/docx.rb
ndr_import-8.6.0 lib/ndr_import/file/docx.rb
ndr_import-8.5.2 lib/ndr_import/file/docx.rb