diff --git a/README.md b/README.md index f495c83..1e59fb1 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,33 @@ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) # Go ViaCep + ##### This lib was developed for get an adress data by zipcode using [ViaCep](https://viacep.com.br/) API ## Import and Usage + ``` go get github.com/WalterPaes/go-viacep ``` ### Create a New Integration -```go -// Parse zipcode to CEP type -zipcode := cep.CEP("01001-000") -// Create a new integration -integration := viacep.NewIntegration(zipcode) +```go +viaCep, err := NewViaCEP("01001-000") ``` ### Get Address + ```go -integration.GetAddress() +address, err := viaCep.GetAddress() ``` ## :rocket: Technologies This project was developed with the following technologies: -- [Go](https://golang.org/) -- [GoLand](https://www.jetbrains.com/go/?gclid=EAIaIQobChMI5-ug_OvG6gIVBgiRCh0GGARZEAAYASAAEgKOSPD_BwE) -- [ViaCep](https://viacep.com.br/) +- [Go](https://golang.org/) +- [GoLand](https://www.jetbrains.com/go/?gclid=EAIaIQobChMI5-ug_OvG6gIVBgiRCh0GGARZEAAYASAAEgKOSPD_BwE) +- [ViaCep](https://viacep.com.br/) Made by [Walter Junior](https://www.linkedin.com/in/walter-paes/) diff --git a/cep/address.go b/cep/address.go deleted file mode 100644 index fe71bee..0000000 --- a/cep/address.go +++ /dev/null @@ -1,15 +0,0 @@ -package cep - -type CEP string - -type Address struct { - CEP `json:"cep"` - Logradouro string `json:"logradouro"` - Complemento string `json:"complemento"` - Bairro string `json:"bairro"` - Localidade string `json:"localidade"` - UF string `json:"uf"` - Unidade string `json:"unidade,omitempty"` - IBGE string `json:"ibge,omitempty"` - Gia string `json:"gia,omitempty"` -} diff --git a/tests/viacep_integration_test.go b/tests/viacep_integration_test.go deleted file mode 100644 index c2e3211..0000000 --- a/tests/viacep_integration_test.go +++ /dev/null @@ -1,40 +0,0 @@ -package tests - -import ( - "github.com/WalterPaes/go-viacep/cep" - "github.com/WalterPaes/go-viacep/viacep" - "testing" -) - -func TestViaCepIntegration(t *testing.T) { - t.Run("SUCESS: Get a address by cep", func(t *testing.T) { - zipcode := cep.CEP("01001-000") - integration := viacep.NewIntegration(zipcode) - - expectedZipcode := zipcode - expectedAddress := "Praça da Sé" - - address, err := integration.GetAddress() - if err != nil { - t.Fatal("Errors was not expected!", err.Error()) - } - - if address.Logradouro != expectedAddress { - t.Errorf("Was expected '%s', but got '%s'", expectedAddress, address.Logradouro) - } - - if address.CEP != expectedZipcode { - t.Errorf("Was expected '%s', but got '%s'", expectedZipcode, address.CEP) - } - }) - - t.Run("ERROR: Invalid zipcode cause a bad request", func(t *testing.T) { - zipcode := cep.CEP("01001") - integration := viacep.NewIntegration(zipcode) - - _, err := integration.GetAddress() - if err == nil { - t.Fatal("Errors was expected!") - } - }) -} diff --git a/viacep/viacep.go b/viacep/viacep.go index 9eac7af..33dc8ef 100644 --- a/viacep/viacep.go +++ b/viacep/viacep.go @@ -2,50 +2,86 @@ package viacep import ( "encoding/json" - "errors" "fmt" - "github.com/WalterPaes/go-viacep/cep" "io/ioutil" "log" "net/http" + "regexp" ) -// Integration represents viacep integration -type Integration struct { - cep cep.CEP +const ( + cepNotFoundError = "CEP '%s' was not found" + invalidCepError = "'%s' is an invalid CEP" +) + +type Address struct { + CEP string `json:"cep"` + Logradouro string `json:"logradouro"` + Complemento string `json:"complemento"` + Bairro string `json:"bairro"` + Localidade string `json:"localidade"` + UF string `json:"uf"` + Unidade string `json:"unidade,omitempty"` + IBGE string `json:"ibge,omitempty"` + Gia string `json:"gia,omitempty"` +} + +type ViaCep struct { + cep string } -func NewIntegration(cep cep.CEP) *Integration { - return &Integration{cep: cep} +func NewViaCEP(cep string) (*ViaCep, error) { + if err := validate(cep); err != nil { + log.Printf("[VIACEP] VALIDATION ERROR: %s \n", err.Error()) + return nil, err + } + + return &ViaCep{ + cep: cep, + }, nil } -func (vcs Integration) GetAddress() (*cep.Address, error) { - url := fmt.Sprintf("https://viacep.com.br/ws/%v/json/", vcs.cep) +func (vc ViaCep) GetAddress() (*Address, error) { + url := fmt.Sprintf("https://viacep.com.br/ws/%v/json/", vc.cep) resp, err := http.Get(url) if err != nil { - log.Printf("[VIACEP] ERROR: %s \n", err.Error()) + log.Printf("[VIACEP] REQUEST ERROR: %s \n", err.Error()) return nil, err } if resp.StatusCode != http.StatusOK { - log.Printf("[VIACEP] ERROR: " + resp.Status) - return nil, errors.New(resp.Status) + notFoundError := fmt.Errorf(cepNotFoundError, vc.cep) + log.Printf("[VIACEP] NOT FOUND ERROR: " + notFoundError.Error()) + return nil, notFoundError } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - log.Printf("[VIACEP] ERROR: %s \n", err.Error()) + log.Printf("[VIACEP] BODY PARSE ERROR: %s \n", err.Error()) return nil, err } - var address *cep.Address + var address *Address err = json.Unmarshal(body, &address) if err != nil { - log.Printf("[VIACEP] ERROR: %s \n", err.Error()) + log.Printf("[VIACEP] JSON PARSE ERROR: %s \n", err.Error()) return nil, err } return address, err } + +func validate(cep string) error { + reg, err := regexp.Compile("[^0-9]+") + if err != nil { + return err + } + + cep = reg.ReplaceAllString(cep, "") + if len(cep) < 7 || len(cep) > 8 { + return fmt.Errorf(invalidCepError, cep) + } + return nil +} \ No newline at end of file diff --git a/viacep/viacep_integration_test.go b/viacep/viacep_integration_test.go new file mode 100644 index 0000000..74a63a9 --- /dev/null +++ b/viacep/viacep_integration_test.go @@ -0,0 +1,77 @@ +package viacep + +import ( + "fmt" + "testing" +) + +func TestViaCepIntegration(t *testing.T) { + t.Run("SUCESS: Get a address by cep", func(t *testing.T) { + cep := "01001-000" + viaCep, err := NewViaCEP(cep) + if err != nil { + t.Fatal("Errors was not expected!", err.Error()) + } + + address, err := viaCep.GetAddress() + if err != nil { + t.Fatal("Errors was not expected!", err.Error()) + } + + if address.CEP != cep { + t.Errorf("Was expected '%s', but got '%s'", cep, address.CEP) + } + + expectedAddress := Address{ + Logradouro: "Praça da Sé", + Complemento: "lado ímpar", + Bairro: "Sé", + Localidade: "São Paulo", + UF: "SP", + } + + if address.Logradouro != expectedAddress.Logradouro { + t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Logradouro, address.Logradouro) + } + + if address.Complemento != expectedAddress.Complemento { + t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Complemento, address.Complemento) + } + + if address.Bairro != expectedAddress.Bairro { + t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Bairro, address.Bairro) + } + + if address.Localidade != expectedAddress.Localidade { + t.Errorf("Was expected '%s', but got '%s'", expectedAddress.Localidade, address.Localidade) + } + + if address.UF != expectedAddress.UF { + t.Errorf("Was expected '%s', but got '%s'", expectedAddress.UF, address.UF) + } + }) + + t.Run("ERROR: Not found zipcode", func(t *testing.T) { + cep := "1234567" + viaCep, err := NewViaCEP(cep) + if err != nil { + t.Fatal("Errors was not expected!", err.Error()) + } + + _, err = viaCep.GetAddress() + notFoundError := fmt.Errorf(cepNotFoundError, cep) + if err.Error() != notFoundError.Error() { + t.Errorf("Was expected '%s', but got '%s'", notFoundError.Error(), err.Error()) + } + }) + + t.Run("ERROR: Invalid zipcode", func(t *testing.T) { + cep := "12345" + _, err := NewViaCEP(cep) + + invalidError := fmt.Errorf(invalidCepError, cep) + if err.Error() != invalidError.Error() { + t.Errorf("Was expected '%s', but got '%s'", invalidError.Error(), err.Error()) + } + }) +}