Skip to content

Commit

Permalink
Commit do código inicial.
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed Apr 17, 2014
1 parent 4c16617 commit 4374fbd
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .travis.yml
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
72 changes: 72 additions & 0 deletions postmon.py
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
41 changes: 41 additions & 0 deletions setup.py
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',
)
45 changes: 45 additions & 0 deletions test_postmon.py
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)
7 changes: 7 additions & 0 deletions tox.ini
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

0 comments on commit 4374fbd

Please sign in to comment.