Skip to content

Commit a24542e

Browse files
committed
Merge pull request #3 from Ziptastic/develop
Add reverse geocoding ability.
2 parents ec9d349 + ec5f885 commit a24542e

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

ziptastic/tests.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
from nose.tools import eq_
44
from json import loads
55

6-
from .ziptastic import Ziptastic
6+
from .ziptastic import Ziptastic, ZiptasticAPIKeyRequiredException
77

8-
compare_v3_json = """[{"city": "Owosso", "country": "US",
9-
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
10-
"postal_code": "48867"}]"""
8+
compare_v3_json = """[{"city": "Owosso", "geohash": "dpshsfsytw8k",
9+
"country": "US", "county": "Shiawassee", "state": "Michigan",
10+
"state_short": "MI", "postal_code": "48867", "latitude": 42.9934,
11+
"longitude": -84.1595, "timezone": "America/Detroit"}]"""
1112

1213
compare_v2_json = """{"city": "Owosso", "country": "US",
1314
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
@@ -40,6 +41,31 @@ def test_get_from_v2_postal_code(self, m):
4041
eq_(url, req.url)
4142
eq_(loads(compare_v2_json), result)
4243

44+
@requests_mock.mock()
45+
def test_reverse_geocoding(self, m):
46+
latitude = '42.9934'
47+
longitude = '-84.1595'
48+
url = 'https://zip.getziptastic.com/v3/' + latitude + '/' + longitude
49+
50+
m.get(url, text=compare_v3_json)
51+
ziptastic = Ziptastic('abc123')
52+
result = ziptastic.get_from_coordinates(latitude, longitude)
53+
54+
req = m.request_history[0]
55+
eq_(url, req.url)
56+
eq_(loads(compare_v3_json), result)
57+
58+
@requests_mock.mock()
59+
def test_reverse_geocoding_api_key(self, m):
60+
latitude = '42.9934'
61+
longitude = '-84.1595'
62+
url = 'https://zip.getziptastic.com/v3/' + latitude + '/' + longitude
63+
64+
m.get(url, text=compare_v3_json)
65+
ziptastic = Ziptastic('')
66+
self.assertRaises(ZiptasticAPIKeyRequiredException,
67+
ziptastic.get_from_coordinates, latitude, longitude)
68+
4369
def test_build_url(self):
4470
version = 'v42'
4571
endpoint = 'test.endpoint'

ziptastic/ziptastic.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import requests
22

33

4+
class ZiptasticAPIKeyRequiredException(Exception):
5+
pass
6+
7+
48
class Ziptastic(object):
59
"""Ziptastic Python Module"""
610

@@ -40,3 +44,24 @@ def get_from_postal_code(self, postal_code, country='US'):
4044
r = requests.get(url, headers=headers)
4145

4246
return r.json()
47+
48+
def get_from_coordinates(self, latitude, longitude):
49+
"""Get geo data from coordinates"""
50+
51+
headers = {}
52+
53+
#: If no api_key is set then default to Version 2 of Ziptastic API.
54+
if self.api_key:
55+
headers.update({
56+
"x-key": self.api_key
57+
})
58+
uri = self.build_url(self.endpoint)
59+
else:
60+
raise ZiptasticAPIKeyRequiredException
61+
62+
url = "{uri}/{latitude}/{longitude}".format(uri=uri, latitude=latitude,
63+
longitude=longitude)
64+
65+
r = requests.get(url, headers=headers)
66+
67+
return r.json()

0 commit comments

Comments
 (0)