diff --git a/lib/correios/cep/parser.rb b/lib/correios/cep/parser.rb index a878473..00a2b0a 100644 --- a/lib/correios/cep/parser.rb +++ b/lib/correios/cep/parser.rb @@ -1,5 +1,3 @@ -require 'ox' - module Correios module CEP class Parser @@ -13,6 +11,18 @@ class Parser 'complemento2' => :complement2 }.freeze + ADDRESS_NOT_FOUND_ERROR = 'CEP NAO ENCONTRADO' + + def address(xml) + address = Address.parse(xml) + return address if address.has_content? + + error_message = extract_error_message(xml) + return nil if error_message == ADDRESS_NOT_FOUND_ERROR + + raise error_message || 'Unknown error' + end + def hash(xml) doc = Ox.parse(xml) @@ -30,6 +40,10 @@ def hash(xml) private + def extract_error_message(xml) + return $1 if xml =~ /(.*)<\/faultstring>/ + end + def find_node(nodes, name) node = nodes.last return nil unless node.is_a?(Ox::Element) diff --git a/spec/correios/cep/parser_spec.rb b/spec/correios/cep/parser_spec.rb index 658708e..0209a26 100644 --- a/spec/correios/cep/parser_spec.rb +++ b/spec/correios/cep/parser_spec.rb @@ -2,6 +2,47 @@ require 'spec_helper' describe Correios::CEP::Parser do + describe '#address' do + let(:expected_address) do + address = Correios::CEP::Address.new + address.street_address = 'Rua Fernando Amorim' + address.neighborhood = 'Cavaleiro' + address.city = 'Jaboatão dos Guararapes' + address.state = 'PE' + address.zipcode = '54250610' + address + end + + context 'when address is found' do + let(:xml) { Fixture.load(:address) } + + it 'returns address' do + expect(subject.address(xml)).to eq expected_address + end + end + + context 'when address is not found' do + let(:xml) { Fixture.load(:address_not_found) } + + it 'returns nil' do + expect(subject.address(xml)).to eq nil + end + end + + context 'when there is an unexpected error' do + { invalid_zipcode: 'BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS', + required_zipcode: 'CEP NAO INFORMADO', + whatever_error: 'QUALQUER OUTRO ERRO', + empty_response: 'Unknown error' + }.each do |name, message| + it 'raises RuntimeError exception' do + xml = Fixture.load(name) + expect { subject.address(xml) }.to raise_error(RuntimeError, message) + end + end + end + end + describe '#hash' do let(:expected_address) do { diff --git a/spec/fixtures/empty_response.xml b/spec/fixtures/empty_response.xml new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/invalid_zipcode.xml b/spec/fixtures/invalid_zipcode.xml new file mode 100644 index 0000000..e4867af --- /dev/null +++ b/spec/fixtures/invalid_zipcode.xml @@ -0,0 +1,11 @@ + + + + soap:Server + BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS + + BUSCA DEFINIDA COMO EXATA, 0 CEP DEVE TER 8 DIGITOS + + + + diff --git a/spec/fixtures/required_zipcode.xml b/spec/fixtures/required_zipcode.xml new file mode 100644 index 0000000..6373008 --- /dev/null +++ b/spec/fixtures/required_zipcode.xml @@ -0,0 +1,11 @@ + + + + soap:Server + CEP NAO INFORMADO + + CEP NAO INFORMADO + + + + diff --git a/spec/fixtures/whatever_error.xml b/spec/fixtures/whatever_error.xml new file mode 100644 index 0000000..287990a --- /dev/null +++ b/spec/fixtures/whatever_error.xml @@ -0,0 +1,11 @@ + + + + soap:Server + QUALQUER OUTRO ERRO + + QUALQUER OUTRO ERRO + + + +