From 3552f27de7557ea891846b87d67c43b9ec45ff67 Mon Sep 17 00:00:00 2001 From: Guido Fioravantti Date: Thu, 5 Nov 2015 11:12:10 +0100 Subject: [PATCH] Code refactor --- lib/python/heremaps/heremapsexceptions.py | 4 ++ lib/python/heremaps/heremapsgeocoder.py | 20 ++++++---- .../heremaps/tests/heremapsgeocoder_tests.py | 38 +++++++++++++------ 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/lib/python/heremaps/heremapsexceptions.py b/lib/python/heremaps/heremapsexceptions.py index 8763ce7..6416872 100644 --- a/lib/python/heremaps/heremapsexceptions.py +++ b/lib/python/heremaps/heremapsexceptions.py @@ -16,3 +16,7 @@ class NoGeocodingParams(Exception): class EmptyGeocoderResponse(Exception): def __str__(self): return repr('The request could not be geocoded') + +class MalformedResult(Exception): + def __str__(self): + return repr('Result structure is malformed') diff --git a/lib/python/heremaps/heremapsgeocoder.py b/lib/python/heremaps/heremapsgeocoder.py index d1b4774..696c79f 100644 --- a/lib/python/heremaps/heremapsgeocoder.py +++ b/lib/python/heremaps/heremapsgeocoder.py @@ -7,12 +7,14 @@ import urllib from heremaps.heremapsexceptions import BadGeocodingParams from heremaps.heremapsexceptions import EmptyGeocoderResponse from heremaps.heremapsexceptions import NoGeocodingParams +from heremaps.heremapsexceptions import MalformedResult class Geocoder: 'A Here Maps Geocoder wrapper for python' URL_GEOCODE_JSON = 'http://geocoder.api.here.com/6.2/geocode.json' DEFAULT_MAXRESULTS = 1 + DEFAULT_GEN = 9 ADDRESS_PARAMS = [ 'city', @@ -50,16 +52,17 @@ class Geocoder: app_code = '' maxresults = '' - def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS): + def __init__(self, app_id, app_code, maxresults=DEFAULT_MAXRESULTS, gen=DEFAULT_GEN): self.app_id = app_id self.app_code = app_code self.maxresults = maxresults + self.gen = gen def geocode(self, params): if not set(params.keys()).issubset(set(self.ADDRESS_PARAMS)): raise BadGeocodingParams(params) - response = self.performRequest(params) + response = self.perform_request(params) try: results = response['Response']['View'][0]['Result'] @@ -68,12 +71,12 @@ class Geocoder: return results - def performRequest(self, params): + def perform_request(self, params): request_params = { 'app_id' : self.app_id, 'app_code' : self.app_code, 'maxresults' : self.maxresults, - 'gen' : '9' + 'gen' : self.gen } request_params.update(params) @@ -86,7 +89,7 @@ class Geocoder: return response - def geocodeAddress(self, **kwargs): + def geocode_address(self, **kwargs): params = {} for key, value in kwargs.iteritems(): if value: params[key] = value @@ -95,8 +98,11 @@ class Geocoder: return self.geocode(params) - def extractLngLatFromResult(self, result): - location = result['Location'] + def extract_lng_lat_from_result(self, result): + try: + location = result['Location'] + except KeyError: + raise MalformedResult() longitude = location['DisplayPosition']['Longitude'] latitude = location['DisplayPosition']['Latitude'] diff --git a/lib/python/heremaps/tests/heremapsgeocoder_tests.py b/lib/python/heremaps/tests/heremapsgeocoder_tests.py index 58d3ae2..6fbd1c0 100644 --- a/lib/python/heremaps/tests/heremapsgeocoder_tests.py +++ b/lib/python/heremaps/tests/heremapsgeocoder_tests.py @@ -7,6 +7,7 @@ from heremaps import heremapsgeocoder from heremaps.heremapsexceptions import BadGeocodingParams from heremaps.heremapsexceptions import EmptyGeocoderResponse from heremaps.heremapsexceptions import NoGeocodingParams +from heremaps.heremapsexceptions import MalformedResult from secrets import * @@ -40,8 +41,8 @@ class GeocoderTestCase(unittest.TestCase): "LocationType":"address", "DisplayPosition":{ "Latitude":40.43433, - "Longitude":-3.70126 - }, + "Longitude":-3.70126 + }, "NavigationPosition":[{ "Latitude":40.43433, "Longitude":-3.70126 @@ -86,27 +87,40 @@ class GeocoderTestCase(unittest.TestCase): def setUp(self): self.geocoder = heremapsgeocoder.Geocoder(None, None) - def test_geocodeAddress_with_valid_params(self): - self.geocoder.performRequest = lambda x: self.GOOD_RESPONSE - response = self.geocoder.geocodeAddress( + def test_geocode_address_with_valid_params(self): + self.geocoder.perform_request = lambda x: self.GOOD_RESPONSE + response = self.geocoder.geocode_address( searchtext='Calle Eloy Gonzalo 27', city='Madrid', country='España') - def test_geocodeAddress_with_invalid_params(self): + def test_geocode_address_with_invalid_params(self): with self.assertRaises(BadGeocodingParams): - self.geocoder.geocodeAddress( + self.geocoder.geocode_address( searchtext='Calle Eloy Gonzalo 27', manolo='escobar') - def test_geocodeAddress_with_no_params(self): + def test_geocode_address_with_no_params(self): with self.assertRaises(NoGeocodingParams): - self.geocoder.geocodeAddress() + self.geocoder.geocode_address() - def test_geocodeAddress_empty_response(self): - self.geocoder.performRequest = lambda x: self.EMPTY_RESPONSE + def test_geocode_address_empty_response(self): + self.geocoder.perform_request = lambda x: self.EMPTY_RESPONSE with self.assertRaises(EmptyGeocoderResponse): - self.geocoder.geocodeAddress(searchtext='lkajfñlasjfñ') + self.geocoder.geocode_address(searchtext='lkajfñlasjfñ') + + def test_extract_lng_lat_from_result(self): + result = self.GOOD_RESPONSE['Response']['View'][0]['Result'][0] + coordinates = self.geocoder.extract_lng_lat_from_result(result) + + self.assertEqual(coordinates[0], -3.70126) + self.assertEqual(coordinates[1], 40.43433) + + def test_extract_lng_lat_from_result_with_malformed_result(self): + result = {'manolo':'escobar'} + + with self.assertRaises(MalformedResult): + self.geocoder.extract_lng_lat_from_result(result) if __name__ == '__main__': main()