diff options
author | Mike Mackintosh <m@zyp.io> | 2015-01-26 16:21:55 -0500 |
---|---|---|
committer | Mike Mackintosh <m@zyp.io> | 2015-01-26 16:21:55 -0500 |
commit | f90b36f2da80070411131af985c2af9f236a1685 (patch) | |
tree | 223eb31957ab6a3b548aaf6e086c96af1c9b7e6d /lib | |
parent | ce5b524748759d23790a710fd1adcca6a547d91d (diff) | |
download | ipaddress-f90b36f2da80070411131af985c2af9f236a1685.tar.gz |
added #ntoa unint32 -> ip and tests
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ipaddress.rb | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/lib/ipaddress.rb b/lib/ipaddress.rb index e2161e1..4033c3a 100644 --- a/lib/ipaddress.rb +++ b/lib/ipaddress.rb @@ -26,6 +26,7 @@ module IPAddress # 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" @@ -41,6 +42,12 @@ module IPAddress # #=> IPAddress::IPv6::Mapped # def IPAddress::parse(str) + + # Check if an int was passed + if str.kind_of? Integer + return IPAddress::IPv4.new(ntoa(str)) + end + case str when /:.+\./ IPAddress::IPv6::Mapped.new(str) @@ -54,6 +61,24 @@ module IPAddress end # + # Converts a unit32 to IPv4 + # + # IPAddress::ntoa(167837953) + # #-> "10.1.1.1" + # + def self.ntoa(uint) + unless(uint.is_a? Numeric and uint <= 0xffffffff) + raise(::ArgumentError, "not a long integer: #{uint.inspect}") + end + ret = [] + 4.times do + ret.unshift(uint & 0xff) + uint >>= 8 + end + ret.join('.') + end + + # # True if the object is an IPv4 address # # ip = IPAddress("192.168.10.100/24") |