Skip to content

Commit f787567

Browse files
dbernheiselpythonicrubyist
authored andcommitted
Remove http requirement (#87)
1 parent 6dad61b commit f787567

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ Once this is complete, you should be able to run the test suite:
118118
rake
119119
```
120120

121+
There are some remote tests that are excluded by default. To run those, run
122+
123+
```
124+
bundle exec rspec --tag remote
125+
```
126+
121127
## Bug Reporting
122128

123129
Please use the [Issues](https://github.com/pythonicrubyist/creek/issues) page to report bugs or suggest new enhancements.

creek.gemspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ Gem::Specification.new do |spec|
2323
spec.add_development_dependency "bundler", "~> 1.3"
2424
spec.add_development_dependency "rake"
2525
spec.add_development_dependency 'rspec', '~> 3.6.0'
26-
spec.add_development_dependency 'pry'
26+
spec.add_development_dependency 'pry-byebug'
2727

2828
spec.add_dependency 'nokogiri', '>= 1.7.0'
2929
spec.add_dependency 'rubyzip', '>= 1.0.0'
30-
spec.add_dependency 'http', '~> 4.0'
3130
end

lib/creek/book.rb

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'zip/filesystem'
22
require 'nokogiri'
33
require 'date'
4-
require 'http'
4+
require 'open-uri'
55

66
module Creek
77

@@ -20,13 +20,7 @@ def initialize path, options = {}
2020
extension = File.extname(options[:original_filename] || path).downcase
2121
raise 'Not a valid file format.' unless (['.xlsx', '.xlsm'].include? extension)
2222
end
23-
if options[:remote]
24-
zipfile = Tempfile.new("file")
25-
zipfile.binmode
26-
zipfile.write(HTTP.get(path).to_s)
27-
zipfile.close
28-
path = zipfile.path
29-
end
23+
path = download_file(path) if options[:remote]
3024
@files = Zip::File.open(path)
3125
@shared_strings = SharedStrings.new(self)
3226
end
@@ -79,5 +73,20 @@ def base_date
7973
result
8074
end
8175
end
76+
77+
private
78+
79+
def download_file(url)
80+
# OpenUri will return a StringIO if under OpenURI::Buffer::StringMax
81+
# threshold, and a Tempfile if over.
82+
downloaded = URI(url).open
83+
if downloaded.is_a? StringIO
84+
path = Tempfile.new(['creek-file', '.xlsx']).path
85+
File.binwrite(path, downloaded.read)
86+
path
87+
else
88+
downloaded.path
89+
end
90+
end
8291
end
8392
end

spec/spec_helper.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
require 'creek'
22
require 'pry'
3+
require 'time'
34

5+
RSpec.configure do |config|
6+
config.filter_run_excluding remote: true
7+
end

spec/test_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@
106106
expect(@creek).not_to be_nil
107107
end
108108

109+
it 'opens small remote files successfully', remote: true do
110+
url = 'https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx'
111+
@creek = Creek::Book.new(url, remote: true)
112+
113+
expect(@creek.sheets[0]).to be_a Creek::Sheet
114+
end
115+
116+
it 'opens large remote files successfully', remote: true do
117+
url = 'http://www.house.leg.state.mn.us/comm/docs/BanaianZooExample.xlsx'
118+
@creek = Creek::Book.new(url, remote: true)
119+
120+
expect(@creek.sheets[0]).to be_a Creek::Sheet
121+
end
122+
109123
it 'find sheets successfully.' do
110124
expect(@creek.sheets.count).to eq(1)
111125
sheet = @creek.sheets.first

0 commit comments

Comments
 (0)