Class: LoaderRuby::Loaders::Docx

Inherits:
Base
  • Object
show all
Defined in:
lib/loader_ruby/loaders/docx.rb

Constant Summary collapse

EXTENSIONS =
%w[.docx].freeze

Instance Method Summary collapse

Instance Method Details

#load(path, **opts) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/loader_ruby/loaders/docx.rb', line 8

def load(path, **opts)
  check_file_exists!(path)
  check_file_size!(path)

  begin
    require "docx"
  rescue LoadError
    raise DependencyMissingError,
      "docx gem is required for DOCX loading. Add `gem 'docx'` to your Gemfile."
  end

  doc = ::Docx::Document.open(path)
  paragraphs = doc.paragraphs.map(&:text)
  content = paragraphs.join("\n")

  Document.new(
    content: content,
    metadata: (path,
      format: :docx,
      paragraphs: paragraphs.size
    )
  )
end