correios_sigep 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.rspec +3 -0
  4. data/.rubocop.yml +7 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +31 -0
  10. data/Rakefile +5 -0
  11. data/bin/rake +16 -0
  12. data/correios_sigep.gemspec +30 -0
  13. data/lib/correios_sigep.rb +38 -0
  14. data/lib/correios_sigep/builders/xml/authentication.rb +36 -0
  15. data/lib/correios_sigep/builders/xml/collect.rb +33 -0
  16. data/lib/correios_sigep/builders/xml/collect_objects.rb +25 -0
  17. data/lib/correios_sigep/builders/xml/product.rb +21 -0
  18. data/lib/correios_sigep/builders/xml/recipient.rb +30 -0
  19. data/lib/correios_sigep/builders/xml/request_collect_number.rb +16 -0
  20. data/lib/correios_sigep/builders/xml/sender.rb +34 -0
  21. data/lib/correios_sigep/configuration.rb +6 -0
  22. data/lib/correios_sigep/logistic_reverse/base_client.rb +32 -0
  23. data/lib/correios_sigep/logistic_reverse/request_collect_number.rb +53 -0
  24. data/lib/correios_sigep/models/collect.rb +27 -0
  25. data/lib/correios_sigep/models/correios_response_codes.rb +12 -0
  26. data/lib/correios_sigep/models/errors/collect_not_answered_for_the_zipcode.rb +8 -0
  27. data/lib/correios_sigep/models/errors/inexistent_zipcode.rb +8 -0
  28. data/lib/correios_sigep/models/errors/ticket_already_used.rb +8 -0
  29. data/lib/correios_sigep/models/errors/unavailable_house_collect.rb +8 -0
  30. data/lib/correios_sigep/models/errors/unavailable_service.rb +8 -0
  31. data/lib/correios_sigep/models/errors/unknown_error.rb +8 -0
  32. data/lib/correios_sigep/models/logistic_reverse.rb +23 -0
  33. data/lib/correios_sigep/models/object.rb +16 -0
  34. data/lib/correios_sigep/models/product.rb +14 -0
  35. data/lib/correios_sigep/models/recipient.rb +25 -0
  36. data/lib/correios_sigep/models/sender.rb +30 -0
  37. data/lib/correios_sigep/version.rb +3 -0
  38. data/spec/correios_sigep/builders/xml/request_collect_number_spec.rb +22 -0
  39. data/spec/correios_sigep/configuration_spec.rb +84 -0
  40. data/spec/correios_sigep/logistic_reverse/base_client_spec.rb +25 -0
  41. data/spec/correios_sigep/logistic_reverse/request_collect_number_spec.rb +101 -0
  42. data/spec/correios_sigep/models/collect_spec.rb +149 -0
  43. data/spec/correios_sigep/models/logistic_reverse_spec.rb +62 -0
  44. data/spec/correios_sigep/models/object_spec.rb +53 -0
  45. data/spec/correios_sigep/models/product_spec.rb +37 -0
  46. data/spec/correios_sigep/models/recipient_spec.rb +108 -0
  47. data/spec/correios_sigep/models/sender_spec.rb +141 -0
  48. data/spec/correios_sigep_spec.rb +41 -0
  49. data/spec/fixtures/correios/response_already_in_use.xml +14 -0
  50. data/spec/fixtures/correios/response_inexistent_zipcode.xml +14 -0
  51. data/spec/fixtures/correios/response_not_answered_for_zipcode.xml +14 -0
  52. data/spec/fixtures/correios/response_success.xml +27 -0
  53. data/spec/fixtures/correios/response_unavailable_house_collect.xml +14 -0
  54. data/spec/fixtures/correios/response_unavailable_service.xml +14 -0
  55. data/spec/fixtures/correios/response_unexpected.xml +14 -0
  56. data/spec/fixtures/correios/wsdl.xml +188 -0
  57. data/spec/spec_helper.rb +38 -0
  58. data/spec/support/fixture_helper.rb +5 -0
  59. data/spec/support/logistic_reverse_helper.rb +15 -0
  60. metadata +240 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 02e1206457931762afbf9a81eaf58edd50cbf3d5
