From 56748c74b8d60a9607795fadfd3f3e26decd30c6 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Thu, 3 Mar 2011 23:57:43 -0500 Subject: [PATCH] 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). --- lib/geocoder.rb | 30 ++++++++++++++++++++++++++---- lib/geocoder/active_record.rb | 6 +++--- lib/geocoder/lookups/base.rb | 22 +++++++++------------- lib/geocoder/lookups/google.rb | 4 ---- lib/geocoder/lookups/yahoo.rb | 4 ---- test/geocoder_test.rb | 6 +++--- 6 files changed, 41 insertions(+), 31 deletions(-) diff --git a/lib/geocoder.rb b/lib/geocoder.rb index d64d4d88..372e49c9 100644 --- a/lib/geocoder.rb +++ b/lib/geocoder.rb @@ -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 diff --git a/lib/geocoder/active_record.rb b/lib/geocoder/active_record.rb index fd52d470..1ddd3c3b 100644 --- a/lib/geocoder/active_record.rb +++ b/lib/geocoder/active_record.rb @@ -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 diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 724ca5c1..4227ba3c 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -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 # diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index 5b621adb..61757a0b 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -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" diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb index 10fc2b05..752e8760 100644 --- a/lib/geocoder/lookups/yahoo.rb +++ b/lib/geocoder/lookups/yahoo.rb @@ -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 diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb index df5aaad5..4dfa26e5 100644 --- a/test/geocoder_test.rb +++ b/test/geocoder_test.rb @@ -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 -- GitLab