Module | IPAddress |
In: |
lib/ipaddress/mongoid.rb
lib/ipaddress/prefix.rb lib/ipaddress/ipv4.rb lib/ipaddress/ipv6.rb lib/ipaddress.rb |
NAME | = | "IPAddress" |
GEM | = | "ipaddress" |
AUTHORS | = | ["Marco Ceresa <ceresa@ieee.org>"] |
Mongoid field serialization
IPAddress objects are converted to String
IPAddress.mongoize IPAddress.parse("172.16.10.1") #=> "172.16.10.1"
Prefix will be removed from host adresses
IPAddress.mongoize "172.16.10.1/32" #=> "172.16.10.1"
Prefix will be kept for network addresses
IPAddress.mongoize "172.16.10.1/24" #=> "172.16.10.1/24"
IPv6 addresses will be stored uncompressed to ease DB search and sorting
IPAddress.mongoize "2001:db8::8:800:200c:417a" #=> "2001:0db8:0000:0000:0008:0800:200c:417a" IPAddress.mongoize "2001:db8::8:800:200c:417a/64" #=> "2001:0db8:0000:0000:0008:0800:200c:417a/64"
Invalid addresses will be serialized as nil
IPAddress.mongoize "invalid" #=> nil IPAddress.mongoize "" #=> nil IPAddress.mongoize 1 #=> nil IPAddress.mongoize nil #=> nil
Parse the argument string to create a new IPv4, IPv6 or Mapped IP object
ip = IPAddress.parse 167837953 # 10.1.1.1 ip = IPAddress.parse "172.16.10.1/24" ip6 = IPAddress.parse "2001:db8::8:800:200c:417a/64" ip_mapped = IPAddress.parse "::ffff:172.16.10.1/128"
All the object created will be instances of the correct class:
ip.class #=> IPAddress::IPv4 ip6.class #=> IPAddress::IPv6 ip_mapped.class #=> IPAddress::IPv6::Mapped
Checks if the given string is a valid IPv4 address
Example:
IPAddress::valid_ipv4? "2002::1" #=> false IPAddress::valid_ipv4? "172.16.10.1" #=> true
Checks if the argument is a valid IPv4 netmask expressed in dotted decimal format.
IPAddress.valid_ipv4_netmask? "255.255.0.0" #=> true
Checks if the given string is a valid IPv6 address
Example:
IPAddress::valid_ipv6? "2002::1" #=> true IPAddress::valid_ipv6? "2002::DEAD::BEEF" #=> false
True if the object is an IPv6 address
ip = IPAddress("192.168.10.100/24") ip.ipv6? #-> false