Skip to content

Commit

Permalink
Add PostcodeAnywhere API error handling; Map key limit exceeded, inva…
Browse files Browse the repository at this point in the history
…lid key to Geocode errors and call all others as just Error
  • Loading branch information
rob-murray committed Oct 11, 2014
1 parent abca18f commit 0d93d6c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
18 changes: 15 additions & 3 deletions lib/geocoder/lookups/postcode_anywhere_uk.rb
Expand Up @@ -4,7 +4,7 @@
module Geocoder::Lookup
class PostcodeAnywhereUk < Base

BASE_URL_GEOCODE_V200 = 'services.postcodeanywhere.co.uk/Geocoding/UK/Geocode/v2.00/json.ws'
BASE_URL_GEOCODE_V2_00 = 'services.postcodeanywhere.co.uk/Geocoding/UK/Geocode/v2.00/json.ws'

def name
'PostcodeAnywhereUk'
Expand All @@ -15,7 +15,7 @@ def required_api_key_parts
end

def query_url(query)
format('%s://%s?%s', protocol, BASE_URL_GEOCODE_V200, url_query_string(query))
format('%s://%s?%s', protocol, BASE_URL_GEOCODE_V2_00, url_query_string(query))
end

private
Expand All @@ -24,7 +24,19 @@ def results(query)
response = fetch_data(query)
return [] if response.nil? || !response.is_a?(Array) || response.empty?

return response
raise_exception_for_response(response[0]) if response[0]['Error']
response
end

def raise_exception_for_response(response)
case response['Error']
when '8', '17' # api docs say these two codes are the same error
raise_error(Geocoder::OverQueryLimitError, response['Cause']) || warn(response['Cause'])
when '2'
raise_error(Geocoder::InvalidApiKey, response['Cause']) || warn(response['Cause'])
else # anything else just raise general error with the api cause
raise_error(Geocoder::Error, response['Cause']) || warn(response['Cause'])
end
end

def query_url_params(query)
Expand Down
37 changes: 31 additions & 6 deletions test/unit/lookups/postcode_anywhere_uk_test.rb
Expand Up @@ -9,16 +9,16 @@ def setup
set_api_key!(:postcode_anywhere_uk)
end

def test_result_components
results = Geocoder.search('Madison Square Garden')
def test_result_components_with_placename_search
results = Geocoder.search('Romsey')

assert_equal 1, results.size
assert_equal 'Maidstone, Kent, TQ 76153 55386', results.first.address
assert_equal [51.2703, 0.5238], results.first.coordinates
assert_equal 'Maidstone', results.first.city
assert_equal 'Romsey, Hampshire, SU 35270 21182', results.first.address
assert_equal [50.9889, -1.4989], results.first.coordinates
assert_equal 'Romsey', results.first.city
end

def test_WR26NJ
def test_result_components_with_postcode
results = Geocoder.search('WR26NJ')

assert_equal 1, results.size
Expand All @@ -30,4 +30,29 @@ def test_WR26NJ
def test_no_results
assert_equal [], Geocoder.search('no results')
end

def test_key_limit_exceeded_error
Geocoder.configure(always_raise: [Geocoder::OverQueryLimitError])

assert_raises Geocoder::OverQueryLimitError do
Geocoder.search('key limit exceeded')
end
end

def test_unknown_key_error
Geocoder.configure(always_raise: [Geocoder::InvalidApiKey])

assert_raises Geocoder::InvalidApiKey do
Geocoder.search('unknown key')
end
end

def test_generic_error
Geocoder.configure(always_raise: [Geocoder::Error])

exception = assert_raises(Geocoder::Error) do
Geocoder.search('generic error')
end
assert_equal 'A generic error occured.', exception.message
end
end

0 comments on commit 0d93d6c

Please sign in to comment.