open_xml_package 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/open_xml_package.rb +17 -0
- data/lib/open_xml_package/rubyzip_fix.rb +17 -0
- data/lib/open_xml_package/version.rb +1 -1
- data/test/open_xml_package_test.rb +28 -9
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0c48c674659490cb99dd732878aed3b4f0425cb
|
4
|
+
data.tar.gz: db6e4f5b6a5e3275759ab1128acaadbe3157e410
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 32b928ff33b063c806d10de43e521499c05fb890ff2164a351ace3633450676f2afd1e87eda4e2e648036453df8eeb09eec32da245080047deffd6f5f347f708
|
7
|
+
data.tar.gz: d20a4f7eeb74dfbc4ee8bab6dbd42c4b4aabf65c54bd8472a288055ae47ae64ecae59b1c48154d0c719411ad13ee11f1cb8c2e07b531c87e27a7c8aaab4f2264
|
data/lib/open_xml_package.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require "open_xml_package/part"
|
2
|
+
require "open_xml_package/rubyzip_fix"
|
2
3
|
require "open_xml_package/version"
|
3
4
|
require "zip"
|
4
5
|
|
@@ -16,6 +17,22 @@ class OpenXmlPackage
|
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
20
|
+
def self.from_stream(stream)
|
21
|
+
stream = StringIO.new(stream) if stream.is_a?(String)
|
22
|
+
|
23
|
+
# Hack: Zip::Entry.read_c_dir_entry initializes
|
24
|
+
# a new Zip::Entry by calling `io.path`. Zip::Entry
|
25
|
+
# uses this to open the original zipfile; but in
|
26
|
+
# this case, the StringIO _is_ the original.
|
27
|
+
def stream.path
|
28
|
+
self
|
29
|
+
end
|
30
|
+
|
31
|
+
zipfile = ::Zip::File.new("", true, true)
|
32
|
+
zipfile.read_from_stream(stream)
|
33
|
+
new(zipfile)
|
34
|
+
end
|
35
|
+
|
19
36
|
def initialize(zipfile=nil)
|
20
37
|
@zipfile = zipfile
|
21
38
|
@parts = []
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require "zip"
|
2
|
+
|
3
|
+
module Zip
|
4
|
+
class InputStream
|
5
|
+
protected
|
6
|
+
|
7
|
+
# The problem in RubyZip 1.1.0 is that we only call `seek`
|
8
|
+
# when `io` is a File. We need to move the cursor to the
|
9
|
+
# right position when `io` is a StringIO as well.
|
10
|
+
def get_io(io, offset = 0)
|
11
|
+
io = ::File.open(io, "rb") unless io.is_a?(IO) || io.is_a?(StringIO)
|
12
|
+
io.seek(offset, ::IO::SEEK_SET)
|
13
|
+
io
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
17
|
+
end
|
@@ -50,7 +50,6 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
|
|
50
50
|
context "Given a sample Word document" do
|
51
51
|
setup do
|
52
52
|
@temp_file = expand_path "./support/sample.docx"
|
53
|
-
@package = OpenXmlPackage.open(temp_file)
|
54
53
|
@expected_contents = Set[
|
55
54
|
"[Content_Types].xml",
|
56
55
|
"_rels/.rels",
|
@@ -68,16 +67,36 @@ class OpenXmlPackageTest < ActiveSupport::TestCase
|
|
68
67
|
"word/webSettings.xml" ]
|
69
68
|
end
|
70
69
|
|
71
|
-
|
72
|
-
|
70
|
+
context ".open" do
|
71
|
+
setup do
|
72
|
+
@package = OpenXmlPackage.open(temp_file)
|
73
|
+
end
|
74
|
+
|
75
|
+
teardown do
|
76
|
+
package.close
|
77
|
+
end
|
78
|
+
|
79
|
+
should "discover the expected parts" do
|
80
|
+
assert_equal @expected_contents, package.parts.map(&:path).to_set
|
81
|
+
end
|
82
|
+
|
83
|
+
should "read their content on-demand" do
|
84
|
+
assert_equal web_settings_content, package.get_part("word/webSettings.xml").content
|
85
|
+
end
|
73
86
|
end
|
74
87
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
88
|
+
context ".from_stream" do
|
89
|
+
setup do
|
90
|
+
@package = OpenXmlPackage.from_stream(File.open(temp_file, "rb", &:read))
|
91
|
+
end
|
92
|
+
|
93
|
+
should "also discover the expected parts" do
|
94
|
+
assert_equal @expected_contents, package.parts.map(&:path).to_set
|
95
|
+
end
|
96
|
+
|
97
|
+
should "read their content" do
|
98
|
+
assert_equal web_settings_content, package.get_part("word/webSettings.xml").content
|
99
|
+
end
|
81
100
|
end
|
82
101
|
end
|
83
102
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: open_xml_package
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bob Lail
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rubyzip
|
@@ -156,6 +156,7 @@ files:
|
|
156
156
|
- Rakefile
|
157
157
|
- lib/open_xml_package.rb
|
158
158
|
- lib/open_xml_package/part.rb
|
159
|
+
- lib/open_xml_package/rubyzip_fix.rb
|
159
160
|
- lib/open_xml_package/version.rb
|
160
161
|
- open_xml_package.gemspec
|
161
162
|
- test/open_xml_package_test.rb
|