lib/throttle/bandwidth.rb in throttle-0.0.1 vs lib/throttle/bandwidth.rb in throttle-0.0.2

- old
+ new

@@ -2,26 +2,44 @@ module Bandwidth def parse_bandwidth(str) amount = nil units = "KBp/s" - if /(\d*)/.match(str) + 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 MBps - amount = 268 if units == "MBp/s" && amount > 268 + # 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 \ No newline at end of file