Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use \A and \z in regular expressions to avoid newline issues. Also, d…

…o not consider any address that begins with 127 to be a loopback IP.
  • Loading branch information...
commit 381957053a649403c37e9ab18b41059055809c20 1 parent f9e1ade
Mike Boone boone authored
Showing with 9 additions and 4 deletions.
  1. +4 −4 lib/geocoder/query.rb
  2. +5 −0 test/query_test.rb
8 lib/geocoder/query.rb
View
@@ -48,7 +48,7 @@ def url
def blank?
!params_given? and (
(text.is_a?(Array) and text.compact.size < 2) or
- text.to_s.match(/^\s*$/)
+ text.to_s.match(/\A\s*\z/)
)
end
@@ -59,14 +59,14 @@ def blank?
# dot-delimited numbers.
#
def ip_address?
- !!text.to_s.match(/^(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/)
+ !!text.to_s.match(/\A(::ffff:)?(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\z/)
end
##
# Is the Query text a loopback IP address?
#
def loopback_ip_address?
- !!(text == "0.0.0.0" or text.to_s.match(/^127/))
+ !!(self.ip_address? and (text == "0.0.0.0" or text.to_s.match(/\A127/)))
end
##
@@ -75,7 +75,7 @@ def loopback_ip_address?
def coordinates?
text.is_a?(Array) or (
text.is_a?(String) and
- !!text.to_s.match(/^-?[0-9\.]+, *-?[0-9\.]+$/)
+ !!text.to_s.match(/\A-?[0-9\.]+, *-?[0-9\.]+\z/)
)
end
5 test/query_test.rb
View
@@ -10,6 +10,7 @@ def test_ip_address_detection
assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
assert !Geocoder::Query.new("232.65.123").ip_address?
assert !Geocoder::Query.new("::ffff:123.456.789").ip_address?
+ assert !Geocoder::Query.new("Test\n232.65.123.94").ip_address?
end
def test_blank_query_detection
@@ -18,6 +19,7 @@ def test_blank_query_detection
assert Geocoder::Query.new("\t ").blank?
assert !Geocoder::Query.new("a").blank?
assert !Geocoder::Query.new("Москва").blank? # no ASCII characters
+ assert !Geocoder::Query.new("\na").blank?
assert Geocoder::Query.new(nil, :params => {}).blank?
assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank?
@@ -32,11 +34,14 @@ def test_coordinates_detection
assert Geocoder::Query.new("51.178844,5").coordinates?
assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
assert !Geocoder::Query.new("232.65.123").coordinates?
+ assert !Geocoder::Query.new("Test\n51.178844, -1.826189").coordinates?
end
def test_loopback_ip_address
assert Geocoder::Query.new("0.0.0.0").loopback_ip_address?
assert Geocoder::Query.new("127.0.0.1").loopback_ip_address?
assert !Geocoder::Query.new("232.65.123.234").loopback_ip_address?
+ assert !Geocoder::Query.new("127 Main St.").loopback_ip_address?
+ assert !Geocoder::Query.new("John Doe\n127 Main St.\nAnywhere, USA").loopback_ip_address?
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.