1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
# $Id: address.rb,v 1.6 2003/04/01 15:42:16 sdalu Exp $
#
# AUTHOR : Stephane D'Alu <sdalu@nic.fr>
# CREATED : 2002/07/19 07:28:13
#
# COPYRIGHT: AFNIC (c) 2003
# LICENSE : RUBY
# CONTACT :
#
# $Revision: 1.6 $
# $Date: 2003/04/01 15:42:16 $
#
# INSPIRED BY:
# - the ruby file: resolv.rb
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
require 'address/common'
require 'address/ipv4'
require 'address/ipv6'
##
## All addresses object are immutables
##
class Address
#
# Address detection order (between IPv4/IPv6)
#
OrderStrict = [ Address::IPv4, Address::IPv6 ]
OrderCompatibility = [ Address::IPv6::Compatibility ]
OrderIPv6Only = [ Address::IPv6 ]
OrderIPv4Only = [ Address::IPv4 ]
OrderDefault = OrderStrict
# Check if a string as a valid address representation
# and respect the address priority order
def self.is_valid(addr, order=OrderDefault)
order.each { |klass|
return true if addr =~ klass::Regex }
false
end
# Try to convert a string into any address (and respect the
# address priority order)
def self.create(arg, order=OrderDefault)
order.each { |klass|
begin
return klass::create(arg)
rescue InvalidAddress
end
}
raise InvalidAddress, "can't interpret #{arg.inspect} as address"
end
end
|