Skip to content

Commit 04a8df2

Browse files
Simon Grondinpythonicrubyist
authored andcommitted
All the fixes from the Creek forks out there, and more (#47)
* Update nokogiri version Needed to upgrade to Ruby 2.4 * Updated rspec version so tests can pass. * Change to use rspec 3 syntax * Convert specs to RSpec 3.6.0 syntax with Transpec This conversion is done by Transpec 3.3.0 with the following command: transpec * 25 conversions from: obj.should to: expect(obj).to * 22 conversions from: == expected to: eq(expected) * 1 conversion from: obj.should_not to: expect(obj).not_to For more details: https://github.com/yujinakayama/transpec#supported-conversions * Added logic to contend with the default namespace having a prefix Added logic to contend with the default namespace having a prefix. Only addressed on the 'book' * Handle sheets files already containing xl/ prefix Sheets generated by certain libraries already prefix xl/ * Bump httparty to 0.15 and Ruby to >= 2.0.0 Plenty of bugfixes were committed to httparty since 0.14 Some of those fixes address very serious bugs and memory leaks Httparty 0.15 requires Ruby >= 2.0.0, and now so does this gem * support for inlineStr cell type
1 parent d9b3df7 commit 04a8df2

7 files changed

Lines changed: 59 additions & 41 deletions

File tree

creek.gemspec

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ Gem::Specification.new do |spec|
1818
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
1919
spec.require_paths = ["lib"]
2020

21-
spec.required_ruby_version = '>= 1.9.2'
21+
spec.required_ruby_version = '>= 2.0.0'
2222

2323
spec.add_development_dependency "bundler", "~> 1.3"
2424
spec.add_development_dependency "rake"
25-
spec.add_development_dependency 'rspec', '~> 2.13.0'
25+
spec.add_development_dependency 'rspec', '~> 3.6.0'
2626
spec.add_development_dependency 'pry'
2727

28-
spec.add_dependency 'nokogiri', '~> 1.6.0'
28+
spec.add_dependency 'nokogiri', '~> 1.7.0'
2929
spec.add_dependency 'rubyzip', '>= 1.0.0'
30-
spec.add_dependency 'httparty', '~> 0.14.0'
30+
spec.add_dependency 'httparty', '~> 0.15.5'
3131
end

lib/creek/book.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,18 @@ def initialize path, options = {}
3434
def sheets
3535
doc = @files.file.open "xl/workbook.xml"
3636
xml = Nokogiri::XML::Document.parse doc
37+
namespaces = xml.namespaces
38+
39+
cssPrefix = ''
40+
namespaces.each do |namespace|
41+
if namespace[1] == 'http://schemas.openxmlformats.org/spreadsheetml/2006/main' && namespace[0] != 'xmlns' then
42+
cssPrefix = namespace[0].split(':')[1]+'|'
43+
end
44+
end
45+
3746
rels_doc = @files.file.open "xl/_rels/workbook.xml.rels"
3847
rels = Nokogiri::XML::Document.parse(rels_doc).css("Relationship")
39-
@sheets = xml.css('sheet').map do |sheet|
48+
@sheets = xml.css(cssPrefix+'sheet').map do |sheet|
4049
sheetfile = rels.find { |el| sheet.attr("r:id") == el.attr("Id") }.attr("Target")
4150
Sheet.new(self, sheet.attr("name"), sheet.attr("sheetid"), sheet.attr("state"), sheet.attr("visible"), sheet.attr("r:id"), sheetfile)
4251
end

lib/creek/sheet.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def rows_with_meta_data
6666
# Returns a hash per row that includes the cell ids and values.
6767
# Empty cells will be also included in the hash with a nil value.
6868
def rows_generator include_meta_data=false
69-
path = "xl/#{@sheetfile}"
69+
path = if @sheetfile.start_with? "/xl/" or @sheetfile.start_with? "xl/" then @sheetfile else "xl/#{@sheetfile}" end
7070
if @book.files.file.exist?(path)
7171
# SAX parsing, Each element in the stream comes through as two events:
7272
# one to open the element and one to close it.
@@ -103,6 +103,10 @@ def rows_generator include_meta_data=false
103103
unless cell.nil?
104104
cells[cell] = convert(node.inner_xml, cell_type, cell_style_idx)
105105
end
106+
elsif (node.name.eql? 't') and (node.node_type.eql? opener)
107+
unless cell.nil?
108+
cells[cell] = convert(node.inner_xml, cell_type, cell_style_idx)
109+
end
106110
end
107111
end
108112
end

spec/shared_string_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
doc = Nokogiri::XML(shared_strings_xml_file)
88
dictionary = Creek::SharedStrings.parse_shared_string_from_document(doc)
99

10-
dictionary.keys.size.should == 5
11-
dictionary[0].should == 'Cell A1'
12-
dictionary[1].should == 'Cell B1'
13-
dictionary[2].should == 'My Cell'
14-
dictionary[3].should == 'Cell A2'
15-
dictionary[4].should == 'Cell B2'
10+
expect(dictionary.keys.size).to eq(5)
11+
expect(dictionary[0]).to eq('Cell A1')
12+
expect(dictionary[1]).to eq('Cell B1')
13+
expect(dictionary[2]).to eq('My Cell')
14+
expect(dictionary[3]).to eq('Cell A2')
15+
expect(dictionary[4]).to eq('Cell B2')
1616
end
1717

1818
end

spec/styles/converter_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def convert(value, type, style)
99

1010
describe :date_time do
1111
it "works" do
12-
convert('41275', 'n', :date_time).should == Date.new(2013,01,01)
12+
expect(convert('41275', 'n', :date_time)).to eq(Date.new(2013,01,01))
1313
end
1414
end
1515
end

spec/styles/style_types_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
xml_file = File.open('spec/fixtures/styles/first.xml')
88
doc = Nokogiri::XML(xml_file)
99
res = Creek::Styles::StyleTypes.new(doc).call
10-
res.size.should == 8
11-
res[3].should == :date_time
12-
res.should == [:unsupported, :unsupported, :unsupported, :date_time, :unsupported, :unsupported, :unsupported, :unsupported]
10+
expect(res.size).to eq(8)
11+
expect(res[3]).to eq(:date_time)
12+
expect(res).to eq([:unsupported, :unsupported, :unsupported, :date_time, :unsupported, :unsupported, :unsupported, :unsupported])
1313
end
1414
end
1515
end

spec/test_spec.rb

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,38 @@
22

33
describe 'Creek trying to parsing an invalid file.' do
44
it 'Fail to open a legacy xls file.' do
5-
lambda { Creek::Book.new 'spec/fixtures/invalid.xls' }.should raise_error 'Not a valid file format.'
5+
expect { Creek::Book.new 'spec/fixtures/invalid.xls' }
6+
.to raise_error 'Not a valid file format.'
67
end
78

89
it 'Ignore file extensions on request.' do
910
path = 'spec/fixtures/sample-as-zip.zip'
10-
lambda { Creek::Book.new path, :check_file_extension => false }.should_not raise_error
11+
expect { Creek::Book.new path, check_file_extension: false }
12+
.not_to raise_error
1113
end
1214

1315
it 'Check file extension when requested.' do
14-
open_book = lambda { Creek::Book.new 'spec/fixtures/invalid.xls', :check_file_extension => true }
15-
open_book.should raise_error 'Not a valid file format.'
16+
expect { Creek::Book.new 'spec/fixtures/invalid.xls', check_file_extension: true }
17+
.to raise_error 'Not a valid file format.'
1618
end
1719

1820
it 'Fail to open remote file' do
19-
lambda { Creek::Book.new 'http://dev-builds.libreoffice.org/tmp/test.xlsx' }.should raise_error
21+
expect { Creek::Book.new 'http://dev-builds.libreoffice.org/tmp/test.xlsx' }
22+
.to raise_error(Zip::Error, /not found/)
2023
end
2124

2225
it 'Opens remote file with remote flag' do
23-
lambda { Creek::Book.new 'http://dev-builds.libreoffice.org/tmp/test.xlsx', remote: true }.should_not raise_error
26+
expect { Creek::Book.new 'http://dev-builds.libreoffice.org/tmp/test.xlsx', remote: true }
27+
.not_to raise_error
2428
end
2529

2630
it 'Check file extension of original_filename if passed.' do
2731
path = 'spec/fixtures/temp_string_io_file_path_with_no_extension'
28-
lambda { Creek::Book.new path, :original_filename => 'invalid.xls' }.should raise_error 'Not a valid file format.'
29-
lambda { Creek::Book.new path, :original_filename => 'valid.xlsx' }.should_not raise_error
32+
expect { Creek::Book.new path, :original_filename => 'invalid.xls' }
33+
.to raise_error 'Not a valid file format.'
34+
expect { Creek::Book.new path, :original_filename => 'valid.xlsx' }
35+
.not_to raise_error
3036
end
31-
3237
end
3338

3439
describe 'Creek parsing a sample XLSX file' do
@@ -49,15 +54,15 @@
4954
end
5055

5156
it 'open an XLSX file successfully.' do
52-
@creek.should_not be_nil
57+
expect(@creek).not_to be_nil
5358
end
5459

5560
it 'find sheets successfully.' do
56-
@creek.sheets.count.should == 1
61+
expect(@creek.sheets.count).to eq(1)
5762
sheet = @creek.sheets.first
58-
sheet.state.should eql nil
59-
sheet.name.should eql 'Sheet1'
60-
sheet.rid.should eql 'rId1'
63+
expect(sheet.state).to eql nil
64+
expect(sheet.name).to eql 'Sheet1'
65+
expect(sheet.rid).to eql 'rId1'
6166
end
6267

6368
it 'Parse rows with empty cells successfully.' do
@@ -68,16 +73,16 @@
6873
row_count += 1
6974
end
7075

71-
rows[0].should == @expected_rows[0]
72-
rows[1].should == @expected_rows[1]
73-
rows[2].should == @expected_rows[2]
74-
rows[3].should == @expected_rows[3]
75-
rows[4].should == @expected_rows[4]
76-
rows[5].should == @expected_rows[5]
77-
rows[6].should == @expected_rows[6]
78-
rows[7].should == @expected_rows[7]
79-
rows[8].should == @expected_rows[8]
80-
row_count.should == 9
76+
expect(rows[0]).to eq(@expected_rows[0])
77+
expect(rows[1]).to eq(@expected_rows[1])
78+
expect(rows[2]).to eq(@expected_rows[2])
79+
expect(rows[3]).to eq(@expected_rows[3])
80+
expect(rows[4]).to eq(@expected_rows[4])
81+
expect(rows[5]).to eq(@expected_rows[5])
82+
expect(rows[6]).to eq(@expected_rows[6])
83+
expect(rows[7]).to eq(@expected_rows[7])
84+
expect(rows[8]).to eq(@expected_rows[8])
85+
expect(row_count).to eq(9)
8186
end
8287

8388
it 'Parse rows with empty cells and meta data successfully.' do
@@ -87,6 +92,6 @@
8792
rows << row
8893
row_count += 1
8994
end
90-
rows.map{|r| r['cells']}.should == @expected_rows
95+
expect(rows.map{|r| r['cells']}).to eq(@expected_rows)
9196
end
9297
end

0 commit comments

Comments
 (0)