Class: ZipGeoJp::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/zip_geo_jp/downloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, local_dir) ⇒ Downloader

Returns a new instance of Downloader.



8
9
10
11
# File 'lib/zip_geo_jp/downloader.rb', line 8

def initialize(url, local_dir)
  @url       = url
  @local_dir = local_dir
end

Instance Attribute Details

#local_dirObject

Returns the value of attribute local_dir.



6
7
8
# File 'lib/zip_geo_jp/downloader.rb', line 6

def local_dir
  @local_dir
end

#urlObject

Returns the value of attribute url.



6
7
8
# File 'lib/zip_geo_jp/downloader.rb', line 6

def url
  @url
end

Instance Method Details

#downloadObject



29
30
31
32
33
34
35
36
# File 'lib/zip_geo_jp/downloader.rb', line 29

def download
  FileUtils.mkdir_p(@local_dir)
  open(@url) do |stream|
    open(local, 'w+b') do |file|
      file.write(stream.read)
    end
  end
end

#downloaded?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/zip_geo_jp/downloader.rb', line 25

def downloaded?
  File.exist? local
end

#filenameObject



13
14
15
# File 'lib/zip_geo_jp/downloader.rb', line 13

def filename
  @filename ||= URI.parse(url).path.to_s.split('/').last
end

#localObject



17
18
19
# File 'lib/zip_geo_jp/downloader.rb', line 17

def local
  File.join(@local_dir, filename)
end

#removeObject



21
22
23
# File 'lib/zip_geo_jp/downloader.rb', line 21

def remove
  FileUtils.remove_file local if File.exist? local
end