Permalink
Browse files

Add error checking for Mapquest lookup

  • Loading branch information...
phallstrom committed Oct 1, 2013
1 parent d780da8 commit 1a73c5ea46d8fe7e72960a689822889472cb4ec1
@@ -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
@@ -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: ..."]
+ }
+}
@@ -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": ["..."]
+ }
+}
@@ -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: ..."]
+ }
+}
@@ -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": []
+ }
}
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

0 comments on commit 1a73c5e

Please sign in to comment.