correios_toolkit 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 48cb312a91a8bba19fae4653d1bb6b6476ff3a7d5c4ab66664511e967ba377b9
4
+ data.tar.gz: 69f73b4dc4c685689ef26b49a3d968faaeac18381c0b64cae59cdf872b45cc0b
5
+ SHA512:
6
+ metadata.gz: 6fa43e15edce41f9250a634f3434767da8c663d4e88df75f2f22a0bee11ef9abc4333d7bfb25f8748ae5e031105d22d84958a02ad2639bfafb630dd2788cd127
7
+ data.tar.gz: 35969cf786c7065cde5d99a2b86e3d8945bee14300670e50d619f5fb7ab1f5c5690f2ca0daead2397f7142c654b27bc8f3494cf2aaa3decfc4e62495268ad88a
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at diligasi@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # Ruby library for an easy integration with the Correios (brazilian post office service) SOAP web service.
2
+
3
+ ![GitHub license](https://img.shields.io/github/license/diligasi/correios_toolkit)
4
+ [![GitHub version](https://badge.fury.io/gh/diligasi%2Fcorreios_toolkit.svg)](https://badge.fury.io/gh/diligasi%2Fcorreios_toolkit)
5
+ ![GitHub top language](https://img.shields.io/github/languages/top/diligasi/correios_toolkit)
6
+ ![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/diligasi/correios_toolkit)
7
+
8
+ The _Correios do Brasil_ (brazilian post office service) makes available, free of charge, to customers
9
+ with or without a contract, a set of services accessible via a SOAP Web service that enable the
10
+ integration from customer's systems with the Correios' technological environment. Therefore, this gem
11
+ aims to create an abstraction layer that facilitates the communication and consumption of the services
12
+ provided from ruby applications.
13
+
14
+ ## Requirements
15
+ Ruby 2.0 and newer are supported. Alternative interpreters compatible with 2.0+ should work as well.
16
+
17
+ Earlier Ruby versions such as 1.8.7, 1.9.2, and 1.9.3 were not tested, don't have any official support,
18
+ and will not receive features and/or security updates.
19
+
20
+ The _correios_toolkit_ gem depends on these other gems for usage at runtime:
21
+
22
+ * [activesupport](https://rubygems.org/gems/activesupport)
23
+ * [rest-client](https://rubygems.org/gems/rest-client)
24
+
25
+ ## Installation
26
+ Add this line to your application's Gemfile:
27
+
28
+ ```ruby
29
+ gem 'correios_toolkit'
30
+ ```
31
+
32
+ And then execute:
33
+
34
+ $ bundle
35
+
36
+ Or install it yourself as:
37
+
38
+ $ gem install correios_toolkit
39
+
40
+ ## Usage
41
+ Basic usage:
42
+
43
+ ```ruby
44
+ require 'correios_toolkit'
45
+
46
+ CorreiosToolkit.consulta_cep('01310000')
47
+ ```
48
+
49
+ ## Current available services
50
+ On the following list are the services already available on this gem:
51
+
52
+ * _ConsultaCEP_
53
+ * Get address' information by zip code
54
+ * Others are coming soon...
55
+
56
+ ## Contributing
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/diligasi/correios_toolkit. This
58
+ project is intended to be a safe, welcoming space for collaboration, and contributors are expected to
59
+ adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
60
+
61
+ ## License
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Code of Conduct
65
+ Everyone interacting in the CorreiosToolkit project's codebases, issue trackers, chat rooms and mailing
66
+ lists is expected to follow the [code of conduct](https://github.com/diligasi/correios_toolkit/blob/master/CODE_OF_CONDUCT.md).
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "correios_toolkit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,43 @@
1
+ require 'correios_toolkit/version'
2
+ require 'correios_toolkit/base'
3
+ require 'correios_toolkit/consulta_cep'
4
+
5
+ ##
6
+ # This module's static methods are the entry point for using CorreiosToolkit.
7
+ module CorreiosToolkit
8
+
9
+ # Get address' information by zip code
10
+ #
11
+ # Example:
12
+ # # Valid zip code (without dots, hyphens and the like)
13
+ # >> CorreiosToolkit.consulta_cep('01310000')
14
+ # => { "bairro"=>"Bela Vista", "cep"=>"01310000", "cidade"=>"São Paulo", "complemento2"=>"- até 610 - lado par", "end"=>"Avenida Paulista", "uf"=>"SP" }
15
+ #
16
+ # # Valid zip code (with dots, hyphens and the like)
17
+ # >> CorreiosToolkit.consulta_cep('01310-000')
18
+ # => { "bairro"=>"Bela Vista", "cep"=>"01310000", "cidade"=>"São Paulo", "complemento2"=>"- até 610 - lado par", "end"=>"Avenida Paulista", "uf"=>"SP" }
19
+ #
20
+ # # Invalid zip code (less/more than 8 numbers)
21
+ # >> CorreiosToolkit.consulta_cep('01310000000')
22
+ # => CorreiosToolkit::Base::LengthError (Wrong CEP format, expected CEP to have 8 numbers but 11 was found.)
23
+ #
24
+ # # Invalid zip code (nonexistent zip code - zip code not found)
25
+ # >> CorreiosToolkit.consulta_cep('01310005')
26
+ # => CorreiosToolkit::Base::GatewayError (CEP NAO ENCONTRADO)
27
+ #
28
+ # Param:
29
+ # <tt>:cep</tt> [String] - The zip code to verify, it must have 8 number
30
+ #
31
+ # Return:
32
+ # # Hash object with indifferent access attributes
33
+ # # => Hash attributes:
34
+ # # <tt>:bairro</tt> - The neighborhood name
35
+ # # <tt>:cep</tt> - The requested zip code
36
+ # # <tt>:cidade</tt> - The city name
37
+ # # <tt>:complemento2</tt> - The complement when available
38
+ # # <tt>:end</tt> - The street name
39
+ # # <tt>:uf</tt> - The state abbreviation
40
+ def self.consulta_cep(cep)
41
+ CorreiosToolkit::ConsultaCep.request_data_for(cep: cep)
42
+ end
43
+ end
@@ -0,0 +1,49 @@
1
+ require 'rest-client'
2
+ require 'active_support/core_ext'
3
+
4
+ ##
5
+ # This class concentrate the basic information to retrieve data from the web
6
+ # service. This class is intended to be inherited by all other classes.
7
+ module CorreiosToolkit
8
+ class Base
9
+ class GatewayError < StandardError; end
10
+ class LengthError < StandardError; end
11
+
12
+ SOAP_URL = 'https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente?wsdl'.freeze
13
+ CORREIOS_DEFAULT_ENCODE = 'ISO-8859-1'.freeze
14
+
15
+ protected
16
+
17
+ def request(payload:, response_node:)
18
+ response = RestClient.post(SOAP_URL, payload, content_type: 'application/xml')
19
+
20
+ if response.code.between?(200, 299)
21
+ json_parse(response.body, response_node)
22
+ else
23
+ raise(RestClient::ExceptionWithResponse, response)
24
+ end
25
+ rescue RestClient::ExceptionWithResponse => e
26
+ raise(GatewayError, json_parse(e.response.body))
27
+ end
28
+
29
+ private
30
+
31
+ def json_parse(xml, node = nil)
32
+ xml.encode!('UTF-8', CORREIOS_DEFAULT_ENCODE)
33
+
34
+ if node.present?
35
+ success_parser(xml, node)
36
+ else
37
+ error_parser(xml)
38
+ end
39
+ end
40
+
41
+ def success_parser(xml, node)
42
+ Hash.from_xml(xml).dig('Envelope', 'Body', "#{node}Response", 'return').with_indifferent_access
43
+ end
44
+
45
+ def error_parser(xml)
46
+ Hash.from_xml(xml).dig('Envelope', 'Body', 'Fault', 'faultstring')
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,41 @@
1
+ # <b>This class is intended to get the full address information for a given zip
2
+ # code.</b> In order to do that, this class receives the zip code value with its
3
+ # 8 digits, normalizes it by removing dots, hyphens and the like, then, requests
4
+ # the information about it from the brazilian post office (Correios) and returns
5
+ # a hash with the corresponding information.
6
+ module CorreiosToolkit
7
+ class ConsultaCep < CorreiosToolkit::Base
8
+
9
+ def self.request_data_for(cep:)
10
+ consulta_cep = new(cep: cep)
11
+ consulta_cep.data
12
+ end
13
+
14
+ def initialize(cep:)
15
+ cep = cep.scan(/[0-9]/).join
16
+
17
+ raise(LengthError, "Wrong CEP format, expected CEP to have 8 numbers but #{cep.size} was found.") if cep.size != 8
18
+
19
+ @cep = cep
20
+ end
21
+
22
+ def data
23
+ request(payload: payload, response_node: 'consultaCEP')
24
+ end
25
+
26
+ private
27
+
28
+ def payload
29
+ <<-EOXML
30
+ <x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:cli="http://cliente.bean.master.sigep.bsb.correios.com.br/">
31
+ <x:Header/>
32
+ <x:Body>
33
+ <cli:consultaCEP>
34
+ <cep>#{@cep}</cep>
35
+ </cli:consultaCEP>
36
+ </x:Body>
37
+ </x:Envelope>
38
+ EOXML
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module CorreiosToolkit
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: correios_toolkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Diogo de Lima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 12.3.3
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 12.3.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.9'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.9'
55
+ description: |2
56
+ The Correios do Brasil (brazilian post office service) makes available, free of charge, to
57
+ customers with or without a contract, a set of services accessible via a SOAP Web service
58
+ that enable the integration from customer's systems with the Correios' technological
59
+ environment.
60
+ Therefore, this gem aims to create an abstraction layer that facilitates the communication
61
+ and consumption of the services provided from ruby applications.
62
+ email:
63
+ - diligasi@gmail.com
64
+ executables: []
65
+ extensions: []
66
+ extra_rdoc_files: []
67
+ files:
68
+ - CODE_OF_CONDUCT.md
69
+ - README.md
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/correios_toolkit.rb
73
+ - lib/correios_toolkit/base.rb
74
+ - lib/correios_toolkit/consulta_cep.rb
75
+ - lib/correios_toolkit/version.rb
76
+ homepage: https://rubygems.org/gems/correios_toolkit
77
+ licenses:
78
+ - MIT
79
+ metadata:
80
+ homepage_uri: https://rubygems.org/gems/correios_toolkit
81
+ source_code_uri: https://github.com/diligasi/correios_toolkit
82
+ changelog_uri: https://github.com/diligasi/correios_toolkit/releases
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubygems_version: 3.0.3
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Ruby library for an easy integration with the Correios (brazilian post office
102
+ service) SOAP web service.
103
+ test_files: []