Module: SpatialFeatures::Download

Defined in:
lib/spatial_features/download.rb

Class Method Summary collapse

Class Method Details

.entries(file) ⇒ Object



39
40
41
42
43
# File 'lib/spatial_features/download.rb', line 39

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

.find_in_zip(file, find:, **unzip_options) ⇒ Object



45
46
47
# File 'lib/spatial_features/download.rb', line 45

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

.normalize_file(file) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/spatial_features/download.rb', line 31

def self.normalize_file(file)
  Tempfile.new.tap do |temp|
    temp.binmode
    temp.write(file.read)
    temp.rewind
  end
end

.open(file) ⇒ Object

file can be a url, path, or file, any of which can return be a zipped archive



13
14
15
16
17
# File 'lib/spatial_features/download.rb', line 13

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

.open_each(file, unzip: nil, **unzip_options) ⇒ Object

file can be a url, path, or file, any of which can return be a zipped archive



20
21
22
23
24
25
26
27
28
29
# File 'lib/spatial_features/download.rb', line 20

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

.read(file, unzip: nil, **unzip_options) ⇒ Object

file can be a url, path, or file, any of which can return be a zipped archive



6
7
8
9
10
# File 'lib/spatial_features/download.rb', line 6

def self.read(file, unzip: nil, **unzip_options)
  file = Download.open_each(file, unzip: unzip, **unzip_options).first
  path = ::File.path(file)
  return ::File.read(path)
end