Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added prefix parameter to IPv6.parse_data #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/ipaddress/ipv6.rb
Expand Up @@ -566,14 +566,13 @@ def self.groups(str)
# #
# With that data you can create a new IPv6 object: # With that data you can create a new IPv6 object:
# #
# ip6 = IPAddress::IPv6::parse_data " \001\r\270\000\000\000\000\000\b\b\000 \fAz" # ip6 = IPAddress::IPv6::parse_data " \001\r\270\000\000\000\000\000\b\b\000 \fAz", 64
# ip6.prefix = 64
# #
# ip6.to_s # ip6.to_s
# #=> "2001:db8::8:800:200c:417a/64" # #=> "2001:db8::8:800:200c:417a/64"
# #
def self.parse_data(str) def self.parse_data(str, prefix=128)
self.new(IN6FORMAT % str.unpack("n8")) return self.new(IN6FORMAT % str.unpack("n8") + '/' + prefix.to_s)
end end


# #
Expand Down
6 changes: 6 additions & 0 deletions test/ipaddress/ipv6_test.rb
Expand Up @@ -279,6 +279,12 @@ def test_classmethod_parse_data
assert_equal "2001:db8::8:800:200c:417a/128", ip.to_string assert_equal "2001:db8::8:800:200c:417a/128", ip.to_string
end end


def test_classmethod_parse_data_with_prefix
str = " \001\r\270\000\000\000\000\000\b\b\000 \fAz"
ip = @klass.parse_data str, 64
assert_equal "2001:db8::8:800:200c:417a/64", ip.to_string
end

def test_classhmethod_parse_u128 def test_classhmethod_parse_u128
@valid_ipv6.each do |ip,num| @valid_ipv6.each do |ip,num|
assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s assert_equal @klass.new(ip).to_s, @klass.parse_u128(num).to_s
Expand Down