lib/cep.rb in cep-0.0.1 vs lib/cep.rb in cep-0.0.2

- old
+ new

@@ -4,23 +4,22 @@ class Cep URL = 'http://m.correios.com.br/movel/buscaCepConfirma.do' + def initialize + @ceps = [] + @_cep = {} + end + def address(cep) response = query({cepEntrada: cep, metodo: :buscarCep}) values = values(response) + end - city_and_state = values[2].split('/').map{|v| v.strip} - - { - logradouro: values[0], - bairro: values[1], - localidade: city_and_state[0], - estado: city_and_state[1], - cep: values[3] - } + def cep(cep) + address(cep)[0] end def url(params = {}) uri = Addressable::URI.new uri.query_values = params @@ -31,12 +30,76 @@ uri = URI.parse(url(params)) Net::HTTP.get_response(uri) end def values(response) - Nokogiri::HTML(response.body).css('.respostadestaque').map do |el| - el.content.gsub(/[\n\t]/,'').strip.gsub(/\s+/,' ') + Nokogiri::HTML(response.body).css('.respostadestaque').each do |el| + el.parent.children.children.each_slice(2).each do |k,v| + elements = clean_element(k, v) + handle_keys(elements) + end end + @ceps.uniq end + private + def clean_element(key, value) + key = resolve_text(key.text).gsub(/:/,'').downcase unless key.nil? + value = resolve_text(value.text) unless value.nil? + [key, value] + end + + def resolve_text(text) + text.gsub(/[\n\t]/,'').gsub(/\s+/,' ').strip + end + + def handle_keys(elements) + key = elements[0] + value = elements[1] + unless key == 'opções' and value == nil + add_cep_in_ceps(key) + resolve_adrress(key, value) + resolve_locality(key, value) + resolve_others(key, value) + end + end + + def add_cep_in_ceps(key) + if key_name_present?(key, ['logradouro','endereço']) + @ceps << @_cep unless @_cep.empty? + @_cep = {} + end + @ceps + end + + def resolve_adrress(key, value) + if key_name_present?(key, ['logradouro','endereço']) + @_cep[:tipo_logradouro] = value.split(' ').map{|v| v.strip}[0] + @_cep[:logradouro] = value + end + @_cep + end + + def resolve_locality(key, value) + if key_name_present?(key, ['localidade / uf']) + @_cep[locality(key,0).to_sym] = locality(value,0) + @_cep[locality(key,1).to_sym] = locality(value,1) + end + @_cep + end + + def resolve_others(key, value) + unless key_name_present?(key, ['logradouro','endereço','localidade / uf']) + @_cep[key.to_sym] = value + end + @_cep + end + + def locality(el, pos) + el.split('/').map{|v| v.strip}[pos] + end + + def key_name_present?(key, names=[]) + names.include?(key) + end end \ No newline at end of file