Module: Throttle::Bandwidth

Included in:
Client
Defined in:
lib/throttle/bandwidth.rb

Instance Method Summary collapse

Instance Method Details

#parse_bandwidth(str) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/throttle/bandwidth.rb', line 3

def parse_bandwidth(str)
  amount = nil
  units  = "KBp/s"
        
  if /(\d*)/.match(str)
    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
    
    "#{amount}#{units}"
  else
    nil
  end    
end