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

Make Geocoder.lookup private.

Also move 'address' and 'coordinates' methods from Geocoder::Lookup::Base
to Geocoder. This simplifies the interface and allows us to choose a
lookup engine dynamically for other types of geocoding (eg: IP addresses).
parent 02bdc9e7
No related branches found
No related tags found
No related merge requests found
......@@ -13,6 +13,32 @@ module Geocoder
lookup.search(*args)
end
##
# Look up the coordinates of the given street address.
#
def coordinates(address)
if (results = search(address)).size > 0
results.first.coordinates
end
end
##
# Look up the address of the given coordinates.
#
def address(latitude, longitude)
if (results = search(latitude, longitude)).size > 0
results.first.address
end
end
# exception classes
class Error < StandardError; end
class ConfigurationError < Error; end
private # -----------------------------------------------------------------
##
# Get the lookup object (which communicates with the remote geocoding API).
#
......@@ -32,10 +58,6 @@ module Geocoder
@lookup = Geocoder::Lookup::Google.new
end
end
# exception classes
class Error < StandardError; end
class ConfigurationError < Error; end
end
Geocoder::Railtie.insert
......@@ -28,7 +28,7 @@ module Geocoder
#
scope :near, lambda{ |location, *args|
latitude, longitude = location.is_a?(Array) ?
location : Geocoder.lookup.coordinates(location)
location : Geocoder.coordinates(location)
if latitude and longitude
near_scope_options(latitude, longitude, *args)
else
......@@ -189,7 +189,7 @@ module Geocoder
"You are attempting to fetch coordinates but have not specified " +
"a method which provides an address for the object."
end
coords = Geocoder.lookup.coordinates(send(address_method))
coords = Geocoder.coordinates(send(address_method))
unless coords.blank?
method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:latitude], coords[0]
......@@ -217,7 +217,7 @@ module Geocoder
"You are attempting to fetch an address but have not specified " +
"attributes which provide coordinates for the object."
end
address = Geocoder.lookup.address(send(lat_attr), send(lon_attr))
address = Geocoder.address(send(lat_attr), send(lon_attr))
unless address.blank?
method = (save ? "update" : "write") + "_attribute"
send method, self.class.geocoder_options[:fetched_address], address
......
......@@ -18,27 +18,23 @@ module Geocoder
end
end
private # -------------------------------------------------------------
##
# Look up the coordinates of the given address.
# Array of results, or nil on timeout or other error.
#
def coordinates(address)
if (results = search(address)).size > 0
results.first.coordinates
end
def results(query, reverse = false)
fail
end
##
# Look up the address of the given coordinates.
# URL to use for querying the geocoding engine.
#
def address(latitude, longitude)
if (results = search(latitude, longitude)).size > 0
results.first.address
end
def query_url(query, reverse = false)
fail
end
private # -------------------------------------------------------------
##
# Class of the result objects
#
......
......@@ -6,10 +6,6 @@ module Geocoder::Lookup
private # ---------------------------------------------------------------
##
# Returns a parsed Google geocoder search result (hash).
# Returns nil if non-200 HTTP response, timeout, or other error.
#
def results(query, reverse = false)
doc = fetch_data(query, reverse)
case doc['status']; when "OK"
......
......@@ -6,10 +6,6 @@ module Geocoder::Lookup
private # ---------------------------------------------------------------
##
# Returns a parsed Yahoo geocoder search result (hash).
# Returns nil if non-200 HTTP response, timeout, or other error.
#
def results(query, reverse = false)
doc = fetch_data(query, reverse)
if doc = doc['ResultSet'] and doc['Error'] == 0
......
......@@ -4,7 +4,7 @@ class GeocoderTest < Test::Unit::TestCase
def setup
Geocoder::Configuration.lookup = :google
Geocoder.set_lookup :google
Geocoder.send :set_lookup, :google
end
def test_fetch_coordinates
......@@ -49,14 +49,14 @@ class GeocoderTest < Test::Unit::TestCase
# --- Yahoo ---
def test_yahoo_result_components
Geocoder::Configuration.lookup = :yahoo
Geocoder.set_lookup :yahoo
Geocoder.send :set_lookup, :yahoo
results = Geocoder.search("Madison Square Garden, New York, NY")
assert_equal "10001", results.first.postal
end
def test_yahoo_address_formatting
Geocoder::Configuration.lookup = :yahoo
Geocoder.set_lookup :yahoo
Geocoder.send :set_lookup, :yahoo
results = Geocoder.search("Madison Square Garden, New York, NY")
assert_equal "Madison Square Garden, New York, NY 10001",
results.first.address
......
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