Forked from augcesar/Estados e Cidades - Código Município - Ruby On Rails - ROR
Created
January 29, 2024 19:39
-
-
Save tteurs/27d59de918cb0a84d7ea06bf3272aafb to your computer and use it in GitHub Desktop.
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
estados = JSON.parse open('https://gist.githubusercontent.com/letanure/3012978/raw/36fc21d9e2fc45c078e0e0e07cce3c81965db8f9/estados-cidades.json').read | |
estados['estados'].select{|x| x['sigla'] != 'CE' }.each do |aux| | |
estado = Estado.find_or_create_by(sigla: aux['sigla'], descricao: aux['nome'].mb_chars.upcase.to_s | |
) | |
aux['cidades'].each do |cidade| | |
Municipio.find_or_create_by(estado:estado, descricao:cidade.mb_chars.upcase.to_s | |
) | |
end | |
end | |
estados = JSON.parse open('https://raw.githubusercontent.com/clubedaentrega/municipios-ibge/master/dados.json').read | |
estados.select{|x,y| x != 'CE' }.each do |aux| | |
estado = Estado.find_by!(sigla: aux.first) | |
# Cidades | |
aux.second.each do |cidade, codigo| | |
cidade = I18n.transliterate(cidade.mb_chars.upcase.to_s).gsub(' ', '') | |
Municipio.where(estado: estado).where("replace(descricao, ' ', '') = '#{cidade}'")&.take&.update(codigo: codigo) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment