class QuartzTorrent::Formatter

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

Constants

Gig

Number of bytes in a Gigabyte.

Kb

Number of bytes in a Kilobyte.

Meg

Number of bytes in a Megabyte.

Public Class Methods

formatPercent(frac) click to toggle source

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

# 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) click to toggle source

Format a size in bytes.

# 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) click to toggle source

Format a speed in bytes per second.

# 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) click to toggle source

Format a duration of time in seconds.

# 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) click to toggle source

Parse a size in the format ‘50 KB’

# 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) click to toggle source

Parse a duration of time into seconds.

# 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