Class: Downloader

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

Overview

A simple class to download files to a given directory. Expects details for the filename and a link. One Downloader should be created / show being downloaded. Downloaders can run on seperate threads for getting many hows at once.

Instance Method Summary collapse

Constructor Details

#initialize(path, format) ⇒ Downloader

Returns a new instance of Downloader.



4
5
6
7
# File 'lib/deadlist/cli/downloader.rb', line 4

def initialize(path, format)
    @path = path
    @format = format
end

Instance Method Details

#get(pos, name, link) ⇒ Object

Goes to a link (assuming the format is already validated), and gets the file, saving with argument names.



10
11
12
13
14
15
16
17
18
# File 'lib/deadlist/cli/downloader.rb', line 10

def get(pos, name, link)
    uri = URI.parse(link); raise ArgumentError, "Only HTTP(S) URLs allowed" unless uri.is_a?(URI::HTTP)
        
    download = uri.open
    filename = "#{@path}/#{pos} -- #{name}.#{@format}"
    IO.copy_stream(download, filename)
rescue => e
    puts "❌ Download failed: #{e.message}"
end