Skip to content
Snippets Groups Projects
Commit 97dffb7f authored by Alex Reisner's avatar Alex Reisner
Browse files

Merge pull request #390 from boone/improve_query_regex

Improve regular expressions for queries.
parents 6b8c69a3 38195705
No related branches found
No related tags found
No related merge requests found
...@@ -48,7 +48,7 @@ module Geocoder ...@@ -48,7 +48,7 @@ module Geocoder
def blank? def blank?
!params_given? and ( !params_given? and (
(text.is_a?(Array) and text.compact.size < 2) or (text.is_a?(Array) and text.compact.size < 2) or
text.to_s.match(/^\s*$/) text.to_s.match(/\A\s*\z/)
) )
end end
...@@ -59,14 +59,14 @@ module Geocoder ...@@ -59,14 +59,14 @@ module Geocoder
# dot-delimited numbers. # dot-delimited numbers.
# #
def ip_address? 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 end
## ##
# Is the Query text a loopback IP address? # Is the Query text a loopback IP address?
# #
def 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 end
## ##
...@@ -75,7 +75,7 @@ module Geocoder ...@@ -75,7 +75,7 @@ module Geocoder
def coordinates? def coordinates?
text.is_a?(Array) or ( text.is_a?(Array) or (
text.is_a?(String) and text.is_a?(String) and
!!text.to_s.match(/^-?[0-9\.]+, *-?[0-9\.]+$/) !!text.to_s.match(/\A-?[0-9\.]+, *-?[0-9\.]+\z/)
) )
end end
......
...@@ -10,6 +10,7 @@ class QueryTest < Test::Unit::TestCase ...@@ -10,6 +10,7 @@ class QueryTest < Test::Unit::TestCase
assert !Geocoder::Query.new("232.65.123.94.43").ip_address? assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
assert !Geocoder::Query.new("232.65.123").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("::ffff:123.456.789").ip_address?
assert !Geocoder::Query.new("Test\n232.65.123.94").ip_address?
end end
def test_blank_query_detection def test_blank_query_detection
...@@ -18,6 +19,7 @@ class QueryTest < Test::Unit::TestCase ...@@ -18,6 +19,7 @@ class QueryTest < Test::Unit::TestCase
assert Geocoder::Query.new("\t ").blank? assert Geocoder::Query.new("\t ").blank?
assert !Geocoder::Query.new("a").blank? assert !Geocoder::Query.new("a").blank?
assert !Geocoder::Query.new("Москва").blank? # no ASCII characters 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 => {}).blank?
assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank? assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank?
...@@ -32,11 +34,14 @@ class QueryTest < Test::Unit::TestCase ...@@ -32,11 +34,14 @@ class QueryTest < Test::Unit::TestCase
assert Geocoder::Query.new("51.178844,5").coordinates? assert Geocoder::Query.new("51.178844,5").coordinates?
assert Geocoder::Query.new("51.178844, -1.826189").coordinates? assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
assert !Geocoder::Query.new("232.65.123").coordinates? assert !Geocoder::Query.new("232.65.123").coordinates?
assert !Geocoder::Query.new("Test\n51.178844, -1.826189").coordinates?
end end
def test_loopback_ip_address def test_loopback_ip_address
assert Geocoder::Query.new("0.0.0.0").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("127.0.0.1").loopback_ip_address?
assert !Geocoder::Query.new("232.65.123.234").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
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment