lib/arborist/mixins.rb in arborist-0.0.1.pre20160606141735 vs lib/arborist/mixins.rb in arborist-0.0.1.pre20160829140603

- old
+ new

@@ -1,8 +1,10 @@ # -*- ruby -*- #encoding: utf-8 +require 'ipaddr' + # A collection of generically-useful mixins module Arborist # A collection of methods for declaring other methods. # @@ -278,11 +280,11 @@ end # refine Time end # module TimeRefinements - ### A collection of utilities for working with Hashes. + # A collection of utilities for working with Hashes. module HashUtilities ############### module_function ############### @@ -374,7 +376,40 @@ return actual == val end end end # HashUtilities + + + # A collection of utilities for working with network addresses, names, etc. + module NetworkUtilities + + # A union of IPv4 and IPv6 regular expressions. + IPADDR_RE = Regexp.union( + IPAddr::RE_IPV4ADDRLIKE, + IPAddr::RE_IPV6ADDRLIKE_COMPRESSED, + IPAddr::RE_IPV6ADDRLIKE_FULL + ) + + + ### Return the specified +address+ as one or more IPAddr objects. + def normalize_address( address ) + addresses = [] + case address + when IPAddr + addresses << address + when IPADDR_RE + addresses << IPAddr.new( address ) + when String + ip_addr = TCPSocket.gethostbyname( address ) + addresses = ip_addr[3..-1].map{|ip| IPAddr.new(ip) } + else + raise "I don't know how to parse a %p host address (%p)" % + [ address.class, address ] + end + + return addresses + end + + end # module NetworkUtilities end # module Arborist