Class: Leecher::Status

Inherits:
Object
  • Object
show all
Defined in:
lib/leecher/status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_clientObject

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

#activeObject



15
16
17
# File 'lib/leecher/status.rb', line 15

def active
  @active ||= aria2_client.tell_active
end

#aria2_statusObject



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_versionObject



11
12
13
# File 'lib/leecher/status.rb', line 11

def aria2_version
  @aria2_version ||= aria2_client.get_version["version"]
end

#calc_diskfreeObject



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_downspeedObject



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

Returns:

  • (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

#diskfreeObject



42
43
44
# File 'lib/leecher/status.rb', line 42

def diskfree
  @diskfree ||= calc_diskfree
end

#downspeedObject



32
33
34
# File 'lib/leecher/status.rb', line 32

def downspeed
  @downspeed ||= calc_downspeed
end

#significant_diskchange?(now, before, diskchange_threshold = 1048576) ⇒ Boolean

Returns:

  • (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_paramsObject



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

#waitingObject



19
20
21
# File 'lib/leecher/status.rb', line 19

def waiting
  @waiting ||= aria2_client.tell_waiting(0, 100)
end