Module: NetworkTester

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

Constant Summary collapse

VERSION =
"0.1.5"

Class Method Summary collapse

Class Method Details

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



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

def loop(max_count=nil, addr='google.com', maxtime=70)
  count = 1
  while (max_count.nil? or count<max_count)
    time = pingr(addr)
    if time.nil?
      puts "#{' '*50}error"
    else
      time = Integer(Float(time))
      puts "#{'*'*(Integer(time/5))} #{time}"
    end
    notifier(time, maxtime)
    sleep 1
  end
rescue Interrupt
end

.mac?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/network_tester.rb', line 63

def mac?
  `uname -s`.chomp=='Darwin'
end

.notifier(time, maxtime = 70) ⇒ Object



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

def notifier(time, maxtime=70)
  if time.nil?
    return notify('error')
  end

  time = Float(time)
  if time < 10
    # notify('sweet')
  elsif time > maxtime
    notify(Integer(time), Integer(time)*2)
  end
  time
end

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



54
55
56
57
58
59
60
61
# File 'lib/network_tester.rb', line 54

def notify(msg='da', rate=600)
  if mac?
    `say #{msg.inspect} -r #{[rate,400].max}`
  else
    puts "\a#{' '*20}#{'-'*10} too long: #{msg.inspect}"
  end
  msg
end

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



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

def pingr(addr='google.com')
  timeout_arg = mac? ? '-t' : '-W'
  cmd = "ping -c 1 #{timeout_arg} 1 #{addr} "
  result = `#{cmd} 2>/dev/null`
  timerow = nil
  result.split("\n").detect do |r|
    if r.match(/time/)
      timerow = r.match(/cmp_seq.*time=([^ ]*)/)
    end
  end

  if timerow.nil?
    nil
  else
    timerow[1] 
  end
end