Skip to content

Commit

Permalink
Add IPv6 format detection
Browse files Browse the repository at this point in the history
  • Loading branch information
sethherr committed Sep 17, 2014
1 parent 497b197 commit 5cb5cf7
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/geocoder/ip_address.rb
Expand Up @@ -6,7 +6,14 @@ def loopback?
end

def valid?
!!self.match(/\A(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/)
ipregex = %r{
\A( # String Starts
((::ffff:)?((\d{1,3})\.){3}\d{1,3}) # Check for IPv4
| # .... Or
(\S+?(:\S+?){6}\S+) # Check for IPv6
)\z
}x
!!self.match(ipregex)
end
end
end
1 change: 1 addition & 0 deletions test/unit/ip_address_test.rb
Expand Up @@ -8,6 +8,7 @@ def test_valid
assert Geocoder::IpAddress.new("232.65.123.94").valid?
assert Geocoder::IpAddress.new("666.65.123.94").valid? # technically invalid
assert Geocoder::IpAddress.new("::ffff:12.34.56.78").valid?
assert Geocoder::IpAddress.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").valid?
assert !Geocoder::IpAddress.new("232.65.123.94.43").valid?
assert !Geocoder::IpAddress.new("232.65.123").valid?
assert !Geocoder::IpAddress.new("::ffff:123.456.789").valid?
Expand Down
1 change: 1 addition & 0 deletions test/unit/query_test.rb
Expand Up @@ -6,6 +6,7 @@ class QueryTest < GeocoderTestCase

def test_ip_address_detection
assert Geocoder::Query.new("232.65.123.94").ip_address?
assert Geocoder::Query.new("3ffe:0b00:0000:0000:0001:0000:0000:000a").ip_address?
assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
assert !Geocoder::Query.new("::ffff:123.456.789").ip_address?
end
Expand Down

0 comments on commit 5cb5cf7

Please sign in to comment.