Module: NetworkTester

Defined in:
lib/network_tester.rb,
lib/network_tester/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.loop(max_count = nil, addr = 'google.com') ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/network_tester.rb', line 37

def loop(max_count=nil, addr='google.com')
  count = 1
  while (max_count.nil? or count<max_count)
    time = pingr
    print time.inspect
    notifier(time)
    sleep 1
  end
rescue => e
  puts 'error, probably not a float'
  puts e
  puts e.backtrace
  notify 'pinger error'
end

.notifier(time) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/network_tester.rb', line 6

def notifier(time)
  time = Float(time)
  if time < 10
    notify('wee')
  elsif time > 70
    notify(Integer(time), rate=Integer(time)*2)
  end
rescue => e
  notify("error",500)
  puts "\n\n#{e} when trying notifier(#{time.inspect})"
  puts e.backtrace
end

.notify(msg = 'da', rate = 600) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/network_tester.rb', line 52

def notify(msg='da', rate=600)
  if `uname -s`.chomp=='Darwin'
    `say #{msg.inspect} -r #{[rate,400].max}`
  else
    puts "\a", "*********"*10, msg.inspect
  end
end

.pingr(addr = 'google.com') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/network_tester.rb', line 19

def pingr(addr='google.com')
  result = `ping -c 1 -t 1 #{addr}`
  timerow = nil
  result.split("\n").detect do |r|
    if r.match /time/
      print "\n#{r}  "
      timerow = r.match /cmp_seq.*time=([^ ]*)/
    end
  end

  if timerow.nil?
    puts result.inspect
    nil
  else
    timerow[1] 
  end
end