Class: XiamiRadio::Downloader

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/xiami_radio/downloader.rb', line 13

def file
  @file
end

#trackObject (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

.circulatorObject



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

#filenameObject



19
20
21
# File 'lib/xiami_radio/downloader.rb', line 19

def filename
  File.join XiamiRadio::TMP_DIR, self.class.circulator.pop(true)
end

#progressObject



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

#startObject



27
28
29
# File 'lib/xiami_radio/downloader.rb', line 27

def start
  @thread ||= Thread.start { request URI(@track.location) }
end

#stopObject



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