Class: Corenlp::Downloader

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, destination) ⇒ Downloader

Returns a new instance of Downloader.



11
12
13
14
15
# File 'lib/corenlp/downloader.rb', line 11

def initialize(url, destination)
  self.url = url
  self.destination = destination
  self.local_file = nil
end

Instance Attribute Details

#destinationObject

Returns the value of attribute destination.



10
11
12
# File 'lib/corenlp/downloader.rb', line 10

def destination
  @destination
end

#local_fileObject

Returns the value of attribute local_file.



10
11
12
# File 'lib/corenlp/downloader.rb', line 10

def local_file
  @local_file
end

#urlObject

Returns the value of attribute url.



10
11
12
# File 'lib/corenlp/downloader.rb', line 10

def url
  @url
end

Instance Method Details

#downloadObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/corenlp/downloader.rb', line 41

def download
  return unless url
  puts "downloading zip file from url #{url}. Extracting files to #{destination}..."
  self.local_file = File.basename(url)
  uri = URI.parse(url)
  if local_file && uri
    Net::HTTP.start(uri.host) do |http|
      resp = http.get(uri.request_uri)
      open(local_file, "wb") do |file|
        file.write(resp.body)
      end
    end
    puts "done. Downloaded file #{local_file}."
    extract
  end
end

#extractObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/corenlp/downloader.rb', line 17

def extract
  puts "extracting file..."
  Zip::File.open(local_file) do |zip_file|
    zip_file.each do |file|
      file_path = File.join(destination, file.name)
      zip_file.extract(file, file_path) unless File.exist?(file_path)
    end

    puts "moving files into directory..."
    dirname = local_file[0...-4]
    dir = File.join(destination, dirname)
    if File.exists?(dir)
      Dir.glob(File.join(dir, "*")).each do |file|
        FileUtils.mv(file, File.join(destination, File.basename(file)))
      end
      FileUtils.rm_rf(dir)
    end

    puts "deleting original zip file..."
    FileUtils.rm(local_file)
    puts "done."
  end
end