lib/spatial_features/download.rb in spatial_features-2.17.2 vs lib/spatial_features/download.rb in spatial_features-2.17.3
- old
+ new
@@ -2,24 +2,34 @@
module SpatialFeatures
module Download
# file can be a url, path, or file, any of which can return be a zipped archive
def self.read(file, unzip: nil, **unzip_options)
- file = open(file, unzip: unzip, **unzip_options)
+ file = Download.open_each(file, unzip: unzip, **unzip_options).first
path = ::File.path(file)
return ::File.read(path)
end
- def self.open(file, unzip: nil, **unzip_options)
+ # file can be a url, path, or file, any of which can return be a zipped archive
+ def self.open(file)
file = Kernel.open(file)
file = normalize_file(file) if file.is_a?(StringIO)
- if unzip && Unzip.is_zip?(file)
- file = find_in_zip(file, find: unzip, **unzip_options)
- end
return file
end
+ # file can be a url, path, or file, any of which can return be a zipped archive
+ def self.open_each(file, unzip: nil, **unzip_options)
+ file = Download.open(file)
+ files = if unzip && Unzip.is_zip?(file)
+ find_in_zip(file, find: unzip, **unzip_options)
+ else
+ [file]
+ end
+
+ return files.map { |f| File.open(f) }
+ end
+
def self.normalize_file(file)
Tempfile.new.tap do |temp|
temp.binmode
temp.write(file.read)
temp.rewind
@@ -31,9 +41,9 @@
file = normalize_file(file) if file.is_a?(StringIO)
Unzip.entries(file)
end
def self.find_in_zip(file, find:, **unzip_options)
- return File.open(Unzip.paths(file, :find => find, **unzip_options))
+ Unzip.paths(file, :find => find, **unzip_options)
end
end
end