Class: Corenlp::Downloader
- Inherits:
-
Object
- Object
- Corenlp::Downloader
- Defined in:
- lib/corenlp/downloader.rb
Instance Attribute Summary collapse
-
#destination ⇒ Object
Returns the value of attribute destination.
-
#local_file ⇒ Object
Returns the value of attribute local_file.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #download ⇒ Object
- #extract ⇒ Object
-
#initialize(url, destination) ⇒ Downloader
constructor
A new instance of Downloader.
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
#destination ⇒ Object
Returns the value of attribute destination.
10 11 12 |
# File 'lib/corenlp/downloader.rb', line 10 def destination @destination end |
#local_file ⇒ Object
Returns the value of attribute local_file.
10 11 12 |
# File 'lib/corenlp/downloader.rb', line 10 def local_file @local_file end |
#url ⇒ Object
Returns the value of attribute url.
10 11 12 |
# File 'lib/corenlp/downloader.rb', line 10 def url @url end |
Instance Method Details
#download ⇒ Object
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 |
#extract ⇒ Object
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 |