diff --git a/lib/geocoder.rb b/lib/geocoder.rb index d64d4d880086c4a9ae4726047583a2a9726d122f..372e49c9fb5d89d2268efb5cb56ad786edf9f7a7 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 fd52d470e33a6af1e2bf908aff2527a067560b53..1ddd3c3b9d4c8a434b5b7aa1e0a1a77fab53a5f2 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 724ca5c1558e4a405b45bce68d5d686436e34ebd..4227ba3c35bf7153bc0474b240ca60cc84c46b5c 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 5b621adbb92e8fd77c06b8caad3c2d0f04cecb95..61757a0bf04d9bc910b1663086239568616eec24 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 10fc2b05fdf772eaadae3a9c16993e588f5a4324..752e8760b3047c15ea4350091bd4dfcd1e08142b 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 df5aaad5d4e88c341e1cc78d87105f62a104d6e9..4dfa26e5183f01c9dad0f965e2fc7bb2ff18f07f 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