brazil-cep 0.1.0

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: ebcf36ea20edd02418b626e0741972620cea2b3a4af54dbaffb0413c4af1bb1e
4
+ data.tar.gz: 4fa2527e8007a0f0d7dc5a13a72f54a54508be5aab0887dfb21582b7c395ccdf
5
+ SHA512:
6
+ metadata.gz: 3aa434fd7b4ab0149bf7a70167c2302b3eb78782d5df6077e18763b3f24e5eb5d70e151384cced2b46d1676d54f90a4a14b2ba710ef4f537842675df8ad9ea52
7
+ data.tar.gz: 2048b051f9853cc1fce960257e49c13d30f11a20d10414c914ce0f4d068fe96e9ea240e72c3eec76597097d2873e5c0a8f84b84301c5e90199a9d106b83f445b
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-03-26
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at daniel.vinciguerra@bivee.com.br. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Daniel Vinciguerra
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # Brazil::Cep
2
+
3
+ This gem provides a simple way to validate and format Brazilian postal codes (CEP).
4
+
5
+ ## Installation
6
+
7
+ `$ gem install brazil-cep`
8
+
9
+ Or add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'brazil-cep'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Fetching address by CEP using default provider
18
+
19
+ ```ruby
20
+ address = Brazil::Cep.fetch('01311-200')
21
+ address.street # => "Avenida Paulista"
22
+ address.neighborhood # => "Bela Vista"
23
+ ```
24
+
25
+ ### Fetching address by CEP using custom provider
26
+
27
+ ```ruby
28
+ address = Brazil::Cep.fetch('01311-200', provider: :postmon)
29
+ address.street # => "Avenida Paulista"
30
+ address.neighborhood # => "Bela Vista"
31
+ ```
32
+
33
+ ### Creating a custom provider adapter
34
+
35
+ ```ruby
36
+ module Brazil
37
+ module Cep
38
+ module Adapters
39
+ class MyProvider < Base
40
+ provider base_url: 'https://api.myprovider.com/address/{{cep}}'
41
+
42
+ private
43
+
44
+ def transformation!
45
+ address_params = {
46
+ street: response[:street],
47
+ neighborhood: response[:neighborhood],
48
+ ...
49
+ }
50
+
51
+ Brazil::Cep::Address.new(**address_params)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ ```
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dvinciguerra/brazil-cep. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/dvinciguerra/brazil-cep/blob/master/CODE_OF_CONDUCT.md).
61
+
62
+ ## Code of Conduct
63
+
64
+ 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
+
67
+ ## License
68
+
69
+ Created by Daniel Vinciguerra and available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/lib/brazil-cep.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift File.expand_path("../lib", __dir__)
4
+
5
+ require "brazil_cep"
@@ -0,0 +1,84 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+
6
+ module Brazil
7
+ module Cep
8
+ module Adapters
9
+ # Base adapter class
10
+ # @abstract
11
+ class Base
12
+ def self.provider(**params)
13
+ @__provider = params
14
+ end
15
+
16
+ # class constructor method
17
+ # @param [String] cep the CEP to fetch (optional)
18
+ def initialize(cep = nil)
19
+ @cep = cep
20
+ @__provider = self.class.instance_variable_get(:@__provider) || {}
21
+
22
+ @open_timeout = ENV.fetch("BRAZIL_CEP_OPEN_TIMEOUT", 3).to_i
23
+ @read_timeout = ENV.fetch("BRAZIL_CEP_READ_TIMEOUT", 3).to_i
24
+ @write_timeout = ENV.fetch("BRAZIL_CEP_WRITE_TIMEOUT", 3).to_i
25
+ end
26
+
27
+ # fetch address information from a Brazilian CEP
28
+ # @param [String] value the CEP to fetch
29
+ # @return [Hash] the address information
30
+ def fetch(value = nil)
31
+ @cep = value unless value.nil?
32
+
33
+ http_request(provider_url) do |http_response|
34
+ raise Brazil::Cep::RequestError, http_response unless http_response.is_a?(Net::HTTPSuccess)
35
+ end
36
+
37
+ deserialization!
38
+ transformation!
39
+ end
40
+
41
+ alias get fetch
42
+
43
+ private
44
+
45
+ attr_reader :cep, :response, :payload, :open_timeout, :read_timeout, :write_timeout
46
+
47
+ def http_request(uri, **options, &block)
48
+ http_options =
49
+ { open_timeout: open_timeout, read_timeout: read_timeout, write_timeout: write_timeout }
50
+ .update(**options)
51
+
52
+ http_options[:use_ssl] = uri.scheme == "https"
53
+
54
+ Net::HTTP.start(uri.host, uri.port, **http_options) do |http|
55
+ request = Net::HTTP::Get.new(uri)
56
+ @response = http.request(request)
57
+
58
+ block.call(@response)
59
+ end
60
+ end
61
+
62
+ # returns the provider URL from provider base_url attribute
63
+ # @return [URI] the provider URL
64
+ def provider_url
65
+ raise ArgumentError unless @__provider.key?(:base_url)
66
+
67
+ URI.parse(@__provider[:base_url].gsub("{{cep}}", cep))
68
+ end
69
+
70
+ # deserialization method
71
+ # @return [Hash] the deserialized payload
72
+ def deserialization!
73
+ @payload = JSON.parse(response.body, symbolize_names: true)
74
+ end
75
+
76
+ # transformation method
77
+ # @return [Hash] the transformed payload
78
+ def transformation!
79
+ @payload
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ module Adapters
6
+ # Postmon provider adapter class
7
+ # This class is responsible for fetching the address information from the Postmon provider
8
+ class Postmon < Base
9
+ provider base_url: "https://api.postmon.com.br/v1/cep/{{cep}}"
10
+
11
+ private
12
+
13
+ def transformation!
14
+ address_params = {
15
+ cep: @payload[:cep],
16
+ state: @payload[:estado],
17
+ city: @payload[:cidade],
18
+ neighborhood: @payload[:bairro],
19
+ street: @payload[:logradouro],
20
+ complement: @payload[:complemento],
21
+ meta: @payload.update(provider: :postmon)
22
+ }
23
+
24
+ Brazil::Cep::Address.new(**address_params)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ module Adapters
6
+ # Republica Virtual provider adapter class
7
+ # This class is responsible for fetching the address information from the Republica Virtual provider
8
+ class RepublicaVirtual < Base
9
+ provider base_url: "http://cep.republicavirtual.com.br/web_cep.php?cep={{cep}}&formato=json"
10
+
11
+ private
12
+
13
+ def transformation!
14
+ address_params = {
15
+ cep: @cep,
16
+ state: @payload[:uf],
17
+ city: @payload[:cidade],
18
+ neighborhood: @payload[:bairro],
19
+ street: "#{@payload[:tipo_logradouro]} #{@payload[:logradouro]}",
20
+ complement: @payload[:complemento],
21
+ meta: @payload.update(provider: :republica_virtual)
22
+ }
23
+
24
+ Brazil::Cep::Address.new(**address_params)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ module Adapters
6
+ # Viacep provider adapter class
7
+ # This class is responsible for fetching the address information from the Viacep provider
8
+ class Viacep < Base
9
+ provider base_url: "https://viacep.com.br/ws/{{cep}}/json/"
10
+
11
+ private
12
+
13
+ def transformation!
14
+ address_params = {
15
+ cep: @payload[:cep],
16
+ state: @payload[:uf],
17
+ city: @payload[:localidade],
18
+ neighborhood: @payload[:bairro],
19
+ street: @payload[:logradouro],
20
+ complement: @payload[:complemento],
21
+ meta: @payload.update(provider: :postmon)
22
+ }
23
+
24
+ Brazil::Cep::Address.new(**address_params)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ # Adapters class namespace
6
+ module Adapters
7
+ autoload :Base, "brazil_cep/adapters/base"
8
+ autoload :Viacep, "brazil_cep/adapters/viacep"
9
+ autoload :Postmon, "brazil_cep/adapters/postmon"
10
+ autoload :RepublicaVirtual, "brazil_cep/adapters/republica_virtual"
11
+
12
+ # list of provider keys and classes
13
+ PROVIDERS = {
14
+ viacep: Viacep,
15
+ postmon: Postmon,
16
+ republica_virtual: RepublicaVirtual
17
+ }.freeze
18
+
19
+ def self.get(provider = :viacep)
20
+ PROVIDERS[provider].new
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ # Address class
6
+ class Address
7
+ attr_reader :cep, :state, :city, :neighborhood, :street, :complement, :meta
8
+
9
+ def initialize(**arguments)
10
+ @cep = arguments[:cep]
11
+ @state = arguments[:state]
12
+ @city = arguments[:city]
13
+ @neighborhood = arguments[:neighborhood]
14
+ @street = arguments[:street]
15
+ @complement = arguments[:complement]
16
+ @meta = arguments[:meta]
17
+ end
18
+
19
+ def provider
20
+ meta[:provider]
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brazil
4
+ module Cep
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/brazil_cep.rb ADDED
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "brazil_cep/version"
4
+ require_relative "brazil_cep/address"
5
+ require_relative "brazil_cep/adapters"
6
+
7
+ module Brazil
8
+ # Brazil::Cep providers a simple way to fetch address information from a Brazilian CEP
9
+ # @public
10
+ module Cep
11
+ class Error < StandardError; end
12
+ class RequestError < Error; end
13
+
14
+ # fetch address information from a Brazilian CEP
15
+ # @param [String] cep the CEP to fetch
16
+ # @return [Hash] the address information
17
+ def self.fetch(cep, provider: :viacep)
18
+ provider = Adapters.get(provider)
19
+ provider.get(cep)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
1
+ module Brazil
2
+ module Cep
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brazil-cep
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Vinciguerra
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-03-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Brazil::CEP is a gem to provide interface to use common CEP services
14
+ email:
15
+ - daniel.vinciguerra@bivee.com.br
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/brazil-cep.rb
28
+ - lib/brazil_cep.rb
29
+ - lib/brazil_cep/adapters.rb
30
+ - lib/brazil_cep/adapters/base.rb
31
+ - lib/brazil_cep/adapters/postmon.rb
32
+ - lib/brazil_cep/adapters/republica_virtual.rb
33
+ - lib/brazil_cep/adapters/viacep.rb
34
+ - lib/brazil_cep/address.rb
35
+ - lib/brazil_cep/version.rb
36
+ - sig/brazil/cep.rbs
37
+ homepage: https://github.com/dvinciguerra/brazil-cep
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/dvinciguerra/brazil-cep
42
+ source_code_uri: https://github.com/dvinciguerra/brazil-cep
43
+ changelog_uri: https://github.com/dvinciguerra/brazil-cep
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.7.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.4.22
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: Brazil::CEP is a gem to provide interface to use common CEP services
63
+ test_files: []