correios-cep 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b6164e71254f2ef59c989f89d5defcaef1413ef
4
- data.tar.gz: a9110b7b615e02f922d341289823a6a99e5331da
3
+ metadata.gz: b4205b263bcf608493677f1f0f6ce7a9cbf4fea6
4
+ data.tar.gz: b39d686fef4ff806416993478d88885a05fe694a
5
5
  SHA512:
6
- metadata.gz: 2819ca06ab734c9b34c0fc9c9f9fd2a7016e5229c04e035a1079f9bf483993ff67c6e9a987c7e300b707f6a0b67ff746259178d9e7e47711b9509760fd3577c0
7
- data.tar.gz: 8dc08cbc5b87127c7159415640a1548b47a78253f99a0c457e81188545cc31bfb56b74a13ed237b975a24e9a54e398acb60ccdf0241658bb007006fb2dab0217
6
+ metadata.gz: 102b1121be45c66e6963b59ab0490cefd5a7431c8509f98f9285cfc34cee7db68b49dee7efa15c97dfa4a855dc5919da27c9b4be21f00bc6dccc59f382f18f46
7
+ data.tar.gz: c9ea444975185a81518c5a71b5fcfd787d6c6c53d69c13645482e45c34aa2c4eb036d490d9ae940fef9996ff960b26af4276b5439ff34e6024c98de0cbe65812
@@ -1,3 +1,6 @@
1
+ ## Version 0.4.0
2
+ - Improvement: Replace Nokogiri by Ox. Issue #3.
3
+
1
4
  ## Version 0.3.3
2
5
  - Fix: Downgrade LogMe gem version to 0.0.8, because 0.0.9 does not support Ruby 1.9.2.
3
6
 
@@ -21,12 +21,12 @@ Gem::Specification.new do |spec|
21
21
  spec.required_ruby_version = Gem::Requirement.new('>= 1.9.2')
22
22
 
23
23
  spec.add_dependency 'log-me', '= 0.0.8'
24
- spec.add_dependency 'nokogiri', '~> 1.6'
24
+ spec.add_dependency "ox", "~> 2.2"
25
25
 
26
- spec.add_development_dependency 'coveralls'
27
- spec.add_development_dependency 'pry'
28
- spec.add_development_dependency 'rake'
29
- spec.add_development_dependency 'rspec', '~> 3.2'
30
- spec.add_development_dependency 'vcr', '~> 2.9'
31
- spec.add_development_dependency 'webmock', '~> 1.21'
26
+ spec.add_development_dependency "coveralls"
27
+ spec.add_development_dependency "pry"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec", "~> 3.2"
30
+ spec.add_development_dependency "vcr", "~> 2.9"
31
+ spec.add_development_dependency "webmock", "~> 1.15"
32
32
  end
@@ -1,4 +1,4 @@
1
- require 'nokogiri'
1
+ require 'ox'
2
2
 
3
3
  module Correios
4
4
  module CEP
@@ -14,21 +14,33 @@ module Correios
14
14
  }.freeze
15
15
 
16
16
  def address(xml)
17
- result = Nokogiri::XML(xml).xpath('//return')
18
- return if result.nil? || result.empty?
17
+ doc = Ox.parse(xml)
18
+ return_node = find_node(doc.nodes, 'return')
19
+ return if return_node.nil?
19
20
 
20
21
  address = {}
21
- result.children.each do |element|
22
- address[ADDRESS_MAP[element.name]] = element.text if ADDRESS_MAP[element.name]
22
+ return_node.nodes.each do |element|
23
+ address[ADDRESS_MAP[element.name]] = text_for(element) if ADDRESS_MAP[element.name]
23
24
  end
24
25
 
25
- join_complements! address
26
+ join_complements(address)
26
27
  address
27
28
  end
28
29
 
29
30
  private
30
31
 
31
- def join_complements!(address)
32
+ def find_node(nodes, name)
33
+ node = nodes.first
34
+ return node if node.nil? || node.name == name
35
+
36
+ find_node(node.nodes, name)
37
+ end
38
+
39
+ def text_for(element)
40
+ element.text.to_s.force_encoding(Encoding::UTF_8)
41
+ end
42
+
43
+ def join_complements(address)
32
44
  address[:complement] += " #{address.delete(:complement2)}"
33
45
  address[:complement].strip!
34
46
  end
@@ -1,5 +1,5 @@
1
1
  module Correios
2
2
  module CEP
3
- VERSION = '0.3.3'
3
+ VERSION = '0.4.0'
4
4
  end
5
5
  end
@@ -3,39 +3,98 @@ require 'spec_helper'
3
3
 
4
4
  describe Correios::CEP::Parser do
5
5
  describe '#address' do
6
+ let(:expected_address) do
7
+ {
8
+ address: 'Rua Fernando Amorim',
9
+ neighborhood: 'Cavaleiro',
10
+ city: 'Jaboatão dos Guararapes',
11
+ state: 'PE',
12
+ zipcode: '54250610',
13
+ complement: ''
14
+ }
15
+ end
16
+
6
17
  context 'when address is found' do
