Module: LeechtopDownloader::CLIHelpers
Overview
CLIHelpers provides shared utility methods for CLI operations
Instance Method Summary collapse
- #acquire_lock(lock_path) ⇒ Object
- #fix_encoding(name) ⇒ Object
- #lock_path_for(url, filename_hint, destination) ⇒ Object
- #skip_download?(filename_hint, skip_existing, destination) ⇒ Boolean
- #skip_locked(filename_hint) ⇒ Object
- #with_app_lock(&blk) ⇒ Object
Instance Method Details
#acquire_lock(lock_path) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 59 def acquire_lock(lock_path) File.new(lock_path, File::WRONLY | File::CREAT | File::EXCL).close true rescue Errno::EEXIST false end |
#fix_encoding(name) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 78 def fix_encoding(name) fixed = name.encode('iso-8859-1').force_encoding('utf-8') fixed.valid_encoding? ? fixed : name rescue EncodingError name end |
#lock_path_for(url, filename_hint, destination) ⇒ Object
50 51 52 53 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 50 def lock_path_for(url, filename_hint, destination) lock_filename = filename_hint.empty? ? "#{Digest::MD5.hexdigest(url)}.lock" : "#{filename_hint}.lock" File.join(destination, lock_filename) end |
#skip_download?(filename_hint, skip_existing, destination) ⇒ Boolean
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 33 def skip_download?(filename_hint, skip_existing, destination) return false unless skip_existing && !filename_hint.empty? if File.exist?(File.join(destination, filename_hint)) Kernel.puts "File #{filename_hint} already exists. Skipping." true else false end end |
#skip_locked(filename_hint) ⇒ Object
69 70 71 72 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 69 def skip_locked(filename_hint) name = filename_hint.empty? ? 'File' : "File #{filename_hint}" Kernel.puts "#{name} is currently being downloaded by another process. Skipping." end |
#with_app_lock(&blk) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/leechtop_downloader/cli_helpers.rb', line 15 def with_app_lock(&blk) lock_path = File.join(Dir.tmpdir, 'leechtop_downloader.lock') File.open(lock_path, 'w') do |file| unless file.flock(File::LOCK_EX | File::LOCK_NB) Kernel.puts 'Error: Another instance of leechtop downloader is already running.' Kernel.exit(1) end blk.call end end |