brazil-cep 0.1.0 → 0.3.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
  SHA256:
3
- metadata.gz: ebcf36ea20edd02418b626e0741972620cea2b3a4af54dbaffb0413c4af1bb1e
4
- data.tar.gz: 4fa2527e8007a0f0d7dc5a13a72f54a54508be5aab0887dfb21582b7c395ccdf
3
+ metadata.gz: 0a4a5033b0b8e49dbd7d48f464c76e857a30140c6e29be85ca3440ed45bec589
4
+ data.tar.gz: ea457dfc8a836178131f231002907c5f6ae165d0f9124bbb7faa711a7e88784c
5
5
  SHA512:
6
- metadata.gz: 3aa434fd7b4ab0149bf7a70167c2302b3eb78782d5df6077e18763b3f24e5eb5d70e151384cced2b46d1676d54f90a4a14b2ba710ef4f537842675df8ad9ea52
7
- data.tar.gz: 2048b051f9853cc1fce960257e49c13d30f11a20d10414c914ce0f4d068fe96e9ea240e72c3eec76597097d2873e5c0a8f84b84301c5e90199a9d106b83f445b
6
+ metadata.gz: d632a23cd2e78c93392d3c405a9d9023efc4d639e69ddf3c7bc334b0ebaa48bf42a968067c18fea8e2cae8e7541b17ea56f6265ddfccf1c9bbe829e298b462ca
7
+ data.tar.gz: 01e72027e7660a8b6f67a1730df08824bbfc72bcbed978d2c7ee2a87fd13901b62f7130ec0576d5b00fe0aeb3101afdb2196abec7d6df1f610288989bcff9f34
data/.rubocop.yml CHANGED
@@ -11,3 +11,156 @@ Style/StringLiteralsInInterpolation:
11
11
 
12
12
  Layout/LineLength:
13
13
  Max: 120
