Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
gitbourahlavignal
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
VIGNAL Nicolas
gitbourahlavignal
Commits
52234fe4
Commit
52234fe4
authored
10 years ago
by
Marco Ceresa
Browse files
Options
Downloads
Plain Diff
Merge pull request #57 from mikemackintosh/uint32-parse
added #ntoa unint32 -> ip and tests
parents
f2080750
f90b36f2
No related branches found
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
README.rdoc
+9
-2
9 additions, 2 deletions
README.rdoc
lib/ipaddress.rb
+25
-0
25 additions, 0 deletions
lib/ipaddress.rb
test/ipaddress_test.rb
+28
-0
28 additions, 0 deletions
test/ipaddress_test.rb
test/test_helper.rb
+1
-0
1 addition, 0 deletions
test/test_helper.rb
with
63 additions
and
2 deletions
README.rdoc
+
9
−
2
View file @
52234fe4
...
...
@@ -89,7 +89,7 @@ or, in a easier way, using the IPAddress parse method
ip = IPAddress.parse "172.16.10.1/24"
which accepts and parses any kind of IP (IPv4, IPV6 and
which accepts and parses any kind of IP (
uint32,
IPv4, IPV6 and
IPv4 IPv6 Mapped addresses).
If you like syntactic sugar, you can use the wrapper method
...
...
@@ -120,7 +120,14 @@ The new created object has prefix /32, which is the same
as we created the following:
host = IPAddress::IPv4.new "10.1.1.1/32"
You can also pass a uint32 to obtain an IPAddress::IPv4 object:
# Create host object
ip = IPAddress 167837953
puts ip.to_string
#=> "10.1.1.1/32"
=== Handling the IPv4 address
Once created, you can obtain the attributes for an IPv4 object:
...
...
This diff is collapsed.
Click to expand it.
lib/ipaddress.rb
+
25
−
0
View file @
52234fe4
...
...
@@ -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
)
...
...
@@ -53,6 +60,24 @@ module IPAddress
end
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
#
...
...
This diff is collapsed.
Click to expand it.
test/ipaddress_test.rb
+
28
−
0
View file @
52234fe4
...
...
@@ -11,9 +11,28 @@ class IPAddressTest < Test::Unit::TestCase
@invalid_ipv6
=
":1:2:3:4:5:6:7"
@invalid_mapped
=
"::1:2.3.4"
@valid_ipv4_uint32
=
[
4294967295
,
# 255.255.255.255
167772160
,
# 10.0.0.0
3232235520
,
# 192.168.0.0
0
]
@invalid_ipv4_uint32
=
[
4294967296
,
# 256.0.0.0
"A294967295"
,
# Invalid uINT
-
1
]
# Invalid
@ipv4class
=
IPAddress
::
IPv4
@ipv6class
=
IPAddress
::
IPv6
@mappedclass
=
IPAddress
::
IPv6
::
Mapped
@invalid_ipv4
=
[
"10.0.0.256"
,
"10.0.0.0.0"
,
"10.0.0"
,
"10.0"
]
@valid_ipv4_range
=
[
"10.0.0.1-254"
,
"10.0.1-254.0"
,
"10.1-254.0.0"
]
@method
=
Module
.
method
(
"IPAddress"
)
end
...
...
@@ -31,6 +50,15 @@ class IPAddressTest < Test::Unit::TestCase
assert_raise
(
ArgumentError
)
{
@method
.
call
(
@invalid_ipv6
)}
assert_raise
(
ArgumentError
)
{
@method
.
call
(
@invalid_mapped
)}
assert_instance_of
@ipv4class
,
@method
.
call
(
@valid_ipv4_uint32
[
0
])
assert_instance_of
@ipv4class
,
@method
.
call
(
@valid_ipv4_uint32
[
1
])
assert_instance_of
@ipv4class
,
@method
.
call
(
@valid_ipv4_uint32
[
2
])
assert_instance_of
@ipv4class
,
@method
.
call
(
@valid_ipv4_uint32
[
3
])
assert_raise
(
ArgumentError
)
{
@method
.
call
(
@invalid_ipv4_uint32
[
0
])}
assert_raise
(
ArgumentError
)
{
@method
.
call
(
@invalid_ipv4_uint32
[
1
])}
assert_raise
(
ArgumentError
)
{
@method
.
call
(
@invalid_ipv4_uint32
[
2
])}
end
def
test_module_method_valid?
...
...
This diff is collapsed.
Click to expand it.
test/test_helper.rb
+
1
−
0
View file @
52234fe4
require
'rubygems'
require
'minitest/autorun'
require
'test/unit'
$LOAD_PATH
.
unshift
(
File
.
dirname
(
__FILE__
))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment