Permalink
Browse files

Improvement: Remove 2nd parameter from MultiGeocoder (not needed)

  • Loading branch information...
dreamcat4 committed May 20, 2009
1 parent b509191 commit 7beef3f96c0c2caaa2418977fe0b4fa4a56299b3
Showing with 14 additions and 12 deletions.
  1. +5 −1 README.markdown
  2. +5 −7 lib/geokit/geocoders.rb
  3. +4 −4 test/test_multi_ip_geocoder.rb
View
@@ -131,7 +131,11 @@ creates a template with these settings and places it in `config/initializers/geo
* Geoplugin.net -- another IP address geocoder
### The Multigeocoder
-* Multi Geocoder - provides failover for the physical location geocoders, and also IP address geocoders.
+Multi Geocoder - provides failover for the physical location geocoders, and also IP address geocoders. Its configured by setting Geokit::Geocoders::provider_order, and Geokit::Geocoders::ip_provider_order. You should call the Multi-Geocoder with its :geocode method, supplying one address parameter which is either a real street address, or an ip address. For example:
+
+ Geokit::Geocoders::MultiGeocoder.geocode("900 Sycamore Drive")
+
+ Geokit::Geocoders::MultiGeocoder.geocode("12.12.12.12")
## MULTIPLE RESULTS
Some geocoding services will return multple results if the there isn't one clear result.
View
@@ -555,20 +555,18 @@ def self.parse_body(body) # :nodoc:
# Limitations:
# - currently only provides the first result. Sometimes geocoders will return multiple results.
# - currently discards the "accuracy" component of the geocoding calls
- class MultiGeocoder < Geocoder
-
- def self.geocode(address, geocode_ip=false)
- self.do_geocode(address, geocode_ip)
- end
-
+ class MultiGeocoder < Geocoder
+
private
# This method will call one or more geocoders in the order specified in the
# configuration until one of the geocoders work.
#
# The failover approach is crucial for production-grade apps, but is rarely used.
# 98% of your geocoding calls will be successful with the first call
- def self.do_geocode(address, geocode_ip=false)
+ def self.do_geocode(address)
+ geocode_ip = !!/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})?$/.match(address)
provider_order = geocode_ip ? Geokit::Geocoders::ip_provider_order : Geokit::Geocoders::provider_order
+
provider_order.each do |provider|
begin
klass = Geokit::Geocoders.const_get "#{Geokit::Inflector::camelize(provider.to_s)}Geocoder"
@@ -13,25 +13,25 @@ def setup
def test_successful_first
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@success)
- assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address,true)
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
end
def test_failover
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@success)
- assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address,true)
+ assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
end
def test_failure
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address).returns(@failure)
- assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address,true)
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
end
def test_invalid_provider
temp = Geokit::Geocoders::ip_provider_order
Geokit::Geocoders.ip_provider_order = [:bogus]
- assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address,true)
+ assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@ip_address)
Geokit::Geocoders.ip_provider_order = temp
end

0 comments on commit 7beef3f

Please sign in to comment.