14
+
15
+ AllCops:
16
+ NewCops: enable
17
+ Gemspec/DeprecatedAttributeAssignment: # new in 1.30
18
+ Enabled: true
19
+ Gemspec/DevelopmentDependencies: # new in 1.44
20
+ Enabled: true
21
+ Gemspec/RequireMFA: # new in 1.23
22
+ Enabled: true
23
+ Layout/LineContinuationLeadingSpace: # new in 1.31
24
+ Enabled: true
25
+ Layout/LineContinuationSpacing: # new in 1.31
26
+ Enabled: true
27
+ Layout/LineEndStringConcatenationIndentation: # new in 1.18
28
+ Enabled: true
29
+ Layout/SpaceBeforeBrackets: # new in 1.7
30
+ Enabled: true
31
+ Lint/AmbiguousAssignment: # new in 1.7
32
+ Enabled: true
33
+ Lint/AmbiguousOperatorPrecedence: # new in 1.21
34
+ Enabled: true
35
+ Lint/AmbiguousRange: # new in 1.19
36
+ Enabled: true
37
+ Lint/ConstantOverwrittenInRescue: # new in 1.31
38
+ Enabled: true
39
+ Lint/DeprecatedConstants: # new in 1.8
40
+ Enabled: true
41
+ Lint/DuplicateBranch: # new in 1.3
42
+ Enabled: true
43
+ Lint/DuplicateMagicComment: # new in 1.37
44
+ Enabled: true
45
+ Lint/DuplicateMatchPattern: # new in 1.50
46
+ Enabled: true
47
+ Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
48
+ Enabled: true
49
+ Lint/EmptyBlock: # new in 1.1
50
+ Enabled: true
51
+ Lint/EmptyClass: # new in 1.3
52
+ Enabled: true
53
+ Lint/EmptyInPattern: # new in 1.16
54
+ Enabled: true
55
+ Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
56
+ Enabled: true
57
+ Lint/ItWithoutArgumentsInBlock: # new in 1.59
58
+ Enabled: true
59
+ Lint/LambdaWithoutLiteralBlock: # new in 1.8
60
+ Enabled: true
61
+ Lint/LiteralAssignmentInCondition: # new in 1.58
62
+ Enabled: true
63
+ Lint/MixedCaseRange: # new in 1.53
64
+ Enabled: true
65
+ Lint/NoReturnInBeginEndBlocks: # new in 1.2
66
+ Enabled: true
67
+ Lint/NonAtomicFileOperation: # new in 1.31
68
+ Enabled: true
69
+ Lint/NumberedParameterAssignment: # new in 1.9
70
+ Enabled: true
71
+ Lint/OrAssignmentToConstant: # new in 1.9
72
+ Enabled: true
73
+ Lint/RedundantDirGlobSort: # new in 1.8
74
+ Enabled: true
75
+ Lint/RedundantRegexpQuantifiers: # new in 1.53
76
+ Enabled: true
77
+ Lint/RefinementImportMethods: # new in 1.27
78
+ Enabled: true
79
+ Lint/RequireRangeParentheses: # new in 1.32
80
+ Enabled: true
81
+ Lint/RequireRelativeSelfPath: # new in 1.22
82
+ Enabled: true
83
+ Lint/SymbolConversion: # new in 1.9
84
+ Enabled: true
85
+ Lint/ToEnumArguments: # new in 1.1
86
+ Enabled: true
87
+ Lint/TripleQuotes: # new in 1.9
88
+ Enabled: true
89
+ Lint/UnexpectedBlockArity: # new in 1.5
90
+ Enabled: true
91
+ Lint/UnmodifiedReduceAccumulator: # new in 1.1
92
+ Enabled: true
93
+ Lint/UselessRescue: # new in 1.43
94
+ Enabled: true
95
+ Lint/UselessRuby2Keywords: # new in 1.23
96
+ Enabled: true
97
+ Metrics/CollectionLiteralLength: # new in 1.47
98
+ Enabled: true
99
+ Naming/BlockForwarding: # new in 1.24
100
+ Enabled: true
101
+ Security/CompoundHash: # new in 1.28
102
+ Enabled: true
103
+ Security/IoMethods: # new in 1.22
104
+ Enabled: true
105
+ Style/ArgumentsForwarding: # new in 1.1
106
+ Enabled: true
107
+ Style/ArrayIntersect: # new in 1.40
108
+ Enabled: true
109
+ Style/CollectionCompact: # new in 1.2
110
+ Enabled: true
111
+ Style/ComparableClamp: # new in 1.44
112
+ Enabled: true
113
+ Style/ConcatArrayLiterals: # new in 1.41
114
+ Enabled: true
115
+ Style/DataInheritance: # new in 1.49
116
+ Enabled: true
117
+ Style/DirEmpty: # new in 1.48
118
+ Enabled: true
119
+ Style/DocumentDynamicEvalDefinition: # new in 1.1
120
+ Enabled: true
121
+ Style/EmptyHeredoc: # new in 1.32
122
+ Enabled: true
123
+ Style/EndlessMethod: # new in 1.8
124
+ Enabled: true
125
+ Style/EnvHome: # new in 1.29
126
+ Enabled: true
127
+ Style/ExactRegexpMatch: # new in 1.51
128
+ Enabled: true
129
+ Style/FetchEnvVar: # new in 1.28
130
+ Enabled: true
131
+ Style/FileEmpty: # new in 1.48
132
+ Enabled: true
133
+ Style/FileRead: # new in 1.24
134
+ Enabled: true
135
+ Style/RedundantEach: # new in 1.38
136
+ Enabled: true
137
+ Style/RedundantFilterChain: # new in 1.52
138
+ Enabled: true
139
+ Style/RedundantHeredocDelimiterQuotes: # new in 1.45
140
+ Enabled: true
141
+ Style/RedundantInitialize: # new in 1.27
142
+ Enabled: true
143
+ Style/RedundantLineContinuation: # new in 1.49
144
+ Enabled: true
145
+ Style/RedundantRegexpArgument: # new in 1.53
146
+ Enabled: true
147
+ Style/RedundantRegexpConstructor: # new in 1.52
148
+ Enabled: true
149
+ Style/RedundantSelfAssignmentBranch: # new in 1.19
150
+ Enabled: true
151
+ Style/RedundantStringEscape: # new in 1.37
152
+ Enabled: true
153
+ Style/ReturnNilInPredicateMethodDefinition: # new in 1.53
154
+ Enabled: true
155
+ Style/SelectByRegexp: # new in 1.22
156
+ Enabled: true
157
+ Style/SingleLineDoEndBlock: # new in 1.57
158
+ Enabled: true
159
+ Style/StringChars: # new in 1.12
160
+ Enabled: true
161
+ Style/SuperWithArgsParentheses: # new in 1.58
162
+ Enabled: true
163
+ Style/SwapValues: # new in 1.1
164
+ Enabled: true
165
+ Style/YAMLFileRead: # new in 1.53
166
+ Enabled: true
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  ## [Unreleased]
2
2
 
