Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions creek.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 17 additions & 8 deletions lib/creek/book.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'zip/filesystem'
require 'nokogiri'
require 'date'
require 'http'
require 'open-uri'

module Creek

Expand All @@ -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
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require 'creek'
require 'pry'
require 'time'

RSpec.configure do |config|
config.filter_run_excluding remote: true
end
14 changes: 14 additions & 0 deletions spec/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down