Skip to content

Commit

Permalink
Fix MultiGeocoder with geocoders that only have one argument (e.g. Bing)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnoack committed Dec 31, 2013
1 parent 1111283 commit 9446d23
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.8.3

* Fix MultiGeocoder with geocoders that only have one argument

## 1.8.2

* Fix due to name clash with dependency definitions in geokit 1.8.1
Expand Down
4 changes: 2 additions & 2 deletions lib/geokit/multi_geocoder.rb
Expand Up @@ -22,14 +22,14 @@ class MultiGeocoder < Geocoder
#
# 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, options = {})
def self.do_geocode(address, *args)
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|
klass = geocoder(provider)
begin
res = klass.send :geocode, address, options
res = klass.send :geocode, address, *args
return res if res.success?
rescue => e
logger.error("An error has occurred during geocoding: #{e}\nAddress: #{address}. Provider: #{provider}")
Expand Down
2 changes: 1 addition & 1 deletion lib/geokit/version.rb
@@ -1,3 +1,3 @@
module Geokit
VERSION = '1.8.2'
VERSION = '1.8.3'
end
28 changes: 14 additions & 14 deletions test/test_multi_geocoder.rb
@@ -1,6 +1,6 @@
require File.join(File.dirname(__FILE__), 'helper')

Geokit::Geocoders::provider_order=[:google,:yahoo,:us]
Geokit::Geocoders::provider_order=[:google, :bing, :us]

class MultiGeocoderTest < BaseGeocoderTest #:nodoc: all

Expand All @@ -10,27 +10,27 @@ def setup
end

def test_successful_first
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address, {}).returns(@success)
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@success)
assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
end

def test_failover
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address, {}).returns(@success)
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
Geokit::Geocoders::BingGeocoder.expects(:geocode).with(@address).returns(@success)
assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
end

def test_double_failover
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address, {}).returns(@success)
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
Geokit::Geocoders::BingGeocoder.expects(:geocode).with(@address).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address).returns(@success)
assert_equal @success, Geokit::Geocoders::MultiGeocoder.geocode(@address)
end

def test_failure
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address, {}).returns(@failure)
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with(@address).returns(@failure)
Geokit::Geocoders::BingGeocoder.expects(:geocode).with(@address).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:geocode).with(@address).returns(@failure)
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode(@address)
end

Expand All @@ -45,7 +45,7 @@ def test_blank_address
t1, t2 = Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order # will need to reset after
Geokit::Geocoders.provider_order = [:google]
Geokit::Geocoders.ip_provider_order = [:geo_plugin]
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with("", {}).returns(@failure)
Geokit::Geocoders::GoogleGeocoder.expects(:geocode).with("").returns(@failure)
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).never
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.geocode("")
Geokit::Geocoders.provider_order, Geokit::Geocoders.ip_provider_order = t1, t2 # reset to orig values
Expand All @@ -58,20 +58,20 @@ def test_reverse_geocode_successful_first

def test_reverse_geocode_failover
Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
Geokit::Geocoders::BingGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
assert_equal @success, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
end

def test_reverse_geocode_double_failover
Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::BingGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:reverse_geocode).with(@latlng).returns(@success)
assert_equal @success, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
end

def test_reverse_geocode_failure
Geokit::Geocoders::GoogleGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::YahooGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::BingGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
Geokit::Geocoders::UsGeocoder.expects(:reverse_geocode).with(@latlng).returns(@failure)
assert_equal @failure, Geokit::Geocoders::MultiGeocoder.reverse_geocode(@latlng)
end
Expand Down
10 changes: 5 additions & 5 deletions test/test_multi_ip_geocoder.rb
Expand Up @@ -12,19 +12,19 @@ def setup
end

def test_successful_first
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address, {}).returns(@success)
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address).returns(@success)
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)
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)
end

def test_failure
Geokit::Geocoders::GeoPluginGeocoder.expects(:geocode).with(@ip_address, {}).returns(@failure)
Geokit::Geocoders::IpGeocoder.expects(:geocode).with(@ip_address, {}).returns(@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)
end

Expand Down

0 comments on commit 9446d23

Please sign in to comment.