Skip to content

Commit 708be74

Browse files
committed
Changed functionality of determining file path or stream
1 parent a965263 commit 708be74

2 files changed

Lines changed: 9 additions & 8 deletions

File tree

lib/docx/document.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,12 @@ class Document
2323
def initialize(path_or_io, options = {})
2424
@replace = {}
2525

26-
@zip = options[:use_buffer] ? Zip::File.open_buffer(path_or_io) : Zip::File.open(path_or_io)
26+
# if path-or_io is string && does not contain a null byte
27+
if (path_or_io.instance_of?(String) && !/\u0000/.match?(path_or_io))
28+
@zip = Zip::File.open(path_or_io)
29+
else
30+
@zip = Zip::File.open_buffer(path_or_io)
31+
end
2732

2833
@document_xml = @zip.read('word/document.xml')
2934
@doc = Nokogiri::XML(@document_xml)
@@ -49,10 +54,6 @@ def self.open(path, &block)
4954
new(path, &block)
5055
end
5156

52-
def self.open_buffer(buffer, &block)
53-
new(buffer, use_buffer: true, &block)
54-
end
55-
5657
def paragraphs
5758
@doc.xpath('//w:document//w:body/w:p').map { |p_node| parse_paragraph_from p_node }
5859
end

spec/docx/document_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
context 'using stream' do
8787
before do
8888
stream = File.binread(@fixtures_path + '/basic.docx')
89-
@doc = Docx::Document.open_buffer(stream)
89+
@doc = Docx::Document.open(stream)
9090
end
9191

9292
it_behaves_like 'reading'
@@ -365,7 +365,7 @@
365365
context 'from a stream' do
366366
before do
367367
stream = File.binread(@fixtures_path + '/saving.docx')
368-
@doc = Docx::Document.open_buffer(stream)
368+
@doc = Docx::Document.open(stream)
369369
end
370370

371371
it_behaves_like 'saving to file'
@@ -393,7 +393,7 @@
393393
doc = Docx::Document.open(@fixtures_path + '/basic.docx')
394394
result = doc.stream
395395

396-
@doc = Docx::Document.open_buffer(result)
396+
@doc = Docx::Document.open(result)
397397
end
398398

399399
it_behaves_like 'reading'

0 commit comments

Comments
 (0)