diff --git a/lib/geocoder.rb b/lib/geocoder.rb index 9e1f157552313149fc08c6f3725866253c19255e..03a9c0897b0a8063c977c733dcf40a2af159e21f 100644 --- a/lib/geocoder.rb +++ b/lib/geocoder.rb @@ -11,12 +11,16 @@ module Geocoder ## # Search for information about an address or a set of coordinates. # - def search(*args) - if blank_query?(args[0]) + def search(query, *args) + # convert coordinates as separate arguments to an array + if query.is_a?(Numeric) and args.first.is_a?(Numeric) + warn "DEPRECATION WARNING: Instead of passing latitude/longitude as separate arguments to the search method, please pass an array: [#{query},#{args.first}]. The old argument format will not be supported in Geocoder v.1.0." + query = [query, args.first] + end + if blank_query?(query) results = [] else - ip = (args.size == 1 and ip_address?(args.first)) - results = lookup(ip).search(*args) + results = lookup(ip_address?(query)).search(query) end results.instance_eval do def warn_search_deprecation(attr) @@ -48,7 +52,7 @@ module Geocoder # Look up the address of the given coordinates. # def address(latitude, longitude) - if (results = search(latitude, longitude)).size > 0 + if (results = search([latitude, longitude])).size > 0 results.first.address end end @@ -126,7 +130,7 @@ module Geocoder # dot-delimited 8-bit numbers. # def ip_address?(value) - !!value.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) + !!value.to_s.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/) end ## diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 56bec5b5c7b78c6655d21ec6f334fc82252d4af3..5c880fdb4834c8ecbd2ac96cc6e2ba841d07faa8 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -20,13 +20,23 @@ module Geocoder # "205.128.54.202") for geocoding, or coordinates (latitude, longitude) # for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s. # - def search(*args) - # if coordinates given as string, split into floats - if coordinates?(args.first) - args = args.first.split(/\s*,\s*/).map{ |i| i.to_f } + def search(query, *args) + # convert coordinates as separate arguments to an array + if query.is_a?(Numeric) and args.first.is_a?(Numeric) + warn "DEPRECATION WARNING: Instead of passing latitude/longitude as separate arguments to the search method, please pass an array: [#{query},#{args.first}]. The old argument format will not be supported in Geocoder v.1.0." + query = [query, args.first] end - reverse = (args.size == 2) - results(args.join(","), reverse).map{ |r| result_class.new(r) } + + # if coordinates given as string, turn into array + query = query.split(/\s*,\s*/) if coordinates?(query) + + if query.is_a?(Array) + reverse = true + query = query.join(',') + else + reverse = false + end + results(query, reverse).map{ |r| result_class.new(r) } end @@ -117,7 +127,7 @@ module Geocoder # Is the given string a loopback IP address? # def loopback_address?(ip) - !!(ip == "0.0.0.0" or ip.match(/^127/)) + !!(ip == "0.0.0.0" or ip.to_s.match(/^127/)) end ## diff --git a/lib/geocoder/orms/base.rb b/lib/geocoder/orms/base.rb index f7cd9e33fd59efea1d6bcdda992a0cf4ce33b454..17346e68bfcabb621669c5de4806ef415830a2ff 100644 --- a/lib/geocoder/orms/base.rb +++ b/lib/geocoder/orms/base.rb @@ -109,14 +109,14 @@ module Geocoder def do_lookup(reverse = false) options = self.class.geocoder_options if reverse and options[:reverse_geocode] - args = to_coordinates + query = to_coordinates elsif !reverse and options[:geocode] - args = [send(options[:user_address])] + query = send(options[:user_address]) else return end - if (results = Geocoder.search(*args)).size > 0 + if (results = Geocoder.search(query)).size > 0 # execute custom block, if specified in configuration block_key = reverse ? :reverse_block : :geocode_block diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index b45c7e2afbf4f83feded90261522445fb97ad87d..99f6d622306becffd409c0863d2be5d62275d469 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -139,7 +139,7 @@ class GeocoderTest < Test::Unit::TestCase def test_result_has_required_attributes all_lookups.each do |l| Geocoder::Configuration.lookup = l - result = Geocoder.search(45.423733, -75.676333).first + result = Geocoder.search([45.423733, -75.676333]).first assert_result_has_required_attributes(result) end end @@ -350,7 +350,7 @@ class GeocoderTest < Test::Unit::TestCase def test_geocoder_ca_result_components Geocoder::Configuration.lookup = :geocoder_ca - result = Geocoder.search(45.423733, -75.676333).first + result = Geocoder.search([45.423733, -75.676333]).first assert_equal "CA", result.country_code assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address end