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

Deprecate separate lat/lon args to Geocoder.search.

parent eafa579e
No related branches found
No related tags found
No related merge requests found
...@@ -11,12 +11,16 @@ module Geocoder ...@@ -11,12 +11,16 @@ module Geocoder
## ##
# Search for information about an address or a set of coordinates. # Search for information about an address or a set of coordinates.
# #
def search(*args) def search(query, *args)
if blank_query?(args[0]) # 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 = [] results = []
else else
ip = (args.size == 1 and ip_address?(args.first)) results = lookup(ip_address?(query)).search(query)
results = lookup(ip).search(*args)
end end
results.instance_eval do results.instance_eval do
def warn_search_deprecation(attr) def warn_search_deprecation(attr)
...@@ -48,7 +52,7 @@ module Geocoder ...@@ -48,7 +52,7 @@ module Geocoder
# Look up the address of the given coordinates. # Look up the address of the given coordinates.
# #
def address(latitude, longitude) def address(latitude, longitude)
if (results = search(latitude, longitude)).size > 0 if (results = search([latitude, longitude])).size > 0
results.first.address results.first.address
end end
end end
...@@ -126,7 +130,7 @@ module Geocoder ...@@ -126,7 +130,7 @@ module Geocoder
# dot-delimited 8-bit numbers. # dot-delimited 8-bit numbers.
# #
def ip_address?(value) 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 end
## ##
......
...@@ -20,13 +20,23 @@ module Geocoder ...@@ -20,13 +20,23 @@ module Geocoder
# "205.128.54.202") for geocoding, or coordinates (latitude, longitude) # "205.128.54.202") for geocoding, or coordinates (latitude, longitude)
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s. # for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s.
# #
def search(*args) def search(query, *args)
# if coordinates given as string, split into floats # convert coordinates as separate arguments to an array
if coordinates?(args.first) if query.is_a?(Numeric) and args.first.is_a?(Numeric)
args = args.first.split(/\s*,\s*/).map{ |i| i.to_f } 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 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 end
...@@ -117,7 +127,7 @@ module Geocoder ...@@ -117,7 +127,7 @@ module Geocoder
# Is the given string a loopback IP address? # Is the given string a loopback IP address?
# #
def loopback_address?(ip) 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 end
## ##
......
...@@ -109,14 +109,14 @@ module Geocoder ...@@ -109,14 +109,14 @@ module Geocoder
def do_lookup(reverse = false) def do_lookup(reverse = false)
options = self.class.geocoder_options options = self.class.geocoder_options
if reverse and options[:reverse_geocode] if reverse and options[:reverse_geocode]
args = to_coordinates query = to_coordinates
elsif !reverse and options[:geocode] elsif !reverse and options[:geocode]
args = [send(options[:user_address])] query = send(options[:user_address])
else else
return return
end end
if (results = Geocoder.search(*args)).size > 0 if (results = Geocoder.search(query)).size > 0
# execute custom block, if specified in configuration # execute custom block, if specified in configuration
block_key = reverse ? :reverse_block : :geocode_block block_key = reverse ? :reverse_block : :geocode_block
......
...@@ -139,7 +139,7 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -139,7 +139,7 @@ class GeocoderTest < Test::Unit::TestCase
def test_result_has_required_attributes def test_result_has_required_attributes
all_lookups.each do |l| all_lookups.each do |l|
Geocoder::Configuration.lookup = 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) assert_result_has_required_attributes(result)
end end
end end
...@@ -350,7 +350,7 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -350,7 +350,7 @@ class GeocoderTest < Test::Unit::TestCase
def test_geocoder_ca_result_components def test_geocoder_ca_result_components
Geocoder::Configuration.lookup = :geocoder_ca 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 "CA", result.country_code
assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
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