Class: QuartzTorrent::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/quartz_torrent/formatter.rb

Overview

Class that can be used to format different quantities into human readable strings.

Constant Summary collapse

Kb =

Number of bytes in a Kilobyte.

1024
Meg =

Number of bytes in a Megabyte.

1024*Kb
Gig =

Number of bytes in a Gigabyte.

1024*Meg

Class Method Summary collapse

Class Method Details

.formatPercent(frac) ⇒ Object

Format a floating point number as a percentage with one decimal place.



29
30
31
32
33
# File 'lib/quartz_torrent/formatter.rb', line 29

def self.formatPercent(frac)
  return nil if ! frac
  s = "%.1f" % (frac.to_f*100)
  s + "%"
end

.formatSize(size) ⇒ Object

Format a size in bytes.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/quartz_torrent/formatter.rb', line 13

def self.formatSize(size)
  return nil if !size
  s = size.to_f
  if s >= Gig
    s = "%.2fGB" % (s / Gig)
  elsif s >= Meg
    s = "%.2fMB" % (s / Meg)
  elsif s >= Kb
    s = "%.2fKB" % (s / Kb)
  else
    s = "%.2fB" % s
  end
  s
end

.formatSpeed(s) ⇒ Object

Format a speed in bytes per second.



36
37
38
39
40
41
42
43
# File 'lib/quartz_torrent/formatter.rb', line 36

def self.formatSpeed(s)
  size = Formatter.formatSize(s)
  if size
    size + "/s"
  else
    nil
  end
end

.formatTime(secs) ⇒ Object

Format a duration of time in seconds.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/quartz_torrent/formatter.rb', line 46

def self.formatTime(secs)
  return nil if ! secs
  s = ""
  time = secs.to_i
  arr = []
  conv = [60,60]
  unit = ["s","m","h"]
  conv.each{ |c|
    v = time % c
    time = time / c
    arr.push v
  }
  arr.push time
  i = unit.size-1
  arr.reverse.each{ |v|
    if v == 0
      i -= 1
    else
      break
    end
  }
  while i >= 0
    s << arr[i].to_s + unit[i]
    i -= 1
  end
  
  s = "0s" if s.length == 0
 
  s
end

.parseSize(size) ⇒ Object

Parse a size in the format ‘50 KB’



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/quartz_torrent/formatter.rb', line 78

def self.parseSize(size)
  return nil if ! size
  if size =~ /(\d+(?:\.\d+)?)\s*([^\d]+)*/
    value = $1.to_f
    suffix = ($2 ? $2.downcase : nil)

    multiplicand = 1
    if suffix.nil? || suffix[0] == 'b'
      multiplicand = 1
    elsif suffix[0,2] == 'kb'
      multiplicand = Kb
    elsif suffix[0,2] == 'mb'
      multiplicand = Meg
    elsif suffix[0,2] == 'gb'
      multiplicand = Gig
    else
      raise "Unknown suffix '#{suffix}' for size '#{size}'"
    end
   
    value*multiplicand
  else  
    raise "Malformed size '#{size}'"
  end
end

.parseTime(time) ⇒ Object

Parse a duration of time into seconds.



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/quartz_torrent/formatter.rb', line 104

def self.parseTime(time)
  return nil if ! time

  if time =~ /(?:(\d+)\s*h)?\s*(?:(\d+)\s*m)?\s*(?:(\d+)\s*s)?/
    h = $1.to_i
    m = $2.to_i
    s = $3.to_i

    h*3600+m*60+s
  else
    raise "Malformed duration '#{time}'"
  end
end