3
+
4
+ ## 0.2.0 - 2024-03-26
5
+
6
+ ### Added
7
+ - Add methods to register new provider adapter
8
+ - Change cep word by zipcode to keep the same terminology
9
+ - Add simplecov to project CI build
10
+ - Add more ruby versions to CI build
11
+
12
+ ### Fixed
13
+ - Remove wrong markdown code block tag from README
14
+
15
+ ### Removed
16
+ - Remove rubocop check from rake default task
17
+
18
+ ### Security
19
+ - Enable rubygems mfa requirement at gemspec file
20
+
21
+
3
22
  ## [0.1.0] - 2024-03-26
4
23
 
5
24
  - Initial release
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Brazil::Cep
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/brazil-cep.svg)](https://badge.fury.io/rb/brazil-cep)
4
+ [![Ruby](https://github.com/dvinciguerra/brazil-cep/actions/workflows/main.yml/badge.svg)](https://github.com/dvinciguerra/brazil-cep/actions/workflows/main.yml)
5
+ [![Maintainability](https://api.codeclimate.com/v1/badges/ebe40dff21c15748fd1c/maintainability)](https://codeclimate.com/github/dvinciguerra/brazil-cep/maintainability)
6
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/ebe40dff21c15748fd1c/test_coverage)](https://codeclimate.com/github/dvinciguerra/brazil-cep/test_coverage)
7
+
3
8
  This gem provides a simple way to validate and format Brazilian postal codes (CEP).
4
9
 
5
10
  ## Installation
@@ -62,7 +67,6 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/dvinci
62
67
  ## Code of Conduct
63
68
 
64
69
  Everyone interacting in the Brazil::Cep project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/dvinciguerra/brazil-cep/blob/master/CODE_OF_CONDUCT.md).
65
- ```
66
70
 
67
71
  ## License
68
72
 
data/Rakefile CHANGED
@@ -5,8 +5,8 @@ require "rspec/core/rake_task"
5
5
 
6
6
  RSpec::Core::RakeTask.new(:spec)
7
7
 
8
- require "rubocop/rake_task"
8
+ # require "rubocop/rake_task"
9
+ # RuboCop::RakeTask.new
9
10
 
10
- RuboCop::RakeTask.new
11
-
12
- task default: %i[spec rubocop]
11
+ # task default: %i[spec rubocop]
12
+ task default: %i[spec]
@@ -31,7 +31,8 @@ module Brazil
31
31
  @cep = value unless value.nil?
32
32
 
33
33
  http_request(provider_url) do |http_response|
34
- raise Brazil::Cep::RequestError, http_response unless http_response.is_a?(Net::HTTPSuccess)
34
+ raise Brazil::Cep::ZipcodeNotFound, http_response if http_response.is_a?(Net::HTTPNotFound)
35
+ raise Brazil::Cep::RequestError, http_response if http_response.code.to_i >= 400
35
36
  end
36
37
 
37
38
  deserialization!
@@ -12,7 +12,7 @@ module Brazil
12
12
 
13
13
  def transformation!
14
14
  address_params = {
15
- cep: @payload[:cep],
15
+ zipcode: @payload[:cep],
16
16
  state: @payload[:estado],
17
17
  city: @payload[:cidade],
18
18
  neighborhood: @payload[:bairro],
@@ -12,7 +12,7 @@ module Brazil
12
12
 
13
13
  def transformation!
14
14
  address_params = {
15
- cep: @cep,
15
+ zipcode: @cep,
16
16
  state: @payload[:uf],
17
17
  city: @payload[:cidade],
18
18
  neighborhood: @payload[:bairro],
@@ -11,14 +11,16 @@ module Brazil
11
11
  private
12
12
 
13
13
  def transformation!
14
+ raise Brazil::Cep::ZipcodeNotFound, @response if @payload[:erro] == true
15
+
14
16
  address_params = {
15
- cep: @payload[:cep],
17
+ zipcode: @payload[:cep],
16
18
  state: @payload[:uf],
17
19
  city: @payload[:localidade],
18
20
  neighborhood: @payload[:bairro],
19
21
  street: @payload[:logradouro],
20
22
  complement: @payload[:complemento],
21
- meta: @payload.update(provider: :postmon)
23
+ meta: @payload.update(provider: :viacep)
22
24
  }
23
25
 
24
26
  Brazil::Cep::Address.new(**address_params)
@@ -4,6 +4,7 @@ module Brazil
4
4
  module Cep
5
5
  # Adapters class namespace
6
6
  module Adapters
7
+ # autoload default adapters
7
8
  autoload :Base, "brazil_cep/adapters/base"
8
9
  autoload :Viacep, "brazil_cep/adapters/viacep"
9
10
  autoload :Postmon, "brazil_cep/adapters/postmon"
@@ -14,8 +15,30 @@ module Brazil
14
15
  viacep: Viacep,
15
16
  postmon: Postmon,
16
17
  republica_virtual: RepublicaVirtual
17
- }.freeze
18
+ }
18
19
 
20
+ private_constant :PROVIDERS
21
+
22
+ # get providers
23
+ # @return [Hash] providers
24
+ def self.providers
25
+ PROVIDERS
26
+ end
27
+
28
+ # register a provider
29
+ #
30
+ # @example
31
+ # Brazil::Cep::Adapters.register(:custom_provider, Brazil::Cep::Adapters::CustomProvider)
32
+ #
33
+ # @param provider_key [Symbol] provider key
34
+ # @param provider_class [Class] provider class
35
+ def self.register(provider_key, provider_class)
36
+ PROVIDERS[provider_key] = provider_class
37
+ end
38
+
39
+ # get provider instance
40
+ # @param provider [Symbol] provider key
41
+ # @return [Base] provider instance
19
42
  def self.get(provider = :viacep)
20
43
  PROVIDERS[provider].new
21
44
  end
@@ -4,10 +4,10 @@ module Brazil
4
4
  module Cep
5
5
  # Address class
6
6
  class Address
7
- attr_reader :cep, :state, :city, :neighborhood, :street, :complement, :meta
7
+ attr_reader :zipcode, :state, :city, :neighborhood, :street, :complement, :meta
8
8
 
9
9
  def initialize(**arguments)
10
- @cep = arguments[:cep]
10
+ @zipcode = arguments[:zipcode]
11
11
  @state = arguments[:state]
12
12
  @city = arguments[:city]
13
13
  @neighborhood = arguments[:neighborhood]
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Brazil
4
4
  module Cep
5
- VERSION = "0.1.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
data/lib/brazil_cep.rb CHANGED
@@ -8,8 +8,24 @@ module Brazil
8
8
  # Brazil::Cep providers a simple way to fetch address information from a Brazilian CEP
9
9
  # @public
10
10
  module Cep
11
+ # generic error class
11
12
  class Error < StandardError; end
12
- class RequestError < Error; end
13
+
14
+ # request error class
15
+ class RequestError < Error
16
+ attr_reader :status, :code, :response
17
+
18
+ def initialize(response)
19
+ @response = response
20
+ @status = response.message || "Undefined"
21
+ @code = response.code.to_i
22
+
23
+ super("Request Error: #{code} #{status} - #{response.body[0..1000].inspect}")
24
+ end
25
+ end
26
+
27
+ # zipcode not found error class
28
+ class ZipcodeNotFound < RequestError; end
13
29
 
14
30
  # fetch address information from a Brazilian CEP
15
31
  # @param [String] cep the CEP to fetch
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brazil-cep
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Vinciguerra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-03-27 00:00:00.000000000 Z
11
+ date: 2024-03-28 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Brazil::CEP is a gem to provide interface to use common CEP services
14
14
  email:
@@ -38,6 +38,7 @@ homepage: https://github.com/dvinciguerra/brazil-cep
38
38
  licenses:
39
39
  - MIT
40
40
  metadata:
41
+ rubygems_mfa_required: 'true'
41
42
  homepage_uri: https://github.com/dvinciguerra/brazil-cep
42
43
  source_code_uri: https://github.com/dvinciguerra/brazil-cep
43
44
  changelog_uri: https://github.com/dvinciguerra/brazil-cep