Class: Yotsuba::Download

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/yotsuba/download.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {animefile: nil, output_dir: "." }) ⇒ Download

Returns a new instance of Download.



7
8
9
10
11
12
13
14
# File 'lib/yotsuba/download.rb', line 7

def initialize(options = {animefile: nil, output_dir: "." })
  @file = options[:animefile]
  @output_dir = File.absolute_path(options[:output_dir])
  @multiple = (@file.download_links.length > 1) if @file
  @status = "Queued"

  init_mutex # Required by Concurrent::Async
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/yotsuba/download.rb', line 5

def file
  @file
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/yotsuba/download.rb', line 5

def id
  @id
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/yotsuba/download.rb', line 5

def status
  @status
end

Instance Method Details

#abortObject



36
37
38
39
# File 'lib/yotsuba/download.rb', line 36

def abort
  finish_request
  @status = "Aborted"
end

#bytes_writtenObject



47
48
49
# File 'lib/yotsuba/download.rb', line 47

def bytes_written
  @path ? File.size(@path) : 0
end

#deleteObject



41
42
43
44
45
# File 'lib/yotsuba/download.rb', line 41

def delete
  abort if @status != "Aborted"
  File.delete(@path) if @path
  return true
end

#percent_downloadedObject



51
52
53
# File 'lib/yotsuba/download.rb', line 51

def percent_downloaded
  100.0 * self.bytes_written / self.file.size
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/yotsuba/download.rb', line 16

def run
  @path = @multiple ? File.join(@output_dir, @file.name+".zip") : @path = File.join(@output_dir, @file.name)

  FileUtils.mkdir_p @output_dir

  if File.exists? @path
    @status = "File exists"
    return
  end

  @file.download_links.each do |link|
    create_request(link).run
  end
  finish_request
end

#run_asyncObject



32
33
34
# File 'lib/yotsuba/download.rb', line 32

def run_async
  self.async.run
end