Sha256: fdc01448807bfecb1ee96f89f7194ddf34dc3f4fa22687513d15d0ba6dbe4211

Contents?: true

Size: 1.07 KB

Versions: 12

Compression:

Stored size: 1.07 KB

Contents

require 'open-uri'

module SpatialFeatures
  module Download
    # file can be a url, path, or file, any of which can return be a zipped archive
    def self.open(file)
      file = URI.open(file)
      file = normalize_file(file) if file.is_a?(StringIO)
      return file
    end

    # file can be a url, path, or file, any of which can return be a zipped archive
    def self.open_each(path_or_url, unzip: nil, **unzip_options)
      file = Download.open(path_or_url)
      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
      end
    end

    def self.entries(file)
      file = Kernel.open(file)
      file = normalize_file(file) if file.is_a?(StringIO)
      Unzip.entries(file)
    end

    def self.find_in_zip(file, find:, **unzip_options)
      Unzip.paths(file, :find => find, **unzip_options)
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spatial_features-3.8.0 lib/spatial_features/download.rb
spatial_features-3.6.3 lib/spatial_features/download.rb
spatial_features-3.6.2 lib/spatial_features/download.rb
spatial_features-3.6.1 lib/spatial_features/download.rb
spatial_features-3.6.0 lib/spatial_features/download.rb
spatial_features-3.5.1 lib/spatial_features/download.rb
spatial_features-3.4.8 lib/spatial_features/download.rb
spatial_features-3.4.2 lib/spatial_features/download.rb
spatial_features-3.4.1 lib/spatial_features/download.rb
spatial_features-3.4.0 lib/spatial_features/download.rb
spatial_features-3.3.0 lib/spatial_features/download.rb
spatial_features-3.2.0 lib/spatial_features/download.rb