From e00981f6b421b2fe5d891a62a72ff5bc9eee44e5 Mon Sep 17 00:00:00 2001 From: Leonardo Gregianin Date: Tue, 29 Mar 2016 11:37:02 -0400 Subject: [PATCH] Primeiro commit --- .travis.yml | 11 +++++++++++ README.md | 5 +++++ test_viacep.py | 12 ++++++++++++ viacep.py | 26 ++++++++++++++++++++++++++ 4 files changed, 54 insertions(+) create mode 100644 .travis.yml create mode 100644 README.md create mode 100644 test_viacep.py create mode 100644 viacep.py diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..660e440 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,11 @@ +language: python +python: + - "2.6" + - "2.7" + - "3.2" + - "3.3" + - "3.4" +script: + - python test_viacep.py +notifications: + email: false diff --git a/README.md b/README.md new file mode 100644 index 0000000..c49d77e --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# # ViaCEP em Python + +[![Build Status](https://travis-ci.org/leogregianin/viacep-python.svg)](https://travis-ci.org/leogregianin/viacep-python) + +## Buscar informações do CEP com o webservice do site viacep.com.br diff --git a/test_viacep.py b/test_viacep.py new file mode 100644 index 0000000..4604b0b --- /dev/null +++ b/test_viacep.py @@ -0,0 +1,12 @@ + +import unittest +import viacep + +class TestCase(unittest.TestCase): + + def test(self): + test = viacep.ViaCEP('78048000') + self.assertEqual(test.retorna_uf(), 'MT') + +if __name__ == '__main__': + unittest.main() diff --git a/viacep.py b/viacep.py new file mode 100644 index 0000000..1d5d50a --- /dev/null +++ b/viacep.py @@ -0,0 +1,26 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import json +import requests + +class ViaCEP: + + def __init__(self, cep): + self.cep = cep + + def retorna_json_completo(self): + url_api = ('http://www.viacep.com.br/ws/%s/json' % self.cep) + req = requests.get(url_api) + dados_json = json.loads(req.text) + return dados_json + + def retorna_uf(self): + url_api = ('http://www.viacep.com.br/ws/%s/json' % self.cep) + req = requests.get(url_api) + dados_json = json.loads(req.text) + return dados_json['uf'] + +if __name__ == '__main__': + test1 = ViaCEP('78048000') + print(u'%s' % test1.retorna_json_completo())