-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4c16617
commit 4374fbd
Showing
5 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
language: python | ||
|
||
python: | ||
- "2.6" | ||
- "2.7" | ||
- "3.2" | ||
- "3.3" | ||
- "pypy" | ||
|
||
script: | ||
- python setup.py test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# coding: utf-8 | ||
import requests | ||
|
||
BASE_URL = 'http://api.postmon.com.br/v1' | ||
|
||
|
||
class Cidade(object): | ||
|
||
def __init__(self, nome, area_km2=None, codigo_ibge=None): | ||
self.nome = nome | ||
self.area_km2 = area_km2 | ||
self.codigo_ibge = codigo_ibge | ||
|
||
def __repr__(self): | ||
return '<postmon.Cidade %r>' % self.nome | ||
|
||
def __str__(self): | ||
return self.nome | ||
|
||
|
||
class Estado(object): | ||
|
||
def __init__(self, uf, nome=None, area_km2=None, codigo_ibge=None): | ||
self.uf = uf | ||
self.nome = nome | ||
self.area_km2 = area_km2 | ||
self.codigo_ibge = codigo_ibge | ||
|
||
def __repr__(self): | ||
return '<postmon.Estado %r>' % self.uf | ||
|
||
def __str__(self): | ||
return self.uf | ||
|
||
|
||
class Endereco(object): | ||
|
||
def __init__(self, **kwargs): | ||
self.cep = kwargs['cep'] | ||
self.logradouro = kwargs.get('logradouro') | ||
self.bairro = kwargs.get('bairro') | ||
|
||
estado_info = kwargs.get('estado_info', {}) | ||
self.estado = Estado(kwargs['estado'], | ||
estado_info.get('nome'), | ||
estado_info.get('area_km2'), | ||
estado_info.get('codigo_ibge')) | ||
|
||
cidade_info = kwargs.get('cidade_info', {}) | ||
self.cidade = Cidade(kwargs['cidade'], | ||
cidade_info.get('area_km2'), | ||
cidade_info.get('codigo_ibge')) | ||
|
||
def __repr__(self): | ||
return '<postmon.Endereco %r>' % self.cep | ||
|
||
def __str__(self): | ||
return '%s, %s - %s, %s - CEP: %s' % (self.logradouro, self.bairro, | ||
self.cidade, self.estado, | ||
self.cep) | ||
|
||
|
||
def buscar_cep(cep): | ||
response = _GET('/cep/%s' % cep) | ||
response.raise_for_status() | ||
return Endereco(**response.json()) | ||
|
||
|
||
def _GET(endpoint): | ||
url = '%s%s' % (BASE_URL, endpoint) | ||
response = requests.get(url) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
try: | ||
import multiprocessing | ||
except ImportError: | ||
pass | ||
|
||
from setuptools import setup | ||
|
||
|
||
setup( | ||
name='postmon', | ||
version='0.1.1', | ||
description='Postmon service wrapper', | ||
url='http://github.com/PostmonAPI/postmon-python', | ||
|
||
author='Iuri de Silvio', | ||
author_email='[email protected]', | ||
license='MIT', | ||
|
||
py_modules=['postmon'], | ||
|
||
install_requires=[ | ||
'requests>=1.0', | ||
], | ||
|
||
classifiers=[ | ||
'Development Status :: 3 - Alpha', | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: MIT License', | ||
'Natural Language :: Portuguese (Brazilian)', | ||
'Operating System :: OS Independent', | ||
'Programming Language :: Python :: 2', | ||
'Programming Language :: Python :: 3', | ||
'Topic :: Software Development :: Libraries :: Python Modules', | ||
], | ||
|
||
tests_require=[ | ||
'nose>=1.0', | ||
'httpretty', | ||
], | ||
test_suite='nose.collector', | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import unittest | ||
import json | ||
|
||
import httpretty | ||
import postmon | ||
|
||
|
||
response = { | ||
"bairro": "Bairro B", | ||
"cidade": "Cidade C", | ||
"cep": "11111111", | ||
"logradouro": "Logradouro L", | ||
"estado_info": { | ||
"area_km2": "999.999,001", | ||
"codigo_ibge": "35", | ||
"nome": "Estado E" | ||
}, | ||
"cidade_info": { | ||
"area_km2": "1099,409", | ||
"codigo_ibge": "3549904" | ||
}, | ||
"estado": "SP" | ||
} | ||
|
||
|
||
class TestValidCep(unittest.TestCase): | ||
|
||
def setUp(self): | ||
httpretty.enable() | ||
url = '%s/cep/11111111' % postmon.BASE_URL | ||
httpretty.register_uri(httpretty.GET, url, body=json.dumps(response)) | ||
self.endereco = postmon.buscar_cep('11111111') | ||
|
||
def tearDown(self): | ||
httpretty.disable() | ||
httpretty.reset() | ||
|
||
def test_cep(self): | ||
self.assertEqual('11111111', self.endereco.cep) | ||
|
||
def test_logradouro(self): | ||
self.assertEqual('Logradouro L', self.endereco.logradouro) | ||
|
||
def test_bairro(self): | ||
self.assertEqual('Bairro B', self.endereco.bairro) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[tox] | ||
envlist = py26,py27,py32,py33,pypy | ||
|
||
[testenv] | ||
commands={envpython} setup.py install | ||
{envpython} setup.py test | ||
sitepackages=False |