Sha256: 426c0742a8166f78a903e8d8df96ea1db60270267a980490e1d0d5b193b0b653

Contents?: true

Size: 946 Bytes

Versions: 1

Compression:

Stored size: 946 Bytes

Contents

module Throttle
  module Bandwidth
    def parse_bandwidth(str)
      amount = nil
      units  = "KBp/s"
            
      if !(/(\d*)/.match(str).to_s.empty?)
        amount = Regexp.last_match(0).to_i
        
        if /((K|M|k|m)(b|B)(p|P))/.match(str)
          units  = Regexp.last_match(0)
          units.gsub!('P', 'p')
          units.gsub!('/s', '')
          units += '/s'
        end  
        
        # Maximum allowed by ipfw
        if over_limit?(amount, units)
          amount = 268
          units  = "MBp/s"
        end  
        
        "#{amount}#{units}"
      else
        nil
      end    
    end  
    
    private
    
    def over_limit?(amount, units)
      case units
      when "MBp/s"
        true if amount > 268
      when "Mbp/s"
        true if amount > 2144
      when "KBp/s"
        true if amount > 268000
      when "Kbp/s"
        true if amount > 2144000
      end       
    end  
  end  
end
  

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
throttle-0.0.2 lib/throttle/bandwidth.rb