codigo_postal 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/codigo_postal.rb +71 -58
- metadata +9 -14
- data/test/test_codigo_postal.rb +0 -131
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57d34d49219a1449735f55baad0fd6cb2b7a10212aed130ea6b250f378c9c5f4
|
4
|
+
data.tar.gz: b854b3f76f91b3af50af999bd52542d8a55ab6681c9f2eeadab709ff509c5258
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cdc61e8f5d27267294bf80e8dc6abb38ad87c311a28cdcff27d74bc2acc5b4a89b044a455708ea34c9cdafc779b23e38b11def809a67c43816761ad89b20db91
|
7
|
+
data.tar.gz: 6bd9ba0ca65aa52c22a75f68432e3ef1161fd0dcb5477f8e82a1ebc4bae0c0fb76ee35fb227e4b8d5252d2f1e48bc72dd460ee1b9d327591d021998d70cff195
|
data/lib/codigo_postal.rb
CHANGED
@@ -1,73 +1,86 @@
|
|
1
1
|
class CodigoPostal
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
2
|
+
SORTED_RANGES = [
|
3
|
+
{range_end: 19999999, code: "SP", name: "São Paulo"},
|
4
|
+
{range_end: 28999999, code: "RJ", name: "Rio de Janeiro"},
|
5
|
+
{range_end: 29999999, code: "ES", name: "Espírito Santo"},
|
6
|
+
{range_end: 39999999, code: "MG", name: "Minas Gerais"},
|
7
|
+
{range_end: 48999999, code: "BA", name: "Bahia"},
|
8
|
+
{range_end: 49999999, code: "SE", name: "Sergipe"},
|
9
|
+
{range_end: 56999999, code: "PE", name: "Pernambuco"},
|
10
|
+
{range_end: 57999999, code: "AL", name: "Alagoas"},
|
11
|
+
{range_end: 58999999, code: "PB", name: "Paraíba"},
|
12
|
+
{range_end: 59999999, code: "RN", name: "Rio Grande do Norte"},
|
13
|
+
{range_end: 63999999, code: "CE", name: "Ceará"},
|
14
|
+
{range_end: 64999999, code: "PI", name: "Piauí"},
|
15
|
+
{range_end: 65999999, code: "MA", name: "Maranhão"},
|
16
|
+
{range_end: 68899999, code: "PA", name: "Pará"},
|
17
|
+
{range_end: 68999999, code: "AP", name: "Amapá"},
|
18
|
+
{range_end: 69299999, code: "AM", name: "Amazonas"},
|
19
|
+
{range_end: 69399999, code: "RR", name: "Roraima"},
|
20
|
+
{range_end: 69899999, code: "AM", name: "Amazonas"},
|
21
|
+
{range_end: 69999999, code: "AC", name: "Acre"},
|
22
|
+
{range_end: 72799999, code: "DF", name: "Distrito Federal"},
|
23
|
+
{range_end: 72999999, code: "GO", name: "Goiás"},
|
24
|
+
{range_end: 73699999, code: "DF", name: "Distrito Federal"},
|
25
|
+
{range_end: 76799999, code: "GO", name: "Goiás"},
|
26
|
+
{range_end: 76999999, code: "RO", name: "Rondônia"},
|
27
|
+
{range_end: 77999999, code: "TO", name: "Tocantins"},
|
28
|
+
{range_end: 78899999, code: "MT", name: "Mato Grosso"},
|
29
|
+
{range_end: 79999999, code: "MS", name: "Mato Grosso do Sul"},
|
30
|
+
{range_end: 87999999, code: "PR", name: "Paraná"},
|
31
|
+
{range_end: 89999999, code: "SC", name: "Santa Catarina"},
|
32
|
+
{range_end: 99999999, code: "RS", name: "Rio Grande do Sul"}
|
33
33
|
]
|
34
34
|
|
35
|
-
|
35
|
+
CEP_PATTERN = /
|
36
|
+
^
|
37
|
+
\s*
|
38
|
+
(\d{1,2}) # leading zero can be ommited
|
39
|
+
\.? # incorrect, but allowed as it's somewhat common
|
40
|
+
(\d{3})
|
41
|
+
-?
|
42
|
+
(\d{3})
|
43
|
+
\s*
|
44
|
+
$
|
45
|
+
/x
|
46
|
+
|
47
|
+
attr_reader :state_code, :state_name, :formatted, :digits
|
48
|
+
|
49
|
+
# for backwards compatibility
|
50
|
+
alias_method :cep_formatted, :formatted
|
51
|
+
alias_method :cep_digits, :digits
|
52
|
+
|
36
53
|
def initialize(cep)
|
37
|
-
|
38
|
-
|
39
|
-
@cep_digits = "#{cep_fields[0]}#{cep_fields[1]}#{cep_fields[2]}".to_i
|
40
|
-
@cep_formatted = "#{cep_fields[0].rjust(2, '0')}#{cep_fields[1]}-#{cep_fields[2]}"
|
41
|
-
state = find_state_by_cep
|
42
|
-
@state_code = state[:code]
|
43
|
-
@state_name = state[:name]
|
44
|
-
end
|
45
|
-
end
|
54
|
+
components = cep.to_s.match CEP_PATTERN
|
55
|
+
return unless components
|
46
56
|
|
47
|
-
|
48
|
-
@
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
57
|
+
@digits = components.captures.join
|
58
|
+
@digits.prepend "0" if @digits.length == 7
|
59
|
+
|
60
|
+
@formatted = @digits.dup.insert 5, "-"
|
61
|
+
|
62
|
+
state = lookup_state
|
63
|
+
@state_code = state[:code]
|
64
|
+
@state_name = state[:name]
|
53
65
|
end
|
54
66
|
|
55
|
-
def
|
56
|
-
|
57
|
-
match_data = cep.match(cep_regex)
|
58
|
-
if match_data
|
59
|
-
return match_data[1..3]
|
60
|
-
else
|
61
|
-
return nil
|
62
|
-
end
|
67
|
+
def to_s
|
68
|
+
@formatted
|
63
69
|
end
|
64
70
|
|
65
71
|
def ==(other)
|
66
|
-
@
|
72
|
+
@digits == other.digits
|
67
73
|
end
|
68
74
|
|
69
|
-
def
|
70
|
-
|
75
|
+
def eql?(other)
|
76
|
+
self == other
|
71
77
|
end
|
72
78
|
|
79
|
+
private
|
80
|
+
|
81
|
+
def lookup_state
|
82
|
+
SORTED_RANGES.find do |range|
|
83
|
+
@digits.to_i <= range[:range_end]
|
84
|
+
end
|
85
|
+
end
|
73
86
|
end
|
metadata
CHANGED
@@ -1,43 +1,40 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codigo_postal
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Mesquita
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
13
|
+
name: tldr
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
16
15
|
requirements:
|
17
16
|
- - "~>"
|
18
17
|
- !ruby/object:Gem::Version
|
19
|
-
version: '
|
18
|
+
version: '1.0'
|
20
19
|
type: :development
|
21
20
|
prerelease: false
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
23
22
|
requirements:
|
24
23
|
- - "~>"
|
25
24
|
- !ruby/object:Gem::Version
|
26
|
-
version: '
|
27
|
-
description: Validate, format and get the corresponding Brazilian state for a
|
28
|
-
without making
|
25
|
+
version: '1.0'
|
26
|
+
description: Validate, format, and get the corresponding Brazilian state for a given
|
27
|
+
CEP without making network requests.
|
29
28
|
email: felipemesquita@hey.com
|
30
29
|
executables: []
|
31
30
|
extensions: []
|
32
31
|
extra_rdoc_files: []
|
33
32
|
files:
|
34
33
|
- lib/codigo_postal.rb
|
35
|
-
- test/test_codigo_postal.rb
|
36
34
|
homepage: https://github.com/felipedmesquita/codigo-postal
|
37
35
|
licenses:
|
38
36
|
- MIT
|
39
37
|
metadata: {}
|
40
|
-
post_install_message:
|
41
38
|
rdoc_options: []
|
42
39
|
require_paths:
|
43
40
|
- lib
|
@@ -52,9 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
49
|
- !ruby/object:Gem::Version
|
53
50
|
version: '0'
|
54
51
|
requirements: []
|
55
|
-
rubygems_version: 3.
|
56
|
-
signing_key:
|
52
|
+
rubygems_version: 3.6.9
|
57
53
|
specification_version: 4
|
58
54
|
summary: An offline gem to find Brazilian states by postal code
|
59
|
-
test_files:
|
60
|
-
- test/test_codigo_postal.rb
|
55
|
+
test_files: []
|
data/test/test_codigo_postal.rb
DELETED
@@ -1,131 +0,0 @@
|
|
1
|
-
puts " - -- - - - -- - >>>> Running tests from #{__FILE__}"
|
2
|
-
require 'minitest/autorun'
|
3
|
-
require 'codigo_postal'
|
4
|
-
|
5
|
-
class CodigoPostalTest < Minitest::Test
|
6
|
-
def test_find_state_by_cep
|
7
|
-
ceps = [
|
8
|
-
['01000-000', 'SP'],
|
9
|
-
['19999-999', 'SP'],
|
10
|
-
['28000-000', 'RJ'],
|
11
|
-
['28999-999', 'RJ'],
|
12
|
-
['29000-000', 'ES'],
|
13
|
-
['29999-999', 'ES'],
|
14
|
-
['30000-000', 'MG'],
|
15
|
-
['39999-999', 'MG'],
|
16
|
-
['80000-000', 'PR'],
|
17
|
-
['87999-999', 'PR'],
|
18
|
-
['88000-000', 'SC'],
|
19
|
-
['89999-999', 'SC'],
|
20
|
-
['90000-000', 'RS'],
|
21
|
-
['99999-999', 'RS'],
|
22
|
-
['40000-000', 'BA'],
|
23
|
-
['48999-999', 'BA'],
|
24
|
-
['49000-000', 'SE'],
|
25
|
-
['49999-999', 'SE'],
|
26
|
-
['50000-000', 'PE'],
|
27
|
-
['56999-999', 'PE'],
|
28
|
-
['57000-000', 'AL'],
|
29
|
-
['57999-999', 'AL'],
|
30
|
-
['58000-000', 'PB'],
|
31
|
-
['58999-999', 'PB'],
|
32
|
-
['59000-000', 'RN'],
|
33
|
-
['59999-999', 'RN'],
|
34
|
-
['60000-000', 'CE'],
|
35
|
-
['63999-999', 'CE'],
|
36
|
-
['64000-000', 'PI'],
|
37
|
-
['64999-999', 'PI'],
|
38
|
-
['65000-000', 'MA'],
|
39
|
-
['65999-999', 'MA'],
|
40
|
-
['66000-000', 'PA'],
|
41
|
-
['68899-999', 'PA'],
|
42
|
-
['68900-000', 'AP'],
|
43
|
-
['68999-999', 'AP'],
|
44
|
-
['69000-000', 'AM'],
|
45
|
-
['69299-999', 'AM'],
|
46
|
-
['69300-000', 'RR'],
|
47
|
-
['69399-999', 'RR'],
|
48
|
-
['69400-000', 'AM'],
|
49
|
-
['69899-999', 'AM'],
|
50
|
-
['69900-000', 'AC'],
|
51
|
-
['69999-999', 'AC'],
|
52
|
-
['76800-000', 'RO'],
|
53
|
-
['76999-999', 'RO']
|
54
|
-
]
|
55
|
-
|
56
|
-
ceps.each do |cep, expected_code|
|
57
|
-
assert_equal expected_code, CodigoPostal.new(cep).state_code
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_rio_pardo
|
62
|
-
rio_pardo = CodigoPostal.new('13.720-000')
|
63
|
-
assert_equal 'SP', rio_pardo.state_code
|
64
|
-
assert_equal '13720-000', rio_pardo.cep_formatted
|
65
|
-
assert_equal 13720000, rio_pardo.cep_digits
|
66
|
-
end
|
67
|
-
|
68
|
-
def test_floripa
|
69
|
-
floripa = CodigoPostal.new('88.063-500')
|
70
|
-
assert_equal 'SC', floripa.state_code
|
71
|
-
assert_equal '88063-500', floripa.cep_formatted
|
72
|
-
assert_equal 88063500, floripa.cep_digits
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_catedral
|
76
|
-
catedral = CodigoPostal.new(1001000)
|
77
|
-
assert_equal 'SP', catedral.state_code
|
78
|
-
assert_equal '01001-000', catedral.cep_formatted
|
79
|
-
assert_equal 1001000, catedral.cep_digits
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_ibirapuera
|
83
|
-
ibirapuera = CodigoPostal.new('04.094-050')
|
84
|
-
reasonable_formats = [
|
85
|
-
'04094-050',
|
86
|
-
'04094050',
|
87
|
-
'04.094-050',
|
88
|
-
'04.094050',
|
89
|
-
'4094050',
|
90
|
-
'4094-050',
|
91
|
-
'4.094050',
|
92
|
-
'4.094-050',
|
93
|
-
4094050
|
94
|
-
]
|
95
|
-
unreasonable_formats = [
|
96
|
-
'04-094.050',
|
97
|
-
'04-094050',
|
98
|
-
'04-094-050'
|
99
|
-
]
|
100
|
-
|
101
|
-
reasonable_formats.each do |format|
|
102
|
-
assert_equal ibirapuera, CodigoPostal.new(format)
|
103
|
-
end
|
104
|
-
|
105
|
-
unreasonable_formats.each do |format|
|
106
|
-
refute_equal ibirapuera, CodigoPostal.new(format)
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_readme_examples
|
112
|
-
assert_output("SP\nSão Paulo\n01001-000\n") do
|
113
|
-
cep = CodigoPostal.new(1001000)
|
114
|
-
puts cep.state_code # => 'SP'
|
115
|
-
puts cep.state_name # => 'São Paulo'
|
116
|
-
puts cep.to_s # => '01001-000'
|
117
|
-
end
|
118
|
-
|
119
|
-
assert_output("true\n") do
|
120
|
-
cep1 = CodigoPostal.new(4094050)
|
121
|
-
cep2 = CodigoPostal.new("04.094-050")
|
122
|
-
puts cep1 == cep2 # => true
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_strip_whitspace
|
127
|
-
assert_equal CodigoPostal.new(' 04.094-050 '), CodigoPostal.new('04.094-050')
|
128
|
-
end
|
129
|
-
|
130
|
-
end
|
131
|
-
|