Skip to content

Commit

Permalink
Added #ipv4? and #ipv6? methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bluemonk committed Mar 27, 2011
1 parent bd5f741 commit f34b5cd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
26 changes: 25 additions & 1 deletion lib/ipaddress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module IPAddress
NAME = "IPAddress"
GEM = "ipaddress"
AUTHORS = ["Marco Ceresa <ceresa@ieee.org>"]

#
# Parse the argument string to create a new
# IPv4, IPv6 or Mapped IP object
Expand Down Expand Up @@ -50,6 +50,30 @@ def IPAddress::parse(str)
end
end

#
# True if the object is an IPv4 address
#
# ip = IPAddress("192.168.10.100/24")
#
# ip.ipv4?
# #-> true
#
def ipv4?
self.kind_of? IPAddress::IPv4
end

#
# True if the object is an IPv6 address
#
# ip = IPAddress("192.168.10.100/24")
#
# ip.ipv6?
# #-> false
#
def ipv6?
self.kind_of? IPAddress::IPv6
end

#
# Checks if the given string is a valid IP address,
# either IPv4 or IPv6
Expand Down
24 changes: 24 additions & 0 deletions lib/ipaddress/ipv4.rb
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,30 @@ def include?(oth)
def include_all?(*others)
others.all? {|oth| include?(oth)}
end

#
# True if the object is an IPv4 address
#
# ip = IPAddress("192.168.10.100/24")
#
# ip.ipv4?
# #-> true
#
# def ipv4?
# true
# end

#
# True if the object is an IPv6 address
#
# ip = IPAddress("192.168.10.100/24")
#
# ip.ipv6?
# #-> false
#
# def ipv6?
# false
# end

#
# Checks if an IPv4 address objects belongs
Expand Down
8 changes: 8 additions & 0 deletions test/ipaddress/ipv4_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,14 @@ def test_method_include_all?
assert_equal false, ip.include_all?(addr1, @klass.new("13.16.0.0/32"))
end

def test_method_ipv4?
assert_equal true, @ip.ipv4?
end

def test_method_ipv6?
assert_equal false, @ip.ipv6?
end

def test_method_private?
assert_equal true, @klass.new("192.168.10.50/24").private?
assert_equal true, @klass.new("192.168.10.50/16").private?
Expand Down
38 changes: 21 additions & 17 deletions test/ipaddress/ipv6_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ def test_method_group
end
end

def test_method_ipv4?
assert_equal false, @ip.ipv4?
end

def test_method_ipv6?
assert_equal true, @ip.ipv6?
end

def test_method_network?
assert_equal true, @network.network?
assert_equal false, @ip.network?
Expand Down Expand Up @@ -197,22 +205,6 @@ def test_classmethod_compress
assert_not_equal compressed, @klass.compress("2001:0db8::cd3")
end

# def test_classmethod_create_unpecified
# unspec = @klass.create_unspecified
# assert_equal "::", unspec.address
# assert_equal 128, unspec.prefix
# assert_equal true, unspec.unspecified?
# assert_instance_of @klass, unspec.class
# end

# def test_classmethod_create_loopback
# loopb = @klass.create_loopback
# assert_equal "::1", loopb.address
# assert_equal 128, loopb.prefix
# assert_equal true, loopb.loopback?
# assert_instance_of @klass, loopb.class
# end

def test_classmethod_parse_data
str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
ip = @klass.parse_data str
Expand All @@ -231,7 +223,7 @@ def test_classmethod_parse_hex
assert_equal @ip.to_s, @klass.parse_hex(@hex,64).to_s
end

end # class IPv4Test
end # class IPv6Test

class IPv6UnspecifiedTest < Test::Unit::TestCase

Expand Down Expand Up @@ -259,6 +251,10 @@ def test_attributes
assert_equal @string, @ip.to_string_uncompressed
assert_equal @u128, @ip.to_u128
end

def test_method_ipv6?
assert_equal true, @ip.ipv6?
end

end # class IPv6UnspecifiedTest

Expand Down Expand Up @@ -289,6 +285,10 @@ def test_attributes
assert_equal @string, @ip.to_string_uncompressed
assert_equal @u128, @ip.to_u128
end

def test_method_ipv6?
assert_equal true, @ip.ipv6?
end

end # class IPv6LoopbackTest

Expand Down Expand Up @@ -345,6 +345,10 @@ def test_attributes
assert_equal @u128, @ip.to_u128
end

def test_method_ipv6?
assert_equal true, @ip.ipv6?
end

def test_mapped?
assert_equal true, @ip.mapped?
end
Expand Down

0 comments on commit f34b5cd

Please sign in to comment.