Class: Yotsuba::Download
- Inherits:
-
Object
- Object
- Yotsuba::Download
- Includes:
- Concurrent::Async
- Defined in:
- lib/yotsuba/download.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #abort ⇒ Object
- #bytes_written ⇒ Object
- #delete ⇒ Object
-
#initialize(options = {animefile: nil, output_dir: "." }) ⇒ Download
constructor
A new instance of Download.
- #percent_downloaded ⇒ Object
- #run ⇒ Object
- #run_async ⇒ Object
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( = {animefile: nil, output_dir: "." }) @file = [:animefile] @output_dir = File.absolute_path([:output_dir]) @multiple = (@file.download_links.length > 1) if @file @status = "Queued" init_mutex # Required by Concurrent::Async end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
5 6 7 |
# File 'lib/yotsuba/download.rb', line 5 def file @file end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
5 6 7 |
# File 'lib/yotsuba/download.rb', line 5 def id @id end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
5 6 7 |
# File 'lib/yotsuba/download.rb', line 5 def status @status end |
Instance Method Details
#abort ⇒ Object
36 37 38 39 |
# File 'lib/yotsuba/download.rb', line 36 def abort finish_request @status = "Aborted" end |
#bytes_written ⇒ Object
47 48 49 |
# File 'lib/yotsuba/download.rb', line 47 def bytes_written @path ? File.size(@path) : 0 end |
#delete ⇒ Object
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_downloaded ⇒ Object
51 52 53 |
# File 'lib/yotsuba/download.rb', line 51 def percent_downloaded 100.0 * self.bytes_written / self.file.size end |
#run ⇒ Object
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_async ⇒ Object
32 33 34 |
# File 'lib/yotsuba/download.rb', line 32 def run_async self.async.run end |