Class: Leecher::Status
- Inherits:
-
Object
- Object
- Leecher::Status
- Defined in:
- lib/leecher/status.rb
Instance Attribute Summary collapse
-
#aria2_client ⇒ Object
Returns the value of attribute aria2_client.
Instance Method Summary collapse
- #active ⇒ Object
- #aria2_status ⇒ Object
- #aria2_version ⇒ Object
- #calc_diskfree ⇒ Object
- #calc_downspeed ⇒ Object
- #changed?(previous, diskchange_threshold = 1048576) ⇒ Boolean
- #diskfree ⇒ Object
- #downspeed ⇒ Object
-
#initialize(aria2_client) ⇒ Status
constructor
A new instance of Status.
- #significant_diskchange?(now, before, diskchange_threshold = 1048576) ⇒ Boolean
- #to_params ⇒ Object
- #waiting ⇒ Object
Constructor Details
#initialize(aria2_client) ⇒ Status
Returns a new instance of Status.
6 7 8 |
# File 'lib/leecher/status.rb', line 6 def initialize(aria2_client) @aria2_client = aria2_client end |
Instance Attribute Details
#aria2_client ⇒ Object
Returns the value of attribute aria2_client.
4 5 6 |
# File 'lib/leecher/status.rb', line 4 def aria2_client @aria2_client end |
Instance Method Details
#active ⇒ Object
15 16 17 |
# File 'lib/leecher/status.rb', line 15 def active @active ||= aria2_client.tell_active end |
#aria2_status ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/leecher/status.rb', line 23 def aria2_status return "downloading" if active.any? if waiting.empty? "idle" else "paused" end end |
#aria2_version ⇒ Object
11 12 13 |
# File 'lib/leecher/status.rb', line 11 def aria2_version @aria2_version ||= aria2_client.get_version["version"] end |
#calc_diskfree ⇒ Object
46 47 48 49 50 |
# File 'lib/leecher/status.rb', line 46 def calc_diskfree dir = aria2_client.global_opts["dir"] stat = Sys::Filesystem.stat(dir) stat.block_size * stat.blocks_available end |
#calc_downspeed ⇒ Object
36 37 38 39 40 |
# File 'lib/leecher/status.rb', line 36 def calc_downspeed active.inject(0) do |sum, download| sum += Integer(download["downloadSpeed"]) end end |
#changed?(previous, diskchange_threshold = 1048576) ⇒ Boolean
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/leecher/status.rb', line 64 def changed?(previous, diskchange_threshold = 1048576) return true if previous.nil? # compare params, ignoring diskfree now = self.to_params now_disk = now.delete("diskfree") before = previous.to_params before_disk = before.delete("diskfree") return true unless now == before significant_diskchange?(now_disk, before_disk, diskchange_threshold) end |
#diskfree ⇒ Object
42 43 44 |
# File 'lib/leecher/status.rb', line 42 def diskfree @diskfree ||= calc_diskfree end |
#downspeed ⇒ Object
32 33 34 |
# File 'lib/leecher/status.rb', line 32 def downspeed @downspeed ||= calc_downspeed end |
#significant_diskchange?(now, before, diskchange_threshold = 1048576) ⇒ Boolean
77 78 79 |
# File 'lib/leecher/status.rb', line 77 def significant_diskchange?(now, before, diskchange_threshold = 1048576) (now - before).abs > diskchange_threshold end |
#to_params ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/leecher/status.rb', line 52 def to_params { "leecher_version" => Leecher::VERSION, "aria2_version" => aria2_version, "aria2_status" => aria2_status, "downspeed" => downspeed, "n_downloads" => active.size + waiting.size, "n_active_downloads" => active.size, "diskfree" => diskfree, } end |
#waiting ⇒ Object
19 20 21 |
# File 'lib/leecher/status.rb', line 19 def waiting @waiting ||= aria2_client.tell_waiting(0, 100) end |