4
+ data.tar.gz: eb941879c5b39fe9444f1358528d72abf9411881
5
+ SHA512:
6
+ metadata.gz: dd24a24f2d2e290d339772a5d330a5cf550b31c51ae7892aef4c7522abbd49d1ba374e03e00c2fe48e7d4efb11546efc3739ea1f9f2e4527dd7aa02f4da1ddb3
7
+ data.tar.gz: ae83a8d394ada4d0edfc35bad75e7f44372d5cdf59bd9a3a90ca20a968342d71f72288ee3bb89471cf48d9893183fe1c0ffcd8e7dd9923e912b00b5b1d5241a8
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ Documentation:
2
+ Enabled: false
3
+ AllCops:
4
+ Exclude:
5
+ - 'bin/*'
6
+ - 'correios_sigep.gemspec'
7
+ - 'spec/**/*'
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ correios-sigep
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in correios_sigep.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Carlos Ribeiro
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ CorreiosSigep
2
+ =============
3
+
4
+ Integrador responsável pela comunicação com o SigepWeb - http://goo.gl/z8VJjJ, sistema dos Correios.
5
+ Documentação do SigepWeb: http://goo.gl/6TWp2f
6
+
7
+ - Atualmente a Gem faz somente uma solicitação de uma postagem de reversa (documentação, pág 36).
8
+
9
+ ## Instalação
10
+
11
+ Adicione ao seu Gemfile:
12
+
13
+ ```ruby
14
+ gem 'correios_sigep'
15
+ ```
16
+
17
+ E execute:
18
+
19
+ $ bundle
20
+
21
+ Ou instale manualmente com:
22
+
23
+ $ gem install correios_sigep
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/correios_sigep/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,5 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ task :console do
4
+ exec 'irb -r correios_sigep -I ./lib'
5
+ end
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'correios_sigep/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'correios_sigep'
8
+ spec.version = CorreiosSigep::VERSION
9
+ spec.authors = ['Carlos Ribeiro', 'Felipe Gentil']
10
+ spec.email = ['duduribeiro.gba@gmail.com', 'mail@carlosribeiro.me', 'cdigentil@gmail.com']
11
+ spec.summary = 'A gem that integrates with Correios SIGEP WEB.'
12
+ spec.description = 'An integration with Correios SIGEP WEB system (http://www.correios.com.br/para-sua-empresa/encomendas/sigep-web)'
13
+ spec.homepage = ''
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_dependency 'savon', '~> 2.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'pry-meta', '~> 0.0.10'
25
+ spec.add_development_dependency 'rake', '~> 10.0'
26
+ spec.add_development_dependency 'rspec', '~> 3.0'
27
+ spec.add_development_dependency 'rubocop', '~> 0.29'
28
+ spec.add_development_dependency 'simplecov', '~> 0.9'
29
+ spec.add_development_dependency 'webmock', '~> 1.0'
30
+ end
@@ -0,0 +1,38 @@
1
+ require 'correios_sigep/version'
2
+ require 'correios_sigep/configuration'
3
+ require 'correios_sigep/builders/xml/recipient'
4
+ require 'correios_sigep/builders/xml/sender'
5
+ require 'correios_sigep/builders/xml/product'
6
+ require 'correios_sigep/builders/xml/collect_objects'
7
+ require 'correios_sigep/builders/xml/collect'
8
+ require 'correios_sigep/builders/xml/authentication'
9
+ require 'correios_sigep/builders/xml/request_collect_number'
10
+ require 'correios_sigep/models/recipient'
11
+ require 'correios_sigep/models/product'
12
+ require 'correios_sigep/models/recipient'
13
+ require 'correios_sigep/models/sender'
14
+ require 'correios_sigep/models/object'
15
+ require 'correios_sigep/models/sender'
16
+ require 'correios_sigep/models/collect'
17
+ require 'correios_sigep/models/logistic_reverse'
18
+ require 'correios_sigep/models/correios_response_codes'
19
+ require 'correios_sigep/models/errors/ticket_already_used'
20
+ require 'correios_sigep/models/errors/unavailable_service'
21
+ require 'correios_sigep/models/errors/inexistent_zipcode'
22
+ require 'correios_sigep/models/errors/unavailable_house_collect'
23
+ require 'correios_sigep/models/errors/collect_not_answered_for_the_zipcode'
24
+ require 'correios_sigep/models/errors/unknown_error'
25
+ require 'correios_sigep/logistic_reverse/base_client'
26
+ require 'correios_sigep/logistic_reverse/request_collect_number'
27
+ require 'nokogiri'
28
+ require 'savon'
29
+
30
+ module CorreiosSigep
31
+ def self.configuration
32
+ @configuration ||= Configuration.new
33
+ end
34
+
35
+ def self.configure
36
+ yield(configuration)
37
+ end
38
+ end
@@ -0,0 +1,36 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class Authentication
5
+ def initialize(builder)
6
+ @builder = builder
7
+ @config = CorreiosSigep.configuration
8
+ end
9
+
10
+ def build_xml
11
+ add_node "usuario", @config.user
12
+ add_node "senha", @config.password
13
+ add_node "codAdministrativo", @config.administrative_code
14
+ add_node "contrato", @config.contract
15
+ add_node "codigo_servico", @config.service_code
16
+ add_node "cartao", @config.card
17
+ end
18
+
19
+ private
20
+
21
+ def add_node(node_name, node_content)
22
+ @builder.root.children.first.add_previous_sibling(
23
+ create_node(node_name, node_content)
24
+ )
25
+ end
26
+
27
+ def create_node(node_name, node_content)
28
+ node = Nokogiri::XML::Node.new(node_name, @builder)
29
+ node.content = node_content
30
+ node
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,33 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class Collect
5
+ def initialize(builder, collect)
6
+ @builder = builder
7
+ @collect = collect
8
+ end
9
+
10
+ def build_xml
11
+ @builder.coletas_solicitadas do
12
+ @builder.tipo @collect.type
13
+ @builder.id_cliente @collect.client_id
14
+ @builder.valor_declarado @collect.declared_value
15
+ @builder.descricao @collect.description
16
+ @builder.cklist @collect.checklist
17
+ @builder.numero @collect.number
18
+ @builder.ag @collect.ag
19
+ @builder.cartao @collect.card
20
+ @builder.servico_adicional @collect.aditional_service
21
+ @builder.ar @collect.ar
22
+ XML::Sender.new(@builder, @collect.sender).build_xml
23
+ XML::Product.new(@builder, @collect.product).build_xml
24
+ @builder.obj_col do
25
+ XML::CollectObjects.new(@builder, @collect.objects).build_xml
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class CollectObjects
5
+ def initialize(builder, objects=[])
6
+ @builder = builder
7
+ @objects = objects
8
+ end
9
+
10
+ def build_xml
11
+ @objects.each do |object|
12
+ @builder.obj do
13
+ @builder.item object.item
14
+ @builder.id object.id
15
+ @builder.desc object.description
16
+ @builder.ship object.ship
17
+ @builder.num object.num
18
+ end
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class Product
5
+ def initialize(builder, product)
6
+ @builder = builder
7
+ @product = product
8
+ end
9
+
10
+ def build_xml
11
+ @builder.produto do
12
+ @builder.codigo @product.code
13
+ @builder.tipo @product.type
14
+ @builder.qtd @product.quantity
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,30 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class Recipient
5
+ def initialize(builder, recipient)
6
+ @builder = builder
7
+ @recipient = recipient
8
+ end
9
+
10
+ def build_xml
11
+ @builder.destinatario do
12
+ @builder.nome @recipient.name
13
+ @builder.logradouro @recipient.address
14
+ @builder.numero @recipient.number
15
+ @builder.complemento @recipient.complement
16
+ @builder.bairro @recipient.neighborhood
17
+ @builder.referencia @recipient.reference
18
+ @builder.cidade @recipient.city
19
+ @builder.uf @recipient.state
20
+ @builder.cep @recipient.postal_code
21
+ @builder.ddd @recipient.area_code
22
+ @builder.telefone @recipient.phone
23
+ @builder.email @recipient.email
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,16 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class RequestCollectNumber
5
+
6
+ def self.build_xml(logistic_reverse)
7
+ document = Nokogiri::XML(logistic_reverse.to_xml)
8
+ XML::Authentication.new(document).build_xml
9
+ document.to_xml(save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION)
10
+ .gsub(/<(\/)?root>/, '')
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,34 @@
1
+ module CorreiosSigep
2
+ module Builders
3
+ module XML
4
+ class Sender
5
+ def initialize(builder, sender)
6
+ @builder = builder
7
+ @sender = sender
8
+ end
9
+
10
+ def build_xml
11
+ @builder.remetente do
12
+ @builder.nome @sender.name
13
+ @builder.logradouro @sender.address
14
+ @builder.numero @sender.number
15
+ @builder.complemento @sender.complement
16
+ @builder.bairro @sender.neighborhood
17
+ @builder.reference @sender.reference
18
+ @builder.cidade @sender.city
19
+ @builder.uf @sender.state
20
+ @builder.cep @sender.postal_code
21
+ @builder.ddd @sender.area_code
22
+ @builder.telefone @sender.phone
23
+ @builder.email @sender.email
24
+ @builder.identificacao @sender.identification
25
+ @builder.ddd_celular @sender.mobile_area_code
26
+ @builder.celular @sender.mobile_phone
27
+ @builder.sms @sender.sms
28
+ end
29
+ end
30
+
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,6 @@
1
+ module CorreiosSigep
2
+ class Configuration
3
+ attr_accessor :administrative_code, :card, :contract, :password,
4
+ :service_code, :user, :wsdl_base_url
5
+ end
6
+ end
@@ -0,0 +1,32 @@
1
+ module CorreiosSigep
2
+ module LogisticReverse
3
+ class BaseClient
4
+ def initialize
5
+ @client = Savon.client(wsdl: wsdl)
6
+ end
7
+
8
+ def wsdl
9
+ @wsdl ||= if ENV['GEM_ENV'] == 'test'
10
+ 'http://webservicescolhomologacao.correios.com.br/ScolWeb/WebServiceScol?wsdl'
11
+ else
12
+ 'http://webservicescol.correios.com.br/ScolWeb/WebServiceScol?wsdl'
13
+ end
14
+ end
15
+
16
+ def invoke(method, message)
17
+ @client.instance_variable_set(:@wsdl,
18
+ Wasabi::Document.new(CorreiosSigep.configuration.wsdl_base_url)) if wsdl_base_url_changed?
19
+ @client.call(method, message: message)
20
+ end
21
+
22
+ private
23
+ def wsdl_base_url_changed?
24
+ wsdl_base_url && wsdl_base_url != @client.instance_variable_get(:@wsdl).document
25
+ end
26
+
27
+ def wsdl_base_url
28
+ CorreiosSigep.configuration.wsdl_base_url
29
+ end
30
+ end
31
+ end
32
+ end