Class: XiamiRadio::Downloader
- Inherits:
-
Object
- Object
- XiamiRadio::Downloader
- Defined in:
- lib/xiami_radio/downloader.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#track ⇒ Object
readonly
Returns the value of attribute track.
Class Method Summary collapse
Instance Method Summary collapse
- #filename ⇒ Object
-
#initialize(track) ⇒ Downloader
constructor
A new instance of Downloader.
- #progress ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(track) ⇒ Downloader
Returns a new instance of Downloader.
15 16 17 |
# File 'lib/xiami_radio/downloader.rb', line 15 def initialize(track) @track = track end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
13 14 15 |
# File 'lib/xiami_radio/downloader.rb', line 13 def file @file end |
#track ⇒ Object (readonly)
Returns the value of attribute track.
13 14 15 |
# File 'lib/xiami_radio/downloader.rb', line 13 def track @track end |
Class Method Details
.circulator ⇒ Object
6 7 8 9 10 |
# File 'lib/xiami_radio/downloader.rb', line 6 def circulator @circulator ||= Queue.new %w(甲 乙).map(&@circulator.method(:push)) if @circulator.empty? @circulator end |
Instance Method Details
#filename ⇒ Object
19 20 21 |
# File 'lib/xiami_radio/downloader.rb', line 19 def filename File.join XiamiRadio::TMP_DIR, self.class.circulator.pop(true) end |
#progress ⇒ Object
23 24 25 |
# File 'lib/xiami_radio/downloader.rb', line 23 def progress @progress.nil? ? 0.00 : (@progress.to_f / @total.to_f).round(2) end |
#start ⇒ Object
27 28 29 |
# File 'lib/xiami_radio/downloader.rb', line 27 def start @thread ||= Thread.start { request URI(@track.location) } end |
#stop ⇒ Object
31 32 33 34 35 |
# File 'lib/xiami_radio/downloader.rb', line 31 def stop @thread&.exit @thread = nil File.delete(@file) if @file end |