Skip to content

Commit

Permalink
refactor: add #download_kit method
Browse files Browse the repository at this point in the history
Add #download_kit method for better separation
  • Loading branch information
ninoseki committed Jun 5, 2019
1 parent d745746 commit 0d73e10
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions lib/miteru/downloader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@ class Downloader

def initialize(base_dir = "/tmp")
@base_dir = base_dir
raise ArgumentError, "#{base_dir} is not existing." unless Dir.exist?(base_dir)
raise ArgumentError, "#{base_dir} is not exist." unless Dir.exist?(base_dir)
end

def download_kits(kits)
kits.each do |kit|
filename = download_filename(kit)
destination = filepath_to_download(filename)
begin
downloaded_filepath = HTTPClient.download(kit.url, destination)
if duplicated?(downloaded_filepath)
puts "Do not download #{kit.url} because there is a file that has a same hash value in the directory (SHA256: #{sha256(downloaded_filepath)})."
FileUtils.rm downloaded_filepath
else
puts "Download #{kit.url} as #{downloaded_filepath}"
end
rescue Down::Error => e
puts "Failed to download: #{kit.url} (#{e})"
end
end
kits.each { |kit| download_kit kit }
end

private

def download_kit(kit)
filename = download_filename(kit)
destination = filepath_to_download(filename)
begin
downloaded_filepath = HTTPClient.download(kit.url, destination)
if duplicated?(downloaded_filepath)
puts "Do not download #{kit.url} because there is a duplicate file in the directory (SHA256: #{sha256(downloaded_filepath)})."
FileUtils.rm downloaded_filepath
else
puts "Download #{kit.url} as #{downloaded_filepath}"
end
rescue Down::Error => e
puts "Failed to download: #{kit.url} (#{e})"
end
end

def download_filename(kit)
domain = URI(kit.base_url).hostname

Expand Down

0 comments on commit 0d73e10

Please sign in to comment.