codigo_postal 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4deb6bb397a6028c2a5fe5580657d07c472e19414e3fb28a7e1adc2d2e91e84e
4
+ data.tar.gz: dcfd84d88b2eab52daa469b1e1a2cc2d19fa43144e65518b60cd0283ac8199e1
5
+ SHA512:
6
+ metadata.gz: 99c6a2741a4c0a702dc791f8c2835af1349aafbc44644c3cd09b44afcbb8dd7c59a203f5c55a8f7ea2ba2d77e8ffb12ecb51110f981354418646ccf02c6bcf38
7
+ data.tar.gz: ac1c6ada139b2f5dae63fe657267c0abe7e8ac940f995ae2a74a743f9d3b385b1694926d119de5a21e824627a5cd43676460206be85d5e984895fc715fee16fc
@@ -0,0 +1,73 @@
1
+ class CodigoPostal
2
+ CEP_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: 87999999, code: 'PR', name: 'Paraná' },
8
+ { range_end: 89999999, code: 'SC', name: 'Santa Catarina' },
9
+ { range_end: 99999999, code: 'RS', name: 'Rio Grande do Sul' },
10
+ { range_end: 48999999, code: 'BA', name: 'Bahia' },
11
+ { range_end: 49999999, code: 'SE', name: 'Sergipe' },
12
+ { range_end: 56999999, code: 'PE', name: 'Pernambuco' },
13
+ { range_end: 57999999, code: 'AL', name: 'Alagoas' },
14
+ { range_end: 58999999, code: 'PB', name: 'Paraíba' },
15
+ { range_end: 59999999, code: 'RN', name: 'Rio Grande do Norte' },
16
+ { range_end: 63999999, code: 'CE', name: 'Ceará' },
17
+ { range_end: 64999999, code: 'PI', name: 'Piauí' },
18
+ { range_end: 65999999, code: 'MA', name: 'Maranhão' },
19
+ { range_end: 68899999, code: 'PA', name: 'Pará' },
20
+ { range_end: 68999999, code: 'AP', name: 'Amapá' },
21
+ { range_end: 69299999, code: 'AM', name: 'Amazonas' },
22
+ { range_end: 69399999, code: 'RR', name: 'Roraima' },
23
+ { range_end: 69899999, code: 'AM', name: 'Amazonas' },
24
+ { range_end: 69999999, code: 'AC', name: 'Acre' },
25
+ { range_end: 76999999, code: 'RO', name: 'Rondônia' },
26
+ { range_end: 77999999, code: 'TO', name: 'Tocantins' },
27
+ { range_end: 72799999, code: 'DF', name: 'Distrito Federal' },
28
+ { range_end: 72999999, code: 'GO', name: 'Goiás' },
29
+ { range_end: 73699999, code: 'DF', name: 'Distrito Federal' },
30
+ { range_end: 76799999, code: 'GO', name: 'Goiás' },
31
+ { range_end: 78899999, code: 'MT', name: 'Mato Grosso' },
32
+ { range_end: 79999999, code: 'MS', name: 'Mato Grosso do Sul' }
33
+ ]
34
+
35
+ attr_reader :state_code, :state_name, :cep_formatted, :cep_digits
36
+ def initialize(cep)
37
+ cep_fields = match_cep cep.to_s
38
+ if !cep_fields.nil?
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
46
+
47
+ def find_state_by_cep
48
+ cep_number = @cep_digits
49
+ CEP_RANGES.sort_by { |range| range[:range_end] }.each do |cep_range|
50
+ return cep_range if cep_number <= cep_range[:range_end]
51
+ end
52
+ nil
53
+ end
54
+
55
+ def match_cep(cep)
56
+ cep_regex = /^(\d{1,2})\.?(\d{3})\-?(\d{3})$/
57
+ match_data = cep.match(cep_regex)
58
+ if match_data
59
+ return match_data[1..3]
60
+ else
61
+ return nil
62
+ end
63
+ end
64
+
65
+ def ==(other)
66
+ @cep_digits == other.cep_digits
67
+ end
68
+
69
+ def to_s
70
+ @cep_formatted
71
+ end
72
+
73
+ end
@@ -0,0 +1,112 @@
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
+ end
112
+
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codigo_postal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Felipe Mesquita
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-05-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ description: This gem provides a simple way to find Brazilian states by postal code.
28
+ email: felipemesquita@hey.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/codigo_postal.rb
34
+ - test/test_codigo_postal.rb
35
+ homepage: https://github.com/felipedmesquita/codigo-postal
36
+ licenses:
37
+ - MIT
38
+ metadata: {}
39
+ post_install_message:
40
+ rdoc_options: []
41
+ require_paths:
42
+ - lib
43
+ required_ruby_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubygems_version: 3.4.12
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: A gem to find Brazilian states by postal code
58
+ test_files:
59
+ - test/test_codigo_postal.rb