Skip to content
Snippets Groups Projects
Commit 1a73c5ea authored by Philip Hallstrom's avatar Philip Hallstrom
Browse files

Add error checking for Mapquest lookup

parent d780da88
No related branches found
No related tags found
No related merge requests found
......@@ -34,9 +34,25 @@ module Geocoder::Lookup
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
......
{
"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: ..."]
}
}
{
"results": [
{
"locations": []
}
],
"info": {
"copyright": {
"text": "© 2012 MapQuest, Inc.",
"imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
"imageAltText": "© 2012 MapQuest, Inc."
},
"statuscode": 403,
"messages": ["..."]
}
}
{
"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": []
}
}
......@@ -316,6 +316,38 @@ class ServicesTest < Test::Unit::TestCase
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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment