Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add error checking for Mapquest lookup (tests included) #528

Merged
merged 1 commit into from

2 participants

Philip Hallstrom Alex Reisner
Philip Hallstrom

Mapquest returns some status information that we can examine to see if the request was successful. It's the same status for both the licensed and open apis.

http://www.mapquestapi.com/geocoding/status_codes.html
http://open.mapquestapi.com/geocoding/status_codes.html

Patch adds checks for the status and raises (or warns) with the relevant error (modeled after the Google lookup).

Alex Reisner alexreisner merged commit 2c9ef68 into from
Alex Reisner
Owner

Awesome, thanks!

Philip Hallstrom

Thanks for the quick response! Any idea when you'll push this out onto ruby gems? Or should I grab it direct from github?

Alex Reisner
Owner

Hoping to do a release in October but no promises!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 1, 2013
  1. Philip Hallstrom
This page is out of date. Refresh to see the latest.
18 lib/geocoder/lookups/mapquest.rb
View
@@ -34,9 +34,25 @@ def query_url_params(query)
params.merge(super)
end
+ # http://www.mapquestapi.com/geocoding/status_codes.html
+ # http://open.mapquestapi.com/geocoding/status_codes.html
def results(query)
return [] unless doc = fetch_data(query)
- doc["results"][0]['locations']
+ return doc["results"][0]['locations'] if doc['info']['statuscode'] == 0 # A successful geocode call
+
+ messages = doc['info']['messages'].join
+
+ case doc['info']['statuscode']
+ when 400 # Error with input
+ raise_error(Geocoder::InvalidRequest, messages) ||
+ warn("Mapquest Geocoding API error: #{messages}")
+ when 403 # Key related error
+ raise_error(Geocoder::InvalidApiKey, messages) ||
+ warn("Mapquest Geocoding API error: #{messages}")
+ when 500 # Unknown error
+ raise_error(Geocoder::Error, messages) ||
+ warn("Mapquest Geocoding API error: #{messages}")
+ end
end
end
16 test/fixtures/mapquest_error
View
@@ -0,0 +1,16 @@
+{
+ "results": [
+ {
+ "locations": []
+ }
+ ],
+ "info": {
+ "copyright": {
+ "text": "© 2012 MapQuest, Inc.",
+ "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
+ "imageAltText": "© 2012 MapQuest, Inc."
+ },
+ "statuscode": 500,
+ "messages": ["Error processing request: ..."]
+ }
+}
16 test/fixtures/mapquest_invalid_api_key
View
@@ -0,0 +1,16 @@
+{
+ "results": [
+ {
+ "locations": []
+ }
+ ],
+ "info": {
+ "copyright": {
+ "text": "© 2012 MapQuest, Inc.",
+ "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
+ "imageAltText": "© 2012 MapQuest, Inc."
+ },
+ "statuscode": 403,
+ "messages": ["..."]
+ }
+}
16 test/fixtures/mapquest_invalid_request
View
@@ -0,0 +1,16 @@
+{
+ "results": [
+ {
+ "locations": []
+ }
+ ],
+ "info": {
+ "copyright": {
+ "text": "© 2012 MapQuest, Inc.",
+ "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
+ "imageAltText": "© 2012 MapQuest, Inc."
+ },
+ "statuscode": 400,
+ "messages": ["Illegal argument from request: ..."]
+ }
+}
11 test/fixtures/mapquest_no_results
View
@@ -3,5 +3,14 @@
{
"locations": []
}
- ]
+ ],
+ "info": {
+ "copyright": {
+ "text": "© 2012 MapQuest, Inc.",
+ "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
+ "imageAltText": "© 2012 MapQuest, Inc."
+ },
+ "statuscode": 0,
+ "messages": []
+ }
}
32 test/services_test.rb
View
@@ -316,6 +316,38 @@ def test_mapquest_address_formatting
result.address
end
+ def test_mapquest_no_results
+ Geocoder.configure(:lookup => :mapquest)
+ set_api_key!(:mapquest)
+ assert_equal [], Geocoder.search("no results")
+ end
+
+ def test_mapquest_raises_exception_when_invalid_request
+ Geocoder.configure(:always_raise => [Geocoder::InvalidRequest])
+ l = Geocoder::Lookup.get(:mapquest)
+ assert_raises Geocoder::InvalidRequest do
+ l.send(:results, Geocoder::Query.new("invalid request"))
+ end
+ end
+
+ def test_mapquest_raises_exception_when_invalid_api_key
+ Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
+ l = Geocoder::Lookup.get(:mapquest)
+ assert_raises Geocoder::InvalidApiKey do
+ l.send(:results, Geocoder::Query.new("invalid api key"))
+ end
+ end
+
+ def test_mapquest_raises_exception_when_error
+ Geocoder.configure(:always_raise => [Geocoder::Error])
+ l = Geocoder::Lookup.get(:mapquest)
+ assert_raises Geocoder::Error do
+ l.send(:results, Geocoder::Query.new("error"))
+ end
+ end
+
+
+
# --- Esri ---
def test_esri_query_for_geocode
Something went wrong with that request. Please try again.