Skip to content
This repository

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

Merged
merged 1 commit into from 9 months ago

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

Showing 1 unique commit by 1 author.

Oct 01, 2013
Philip Hallstrom phallstrom Add error checking for Mapquest lookup 1a73c5e
This page is out of date. Refresh to see the latest.
18 lib/geocoder/lookups/mapquest.rb
@@ -34,9 +34,25 @@ def query_url_params(query)
34 34 params.merge(super)
35 35 end
36 36
  37 + # http://www.mapquestapi.com/geocoding/status_codes.html
  38 + # http://open.mapquestapi.com/geocoding/status_codes.html
37 39 def results(query)
38 40 return [] unless doc = fetch_data(query)
39   - doc["results"][0]['locations']
  41 + return doc["results"][0]['locations'] if doc['info']['statuscode'] == 0 # A successful geocode call
  42 +
  43 + messages = doc['info']['messages'].join
  44 +
  45 + case doc['info']['statuscode']
  46 + when 400 # Error with input
  47 + raise_error(Geocoder::InvalidRequest, messages) ||
  48 + warn("Mapquest Geocoding API error: #{messages}")
  49 + when 403 # Key related error
  50 + raise_error(Geocoder::InvalidApiKey, messages) ||
  51 + warn("Mapquest Geocoding API error: #{messages}")
  52 + when 500 # Unknown error
  53 + raise_error(Geocoder::Error, messages) ||
  54 + warn("Mapquest Geocoding API error: #{messages}")
  55 + end
40 56 end
41 57
42 58 end
16 test/fixtures/mapquest_error
... ... @@ -0,0 +1,16 @@
  1 +{
  2 + "results": [
  3 + {
  4 + "locations": []
  5 + }
  6 + ],
  7 + "info": {
  8 + "copyright": {
  9 + "text": "© 2012 MapQuest, Inc.",
  10 + "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
  11 + "imageAltText": "© 2012 MapQuest, Inc."
  12 + },
  13 + "statuscode": 500,
  14 + "messages": ["Error processing request: ..."]
  15 + }
  16 +}
16 test/fixtures/mapquest_invalid_api_key
... ... @@ -0,0 +1,16 @@
  1 +{
  2 + "results": [
  3 + {
  4 + "locations": []
  5 + }
  6 + ],
  7 + "info": {
  8 + "copyright": {
  9 + "text": "© 2012 MapQuest, Inc.",
  10 + "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
  11 + "imageAltText": "© 2012 MapQuest, Inc."
  12 + },
  13 + "statuscode": 403,
  14 + "messages": ["..."]
  15 + }
  16 +}
16 test/fixtures/mapquest_invalid_request
... ... @@ -0,0 +1,16 @@
  1 +{
  2 + "results": [
  3 + {
  4 + "locations": []
  5 + }
  6 + ],
  7 + "info": {
  8 + "copyright": {
  9 + "text": "© 2012 MapQuest, Inc.",
  10 + "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
  11 + "imageAltText": "© 2012 MapQuest, Inc."
  12 + },
  13 + "statuscode": 400,
  14 + "messages": ["Illegal argument from request: ..."]
  15 + }
  16 +}
11 test/fixtures/mapquest_no_results
@@ -3,5 +3,14 @@
3 3 {
4 4 "locations": []
5 5 }
6   - ]
  6 + ],
  7 + "info": {
  8 + "copyright": {
  9 + "text": "© 2012 MapQuest, Inc.",
  10 + "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
  11 + "imageAltText": "© 2012 MapQuest, Inc."
  12 + },
  13 + "statuscode": 0,
  14 + "messages": []
  15 + }
7 16 }
32 test/services_test.rb
@@ -316,6 +316,38 @@ def test_mapquest_address_formatting
316 316 result.address
317 317 end
318 318
  319 + def test_mapquest_no_results
  320 + Geocoder.configure(:lookup => :mapquest)
  321 + set_api_key!(:mapquest)
  322 + assert_equal [], Geocoder.search("no results")
  323 + end
  324 +
  325 + def test_mapquest_raises_exception_when_invalid_request
  326 + Geocoder.configure(:always_raise => [Geocoder::InvalidRequest])
  327 + l = Geocoder::Lookup.get(:mapquest)
  328 + assert_raises Geocoder::InvalidRequest do
  329 + l.send(:results, Geocoder::Query.new("invalid request"))
  330 + end
  331 + end
  332 +
  333 + def test_mapquest_raises_exception_when_invalid_api_key
  334 + Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
  335 + l = Geocoder::Lookup.get(:mapquest)
  336 + assert_raises Geocoder::InvalidApiKey do
  337 + l.send(:results, Geocoder::Query.new("invalid api key"))
  338 + end
  339 + end
  340 +
  341 + def test_mapquest_raises_exception_when_error
  342 + Geocoder.configure(:always_raise => [Geocoder::Error])
  343 + l = Geocoder::Lookup.get(:mapquest)
  344 + assert_raises Geocoder::Error do
  345 + l.send(:results, Geocoder::Query.new("error"))
  346 + end
  347 + end
  348 +
  349 +
  350 +
319 351 # --- Esri ---
320 352
321 353 def test_esri_query_for_geocode

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.