Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ Official python library for GetZiptastic.com
from ziptastic import Ziptastic

# Set your API key. (Available at https://www.getziptastic.com/dashboard)
Ziptastic.api_key = '<your api key>'
result = Ziptastic.get_from_postal_code('48867')
api = Ziptastic('<your api key>')
result = api.get_from_postal_code('48867')
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ docutils==0.12
funcsigs==0.4
Jinja2==2.8
MarkupSafe==0.23
mock==1.3.0
nose==1.3.7
nose2==0.5.0
pbr==1.8.1
Pygments==2.0.2
pytz==2015.6
requests==2.8.0
requests-mock==0.6.0
six==1.10.0
snowballstemmer==1.2.0
Sphinx==1.3.1
Expand Down
69 changes: 27 additions & 42 deletions ziptastic/tests.py
Original file line number Diff line number Diff line change
@@ -1,62 +1,47 @@
import unittest
from mock import patch, Mock
import requests_mock
from nose.tools import eq_
from json import loads

from ziptastic import Ziptastic

compare_v3_json = ["""[{"city": "Owosso", "country": "US",
compare_v3_json = """[{"city": "Owosso", "country": "US",
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
"postal_code": "48867"}]"""]
"postal_code": "48867"}]"""

compare_v2_json = ["""{"city": "Owosso", "country": "US",
compare_v2_json = """{"city": "Owosso", "country": "US",
"county": "Shiawassee", "state": "Michigan", "state_short": "MI",
"postal_code": "48867"}"""]
"postal_code": "48867"}"""


class TestZiptasticLib(unittest.TestCase):
def setUp(self):
self.patcher = patch('urllib2.urlopen')
self.urllib_mock = self.patcher.start()

self.urllib2_v3_mock = Mock()
self.urllib2_v3_mock.read.side_effect = compare_v3_json
self.urllib_mock.return_value = self.urllib2_v3_mock

def setUpV3(self):
self.urllib_mock.stop()
self.urllib_mock = self.patcher.start()
self.urllib2_v2_mock = Mock()
self.urllib2_v2_mock.read.side_effect = compare_v2_json
self.urllib_mock.return_value = self.urllib2_v2_mock

def tearDown(self):
self.urllib_mock.stop()

def test_get_from_v3_postal_code(self):
@requests_mock.mock()
def test_get_from_v3_postal_code(self, m):
url = 'https://zip.getziptastic.com/v3/US/48867'
m.get(url, text=compare_v3_json)
postal_code = '48867'
Ziptastic.api_key = 'abc123'
result = Ziptastic.get_from_postal_code(postal_code)

self.assertIn('postal_code', result[0])
self.assertEqual('https://zip.getziptastic.com/v3/US/48867',
self.urllib_mock.call_args[0][0].get_full_url())
ziptastic = Ziptastic('abc123')
result = ziptastic.get_from_postal_code(postal_code)

def test_get_from_v2_postal_code(self):
self.setUpV3()
req = m.request_history[0]
eq_(url, req.url)
eq_(loads(compare_v3_json), result)

@requests_mock.mock()
def test_get_from_v2_postal_code(self, m):
url = 'https://zip.getziptastic.com/v2/US/48867'
m.get(url, text=compare_v2_json)
postal_code = '48867'
result = Ziptastic.get_from_postal_code(postal_code)
ziptastic = Ziptastic('')
result = ziptastic.get_from_postal_code(postal_code)

self.assertIn('postal_code', result)
self.assertEqual('https://zip.getziptastic.com/v2/US/48867',
self.urllib_mock.call_args[0][0].get_full_url())
req = m.request_history[0]
eq_(url, req.url)
eq_(loads(compare_v2_json), result)

def test_build_url(self):
version = 'v42'
endpoint = 'test.endpoint'
correct_url = 'https://test.endpoint/v42/'
self.assertEquals(correct_url, Ziptastic.build_url(endpoint,
version=version))

def test_object_is_returned(self):
result = Ziptastic.get_from_postal_code('48867')
self.assertTrue(type(result), 'object')
correct_url = 'https://test.endpoint/v42'
eq_(correct_url, Ziptastic.build_url(endpoint, version=version))
36 changes: 20 additions & 16 deletions ziptastic/ziptastic.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
import urllib2
import json
import requests


class Ziptastic(object):
"""Ziptastic Python Module"""

#: This is your Ziptastic API Key that you can get from
#: https://www.getziptastic.com/dashboard
api_key = ''

#: The current endpoint where Ziptastic APIs are served from.
endpoint = 'zip.getziptastic.com'

def __init__(self, api_key):
"""
Initialize Ziptastic API

:param: This is your Ziptastic API Key that you can get from
https://www.getziptastic.com/dashboard
"""
self.api_key = api_key

@staticmethod
def build_url(endpoint, version='v3', preferred_protocol='https'):
"""Build the Ziptastic API url."""
return preferred_protocol + '://' + endpoint + '/' + version + '/'
return preferred_protocol + '://' + endpoint + '/' + version

@classmethod
def get_from_postal_code(cls, postal_code, country='US'):
def get_from_postal_code(self, postal_code, country='US'):
"""Get geo data from postal code and country code"""
headers = {}

#: If no api_key is set then default to Version 2 of Ziptastic API.
if cls.api_key:
if self.api_key:
headers.update({
"x-key": cls.api_key
"x-key": self.api_key
})
uri = cls.build_url(cls.endpoint)
uri = self.build_url(self.endpoint)
else:
uri = cls.build_url(cls.endpoint, version='v2')
uri = self.build_url(self.endpoint, version='v2')

url = uri + str(country) + '/' + str(postal_code)
url = "{uri}/{country}/{postal_code}".format(uri=uri, country=country,
postal_code=postal_code)

request = urllib2.Request(url, headers=headers)
contents = urllib2.urlopen(request).read()
r = requests.get(url, headers=headers)

#: An object is returned.
return json.loads(contents)
return r.json()