diff --git a/README.md b/README.md index ea287db..b64d4b5 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,12 @@ Once this is complete, you should be able to run the test suite: rake ``` +There are some remote tests that are excluded by default. To run those, run + +``` +bundle exec rspec --tag remote +``` + ## Bug Reporting Please use the [Issues](https://github.com/pythonicrubyist/creek/issues) page to report bugs or suggest new enhancements. diff --git a/creek.gemspec b/creek.gemspec index 7ad309f..e261388 100644 --- a/creek.gemspec +++ b/creek.gemspec @@ -23,9 +23,8 @@ Gem::Specification.new do |spec| spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" spec.add_development_dependency 'rspec', '~> 3.6.0' - spec.add_development_dependency 'pry' + spec.add_development_dependency 'pry-byebug' spec.add_dependency 'nokogiri', '>= 1.7.0' spec.add_dependency 'rubyzip', '>= 1.0.0' - spec.add_dependency 'http', '~> 4.0' end diff --git a/lib/creek/book.rb b/lib/creek/book.rb index 613cac6..061b935 100644 --- a/lib/creek/book.rb +++ b/lib/creek/book.rb @@ -1,7 +1,7 @@ require 'zip/filesystem' require 'nokogiri' require 'date' -require 'http' +require 'open-uri' module Creek @@ -20,13 +20,7 @@ def initialize path, options = {} extension = File.extname(options[:original_filename] || path).downcase raise 'Not a valid file format.' unless (['.xlsx', '.xlsm'].include? extension) end - if options[:remote] - zipfile = Tempfile.new("file") - zipfile.binmode - zipfile.write(HTTP.get(path).to_s) - zipfile.close - path = zipfile.path - end + path = download_file(path) if options[:remote] @files = Zip::File.open(path) @shared_strings = SharedStrings.new(self) end @@ -79,5 +73,20 @@ def base_date result end end + + private + + def download_file(url) + # OpenUri will return a StringIO if under OpenURI::Buffer::StringMax + # threshold, and a Tempfile if over. + downloaded = URI(url).open + if downloaded.is_a? StringIO + path = Tempfile.new(['creek-file', '.xlsx']).path + File.binwrite(path, downloaded.read) + path + else + downloaded.path + end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 13f918c..ed04310 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,7 @@ require 'creek' require 'pry' +require 'time' +RSpec.configure do |config| + config.filter_run_excluding remote: true +end diff --git a/spec/test_spec.rb b/spec/test_spec.rb index 2ba1ebb..dabb932 100644 --- a/spec/test_spec.rb +++ b/spec/test_spec.rb @@ -106,6 +106,20 @@ expect(@creek).not_to be_nil end + it 'opens small remote files successfully', remote: true do + url = 'https://file-examples.com/wp-content/uploads/2017/02/file_example_XLSX_10.xlsx' + @creek = Creek::Book.new(url, remote: true) + + expect(@creek.sheets[0]).to be_a Creek::Sheet + end + + it 'opens large remote files successfully', remote: true do + url = 'http://www.house.leg.state.mn.us/comm/docs/BanaianZooExample.xlsx' + @creek = Creek::Book.new(url, remote: true) + + expect(@creek.sheets[0]).to be_a Creek::Sheet + end + it 'find sheets successfully.' do expect(@creek.sheets.count).to eq(1) sheet = @creek.sheets.first