From f4738836456624f43f307abacac8588a507ae32a Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Fri, 25 Mar 2011 10:34:00 -0400 Subject: [PATCH] Fix: handle coordinates string properly. Get rid of space after comma, if any (Google was returning no results). --- lib/geocoder/lookups/base.rb | 8 ++++++-- test/geocoder_test.rb | 14 +++++++------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index d39ab524..56bec5b5 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -21,7 +21,11 @@ module Geocoder # for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s. # def search(*args) - reverse = (args.size == 2) || coordinates?(args.first) + # if coordinates given as string, split into floats + if coordinates?(args.first) + args = args.first.split(/\s*,\s*/).map{ |i| i.to_f } + end + reverse = (args.size == 2) results(args.join(","), reverse).map{ |r| result_class.new(r) } end @@ -120,7 +124,7 @@ module Geocoder # Does the given string look like latitude/longitude coordinates? # def coordinates?(value) - !!value.to_s.match(/^[0-9\.\-]+, ?[0-9\.\-]+$/) + !!value.to_s.match(/^[0-9\.\-]+, *[0-9\.\-]+$/) end ## diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index 7b071735..bb1874c3 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -258,6 +258,13 @@ class GeocoderTest < Test::Unit::TestCase assert !Geocoder.send(:blank_query?, "МоÑква") # no ASCII characters end + def test_coordinates_detection + lookup = Geocoder::Lookup::Google.new + assert lookup.send(:coordinates?, "51.178844,5") + assert lookup.send(:coordinates?, "51.178844, -1.826189") + assert !lookup.send(:coordinates?, "232.65.123") + end + def test_does_not_choke_on_nil_address all_lookups.each do |l| Geocoder::Configuration.lookup = l @@ -351,13 +358,6 @@ class GeocoderTest < Test::Unit::TestCase assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States") end - def test_detection_of_coordinates_in_search_string - Geocoder::Configuration.lookup = :geocoder_ca - result = Geocoder.search("51.178844, -1.826189").first - assert_not_nil result.city - # city only present if reverse geocoding search performed - end - private # ------------------------------------------------------------------ -- GitLab