7
- let(:xml) do
8
- "<?xml version='1.0' encoding='UTF-8'?>" +
9
- "<S:Envelope>" +
10
- "<S:Body>" +
11
- "<ns2:consultaCEPResponse xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">" +
12
- "<return>" +
13
- "<bairro>Cavaleiro</bairro>" +
14
- "<cep>54250610</cep>" +
15
- "<cidade>Jaboatão dos Guararapes</cidade>" +
16
- "<complemento>de 1500 até o fim</complemento>" +
17
- "<complemento2>(zona mista)</complemento2>" +
18
- "<end>Rua Fernando Amorim</end>" +
19
- "<id>0</id>" +
20
- "<uf>PE</uf>" +
21
- "</return>" +
22
- "</ns2:consultaCEPResponse>" +
23
- "</S:Body>" +
24
- "</S:Envelope>"
18
+ context 'and does not have complement' do
19
+ let(:xml) do
20
+ "<?xml version='1.0' encoding='UTF-8'?>" +
21
+ "<S:Envelope>" +
22
+ "<S:Body>" +
23
+ "<ns2:consultaCEPResponse xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">" +
24
+ "<return>" +
25
+ "<bairro>Cavaleiro</bairro>" +
26
+ "<cep>54250610</cep>" +
27
+ "<cidade>Jaboatão dos Guararapes</cidade>" +
28
+ "<complemento></complemento>" +
29
+ "<complemento2></complemento2>" +
30
+ "<end>Rua Fernando Amorim</end>" +
31
+ "<id>0</id>" +
32
+ "<uf>PE</uf>" +
33
+ "</return>" +
34
+ "</ns2:consultaCEPResponse>" +
35
+ "</S:Body>" +
36
+ "</S:Envelope>"
37
+ end
38
+
39
+ it 'returns address' do
40
+ expect(subject.address(xml)).to eq expected_address
41
+ end
25
42
  end
26
- let(:expected_address) do
27
- {
28
- address: 'Rua Fernando Amorim',
29
- neighborhood: 'Cavaleiro',
30
- city: 'Jaboatão dos Guararapes',
31
- state: 'PE',
32
- zipcode: '54250610',
33
- complement: 'de 1500 até o fim (zona mista)'
34
- }
43
+
44
+ context 'and has one complement' do
45
+ let(:xml) do
46
+ "<?xml version='1.0' encoding='UTF-8'?>" +
47
+ "<S:Envelope>" +
48
+ "<S:Body>" +
49
+ "<ns2:consultaCEPResponse xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">" +
50
+ "<return>" +
51
+ "<bairro>Cavaleiro</bairro>" +
52
+ "<cep>54250610</cep>" +
53
+ "<cidade>Jaboatão dos Guararapes</cidade>" +
54
+ "<complemento>de 1500 até o fim</complemento>" +
55
+ "<complemento2></complemento2>" +
56
+ "<end>Rua Fernando Amorim</end>" +
57
+ "<id>0</id>" +
58
+ "<uf>PE</uf>" +
59
+ "</return>" +
60
+ "</ns2:consultaCEPResponse>" +
61
+ "</S:Body>" +
62
+ "</S:Envelope>"
63
+ end
64
+
65
+ it 'returns address' do
66
+ expected_address[:complement] = 'de 1500 até o fim'
67
+
68
+ expect(subject.address(xml)).to eq expected_address
69
+ end
35
70
  end
36
71
 
37
- it 'returns address' do
38
- expect(subject.address(xml)).to eq expected_address
72
+ context 'and has two complements' do
73
+ let(:xml) do
74
+ "<?xml version='1.0' encoding='UTF-8'?>" +
75
+ "<S:Envelope>" +
76
+ "<S:Body>" +
77
+ "<ns2:consultaCEPResponse xmlns:ns2=\"http://cliente.bean.master.sigep.bsb.correios.com.br/\">" +
78
+ "<return>" +
79
+ "<bairro>Cavaleiro</bairro>" +
80
+ "<cep>54250610</cep>" +
81
+ "<cidade>Jaboatão dos Guararapes</cidade>" +
82
+ "<complemento>de 1500 até o fim</complemento>" +
83
+ "<complemento2>(zona mista)</complemento2>" +
84
+ "<end>Rua Fernando Amorim</end>" +
85
+ "<id>0</id>" +
86
+ "<uf>PE</uf>" +
87
+ "</return>" +
88
+ "</ns2:consultaCEPResponse>" +
89
+ "</S:Body>" +
90
+ "</S:Envelope>"
91
+ end
92
+
93
+ it 'returns address' do
94
+ expected_address[:complement] = 'de 1500 até o fim (zona mista)'
95
+
96
+ expect(subject.address(xml)).to eq expected_address
97
+ end
39
98
  end
40
99
  end
41
100
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: correios-cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Prodis a.k.a. Fernando Hamasaki de Amorim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-24 00:00:00.000000000 Z
11
+ date: 2015-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: log-me
@@ -25,19 +25,19 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.0.8
27
27
  - !ruby/object:Gem::Dependency
28
- name: nokogiri
28
+ name: ox
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.6'
33
+ version: '2.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.6'
40
+ version: '2.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: coveralls
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -114,14 +114,14 @@ dependencies:
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '1.21'
117
+ version: '1.15'
118
118
  type: :development
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
122
  - - "~>"
123
123
  - !ruby/object:Gem::Version
124
- version: '1.21'
124
+ version: '1.15'
125
125
  description: Correios CEP gem gets updated Brazilian address from a zipcode, directly
126
126
  from Correios database. No HTML parsers.
127
127
  email: