Class: Avalon::Internet

Inherits:
Node
  • Object
show all
Defined in:
lib/avalon/internet.rb

Overview

Internet is a node encapsulating information about Internet connectivity

Constant Summary collapse

IP_REGEXP =
/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/

Instance Attribute Summary

Attributes inherited from Node

#data, #ip

Instance Method Summary collapse

Methods inherited from Node

#[], #[]=, create, #num, #pool_hash, #reset, #unit_hash

Methods included from Utils

#alarm, #duration, #find_file, #ping, #play, #system

Constructor Details

#initialize(monitor, *sites) ⇒ Internet

Returns a new instance of Internet.



8
9
10
11
12
13
14
15
# File 'lib/avalon/internet.rb', line 8

def initialize monitor, *sites
  sites.map! do |site|
    name = site =~ IP_REGEXP ? site : site.split(/\./)[-2]
    [name.to_sym, site]
  end
  @sites = Hash[ *sites.flatten ]
  super()
end

Instance Method Details

#poll(verbose = true) ⇒ Object



17
18
19
20
# File 'lib/avalon/internet.rb', line 17

def poll verbose=true
  @sites.each {|name, site| self[name] = ping site }
  puts "#{self}" if verbose
end

#reportObject

Check for any exceptional situations with Node, sound alarm if any



23
24
25
26
27
# File 'lib/avalon/internet.rb', line 23

def report
  @data.each do |target, ping|
    alarm "Ping #{target} failed, check your Internet connection" unless ping
  end
end

#to_sObject



29
30
31
# File 'lib/avalon/internet.rb', line 29

def to_s
  "Internet: " + @data.map {|name, value| "#{name}:#{value}"}.join(" ")
end