Skip to content

Instantly share code, notes, and snippets.

@argentinaluiz
Created December 31, 2018 11:40
Show Gist options
  • Select an option

  • Save argentinaluiz/0df59e611c0a4f527605ea8f9b574bd0 to your computer and use it in GitHub Desktop.

Select an option

Save argentinaluiz/0df59e611c0a4f527605ea8f9b574bd0 to your computer and use it in GitHub Desktop.
import { Injectable } from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {Cep} from './cep';
import {map} from 'rxjs/operators';
@Injectable()
export class CepService {
constructor(private http:HttpClient) { }
buscar(cep:string){
return this.http.get<any>(`https://viacep.com.br/ws/${cep}/json/`)
.pipe(
map(response => this.converterRespostaParaCep(response.data))
);
}
private converterRespostaParaCep(cepNaResposta):Cep{
let cep = new Cep();
cep.cep = cepNaResposta.cep;
cep.logradouro = cepNaResposta.logradouro;
cep.complemento = cepNaResposta.complemento;
cep.bairro = cepNaResposta.bairro;
cep.cidade = cepNaResposta.localidade;
cep.estado = cepNaResposta.uf;
return cep;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment