correios-frete 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.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +31 -0
- data/README.rdoc +139 -0
- data/Rakefile +45 -0
- data/correios-frete.gemspec +68 -0
- data/lib/correios-frete.rb +6 -0
- data/lib/correios/frete.rb +48 -0
- data/lib/correios/frete/parser.rb +22 -0
- data/lib/correios/frete/servico.rb +64 -0
- data/lib/correios/frete/version.rb +11 -0
- data/lib/correios/frete/web_service.rb +37 -0
- data/spec/correios/frete/parser_spec.rb +69 -0
- data/spec/correios/frete/servico_spec.rb +145 -0
- data/spec/correios/frete/web_service_spec.rb +42 -0
- data/spec/correios/frete_spec.rb +90 -0
- data/spec/spec_helper.rb +12 -0
- metadata +130 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.2)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
nokogiri (1.4.6)
|
11
|
+
rake (0.9.2)
|
12
|
+
rspec (2.6.0)
|
13
|
+
rspec-core (~> 2.6.0)
|
14
|
+
rspec-expectations (~> 2.6.0)
|
15
|
+
rspec-mocks (~> 2.6.0)
|
16
|
+
rspec-core (2.6.4)
|
17
|
+
rspec-expectations (2.6.0)
|
18
|
+
diff-lcs (~> 1.1.2)
|
19
|
+
rspec-mocks (2.6.0)
|
20
|
+
sax-machine (0.0.16)
|
21
|
+
nokogiri (> 0.0.0)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (~> 1.0.0)
|
28
|
+
jeweler
|
29
|
+
nokogiri
|
30
|
+
rspec
|
31
|
+
sax-machine
|
data/README.rdoc
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
= correios-frete
|
2
|
+
|
3
|
+
Cálculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).
|
4
|
+
|
5
|
+
|
6
|
+
== Instalando
|
7
|
+
|
8
|
+
=== Gemfile
|
9
|
+
gem 'correios-frete'
|
10
|
+
|
11
|
+
=== Instalação direta
|
12
|
+
$ gem install correios-frete
|
13
|
+
|
14
|
+
|
15
|
+
== Usando
|
16
|
+
|
17
|
+
require 'correios-frete'
|
18
|
+
|
19
|
+
frete = Correios::Frete.new :cep_origem => "04094-050",
|
20
|
+
:cep_destino => "90619-900",
|
21
|
+
:peso => 0.3,
|
22
|
+
:comprimento => 30,
|
23
|
+
:largura => 15,
|
24
|
+
:altura => 2
|
25
|
+
|
26
|
+
# ------------------------------------
|
27
|
+
# Cálculo de um serviço de frete por vez
|
28
|
+
# ------------------------------------
|
29
|
+
servico = frete.calcular :sedex
|
30
|
+
servico.valor # => 26.2
|
31
|
+
servico.prazo_entrega # => 1
|
32
|
+
|
33
|
+
servico = frete.calcular :pac
|
34
|
+
servico.valor # => 10.0
|
35
|
+
servico.prazo_entrega # => 5
|
36
|
+
|
37
|
+
# ------------------------------------
|
38
|
+
# Cálculo de vários serviços ao mesmo tempo
|
39
|
+
# ------------------------------------
|
40
|
+
servicos = frete.calcular :sedex, :pac
|
41
|
+
|
42
|
+
servicos[:sedex].valor # => 26.2
|
43
|
+
servicos[:sedex].prazo_entrega # => 1
|
44
|
+
|
45
|
+
servicos[:pac].valor # => 10.0
|
46
|
+
servicos[:pac].prazo_entrega # => 5
|
47
|
+
|
48
|
+
# ------------------------------------
|
49
|
+
# Verificação de sucesso e erro
|
50
|
+
# ------------------------------------
|
51
|
+
servico = frete.calcular :sedex
|
52
|
+
servico.success? # => true
|
53
|
+
servico.error? # => false
|
54
|
+
|
55
|
+
|
56
|
+
== Informações adicionais
|
57
|
+
|
58
|
+
=== Serviços suportados
|
59
|
+
:pac
|
60
|
+
:sedex
|
61
|
+
:sedex_10
|
62
|
+
:sedex_hoje
|
63
|
+
|
64
|
+
|
65
|
+
=== Maneiras de configurar atributos no construtor de Correios::Frete
|
66
|
+
|
67
|
+
==== Com um hash
|
68
|
+
frete = Correios::Frete.new :cep_origem => "04094-050",
|
69
|
+
:cep_destino => "90619-900",
|
70
|
+
:peso => 0.3,
|
71
|
+
:comprimento => 30,
|
72
|
+
:largura => 15,
|
73
|
+
:altura => 2
|
74
|
+
|
75
|
+
==== Com um bloco
|
76
|
+
frete = Correios::Frete.new do |f|
|
77
|
+
f.cep_origem = "04094-050"
|
78
|
+
f.cep_destino = "90619-900"
|
79
|
+
f.peso = 0.3
|
80
|
+
f.comprimento = 30
|
81
|
+
f.largura = 15
|
82
|
+
f.altura = 2
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
=== Atributos de Correios::Frete
|
87
|
+
|
88
|
+
==== String
|
89
|
+
cep_origem, cep_destino
|
90
|
+
==== Float
|
91
|
+
peso, valor_declarado
|
92
|
+
==== Fixnum
|
93
|
+
comprimento, largura, altura, diametro
|
94
|
+
==== Boolean
|
95
|
+
mao_propria, aviso_recebimento
|
96
|
+
==== Symbol
|
97
|
+
formato (:caixa_pacote, :rolo_prisma)
|
98
|
+
|
99
|
+
|
100
|
+
=== Atributos de Correios::Frete::Servico
|
101
|
+
|
102
|
+
==== String
|
103
|
+
codigo, erro, msg_erro
|
104
|
+
==== Float
|
105
|
+
valor, valor_mao_propria, valor_aviso_recebimento, valor_valor_declarado
|
106
|
+
==== Fixnum
|
107
|
+
prazo_entrega
|
108
|
+
==== Boolean
|
109
|
+
entrega_domiciliar, entrega_sabado
|
110
|
+
==== Symbol
|
111
|
+
tipo (:pac, :sedex, :sedex_10, :sedex_hoje)
|
112
|
+
|
113
|
+
== Copyright
|
114
|
+
|
115
|
+
(The MIT License)
|
116
|
+
|
117
|
+
{Prodis a.k.a. Fernando Hamasaki de Amorim}[http://prodis.blog.br]
|
118
|
+
|
119
|
+
Copyright (c) 2011 Prodis
|
120
|
+
|
121
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
122
|
+
a copy of this software and associated documentation files (the
|
123
|
+
"Software"), to deal in the Software without restriction, including
|
124
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
125
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
126
|
+
permit persons to whom the Software is furnished to do so, subject to
|
127
|
+
the following conditions:
|
128
|
+
|
129
|
+
The above copyright notice and this permission notice shall be
|
130
|
+
included in all copies or substantial portions of the Software.
|
131
|
+
|
132
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
133
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
134
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
135
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
136
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
137
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
138
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
139
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler'
|
4
|
+
begin
|
5
|
+
Bundler.setup(:default, :development)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
$stderr.puts e.message
|
8
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
require 'rake'
|
12
|
+
|
13
|
+
require 'jeweler'
|
14
|
+
require './lib/correios/frete/version'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "correios-frete"
|
18
|
+
gem.homepage = "http://github.com/prodis/correios-frete"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Cálculo de frete dos Correios.}
|
21
|
+
gem.description = %Q{Cálculo de frete dos Correios.}
|
22
|
+
gem.email = "prodis@gmail.com"
|
23
|
+
gem.authors = ["Prodis a.k.a. Fernando Hamasaki de Amorim"]
|
24
|
+
gem.version = Correios::Frete::Version::STRING
|
25
|
+
# dependencies defined in Gemfile
|
26
|
+
end
|
27
|
+
Jeweler::RubygemsDotOrgTasks.new
|
28
|
+
|
29
|
+
require 'rspec/core'
|
30
|
+
require 'rspec/core/rake_task'
|
31
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
32
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "correios-frete #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{correios-frete}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Prodis a.k.a. Fernando Hamasaki de Amorim"]
|
12
|
+
s.date = %q{2011-07-02}
|
13
|
+
s.description = %q{Cálculo de frete dos Correios.}
|
14
|
+
s.email = %q{prodis@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".document",
|
20
|
+
".rspec",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"correios-frete.gemspec",
|
26
|
+
"lib/correios-frete.rb",
|
27
|
+
"lib/correios/frete.rb",
|
28
|
+
"lib/correios/frete/parser.rb",
|
29
|
+
"lib/correios/frete/servico.rb",
|
30
|
+
"lib/correios/frete/version.rb",
|
31
|
+
"lib/correios/frete/web_service.rb",
|
32
|
+
"spec/correios/frete/parser_spec.rb",
|
33
|
+
"spec/correios/frete/servico_spec.rb",
|
34
|
+
"spec/correios/frete/web_service_spec.rb",
|
35
|
+
"spec/correios/frete_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/prodis/correios-frete}
|
39
|
+
s.licenses = ["MIT"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.6.1}
|
42
|
+
s.summary = %q{Cálculo de frete dos Correios.}
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
|
49
|
+
s.add_runtime_dependency(%q<sax-machine>, [">= 0"])
|
50
|
+
s.add_runtime_dependency(%q<rspec>, [">= 0"])
|
51
|
+
s.add_runtime_dependency(%q<bundler>, ["~> 1.0.0"])
|
52
|
+
s.add_runtime_dependency(%q<jeweler>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
55
|
+
s.add_dependency(%q<sax-machine>, [">= 0"])
|
56
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
57
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
58
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<nokogiri>, [">= 0"])
|
62
|
+
s.add_dependency(%q<sax-machine>, [">= 0"])
|
63
|
+
s.add_dependency(%q<rspec>, [">= 0"])
|
64
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
65
|
+
s.add_dependency(%q<jeweler>, [">= 0"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Correios
|
3
|
+
class Frete
|
4
|
+
attr_accessor :cep_origem, :cep_destino
|
5
|
+
attr_accessor :peso, :comprimento, :altura, :largura, :diametro
|
6
|
+
attr_accessor :formato, :mao_propria, :aviso_recebimento, :valor_declarado
|
7
|
+
attr_writer :web_service, :parser
|
8
|
+
|
9
|
+
DEFAULT_OPTIONS = {
|
10
|
+
:peso => 0.0,
|
11
|
+
:comprimento => 0.0,
|
12
|
+
:altura => 0.0,
|
13
|
+
:largura => 0.0,
|
14
|
+
:diametro => 0.0,
|
15
|
+
:formato => :caixa_pacote,
|
16
|
+
:mao_propria => false,
|
17
|
+
:aviso_recebimento => false,
|
18
|
+
:valor_declarado => 0.0
|
19
|
+
}
|
20
|
+
|
21
|
+
def initialize(options = {})
|
22
|
+
DEFAULT_OPTIONS.merge(options).each do |attr, value|
|
23
|
+
self.send("#{attr}=", value)
|
24
|
+
end
|
25
|
+
|
26
|
+
yield self if block_given?
|
27
|
+
end
|
28
|
+
|
29
|
+
def web_service
|
30
|
+
@web_service ||= Correios::Frete::WebService.new
|
31
|
+
end
|
32
|
+
|
33
|
+
def parser
|
34
|
+
@parser ||= Correios::Frete::Parser.new
|
35
|
+
end
|
36
|
+
|
37
|
+
def calcular(*service_types)
|
38
|
+
response = web_service.request(self, service_types)
|
39
|
+
services = parser.servicos(response)
|
40
|
+
|
41
|
+
if service_types.size == 1
|
42
|
+
services.values.first
|
43
|
+
else
|
44
|
+
services
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
class Correios::Frete::Parser
|
5
|
+
SERVICES = {
|
6
|
+
:pac => "41106",
|
7
|
+
:sedex => "40010",
|
8
|
+
:sedex_10 => "40215",
|
9
|
+
:sedex_hoje => "40290"
|
10
|
+
}
|
11
|
+
|
12
|
+
def servicos(xml)
|
13
|
+
servicos = {}
|
14
|
+
|
15
|
+
Nokogiri::XML(xml).root.elements.each do |element|
|
16
|
+
servico = Correios::Frete::Servico.new.parse(element.to_xml)
|
17
|
+
servicos[servico.tipo] = servico
|
18
|
+
end
|
19
|
+
|
20
|
+
servicos
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'sax-machine'
|
3
|
+
|
4
|
+
class Correios::Frete::Servico
|
5
|
+
include SAXMachine
|
6
|
+
|
7
|
+
TYPES = {
|
8
|
+
:pac => "41106",
|
9
|
+
:sedex => "40010",
|
10
|
+
:sedex_10 => "40215",
|
11
|
+
:sedex_hoje => "40290"
|
12
|
+
}
|
13
|
+
|
14
|
+
element :Codigo, :as => :codigo
|
15
|
+
element :Valor, :as => :valor
|
16
|
+
element :PrazoEntrega, :as => :prazo_entrega
|
17
|
+
element :ValorMaoPropria, :as => :valor_mao_propria
|
18
|
+
element :ValorAvisoRecebimento, :as => :valor_aviso_recebimento
|
19
|
+
element :ValorValorDeclarado, :as => :valor_valor_declarado
|
20
|
+
element :EntregaDomiciliar, :as => :entrega_domiciliar
|
21
|
+
element :EntregaSabado, :as => :entrega_sabado
|
22
|
+
element :Erro, :as => :erro
|
23
|
+
element :MsgErro, :as => :msg_erro
|
24
|
+
attr_reader :tipo
|
25
|
+
|
26
|
+
alias_method :original_parse, :parse
|
27
|
+
|
28
|
+
def parse(xml_text)
|
29
|
+
original_parse xml_text
|
30
|
+
@tipo = TYPES.key(codigo)
|
31
|
+
cast_to_float! :valor, :valor_mao_propria, :valor_aviso_recebimento, :valor_valor_declarado
|
32
|
+
cast_to_int! :prazo_entrega
|
33
|
+
cast_to_boolean! :entrega_domiciliar, :entrega_sabado
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def success?
|
38
|
+
erro == "0"
|
39
|
+
end
|
40
|
+
|
41
|
+
def error?
|
42
|
+
!success?
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def cast_to_float!(*attributes)
|
48
|
+
attributes.each do |attr|
|
49
|
+
instance_variable_set("@#{attr}", send(attr).to_s.gsub("," ,".").to_f)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def cast_to_int!(*attributes)
|
54
|
+
attributes.each do |attr|
|
55
|
+
instance_variable_set("@#{attr}", send(attr).to_i)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def cast_to_boolean!(*attributes)
|
60
|
+
attributes.each do |attr|
|
61
|
+
instance_variable_set("@#{attr}", send(attr) == "S")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
class Correios::Frete::WebService
|
6
|
+
URL = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx"
|
7
|
+
FORMATS = { :caixa_pacote => 1, :rolo_prisma => 2 }
|
8
|
+
CONDITIONS = { true => "S", false => "N" }
|
9
|
+
|
10
|
+
def request(frete, service_types)
|
11
|
+
Net::HTTP.get(URI.parse("#{URL}?#{params_for(frete, service_types)}"))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def params_for(frete, service_types)
|
17
|
+
"sCepOrigem=#{frete.cep_origem}&" +
|
18
|
+
"sCepDestino=#{frete.cep_destino}&" +
|
19
|
+
"nVlPeso=#{frete.peso}&" +
|
20
|
+
"nVlComprimento=#{frete.comprimento}&" +
|
21
|
+
"nVlAltura=#{frete.altura}&" +
|
22
|
+
"nVlLargura=#{frete.largura}&" +
|
23
|
+
"nVlDiametro=#{frete.diametro}&" +
|
24
|
+
"nCdFormato=#{FORMATS[frete.formato]}&" +
|
25
|
+
"sCdMaoPropria=#{CONDITIONS[frete.mao_propria]}&" +
|
26
|
+
"sCdAvisoRecebimento=#{CONDITIONS[frete.aviso_recebimento]}&" +
|
27
|
+
"nVlValorDeclarado=#{frete.valor_declarado}&" +
|
28
|
+
"nCdServico=#{service_codes_for(service_types)}&" +
|
29
|
+
"nCdEmpresa=&" +
|
30
|
+
"sDsSenha=&" +
|
31
|
+
"StrRetorno=xml"
|
32
|
+
end
|
33
|
+
|
34
|
+
def service_codes_for(service_types)
|
35
|
+
service_types.map { |type| Correios::Frete::Servico::TYPES[type] }.join(",")
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete::Parser do
|
5
|
+
describe "#servicos" do
|
6
|
+
before :each do
|
7
|
+
@xml = """<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>
|
8
|
+
<Servicos>
|
9
|
+
<cServico>
|
10
|
+
<Codigo>41106</Codigo>
|
11
|
+
<Valor>15,70</Valor>
|
12
|
+
<PrazoEntrega>3</PrazoEntrega>
|
13
|
+
<ValorMaoPropria>3,75</ValorMaoPropria>
|
14
|
+
<ValorAvisoRecebimento>1,99</ValorAvisoRecebimento>
|
15
|
+
<ValorValorDeclarado>1,50</ValorValorDeclarado>
|
16
|
+
<EntregaDomiciliar>S</EntregaDomiciliar>
|
17
|
+
<EntregaSabado>N</EntregaSabado>
|
18
|
+
<Erro>-3</Erro>
|
19
|
+
<MsgErro>Somente para teste</MsgErro>
|
20
|
+
</cServico>
|
21
|
+
<cServico>
|
22
|
+
<Codigo>40010</Codigo>
|
23
|
+
<Valor>17,80</Valor>
|
24
|
+
<PrazoEntrega>1</PrazoEntrega>
|
25
|
+
<ValorMaoPropria>3,70</ValorMaoPropria>
|
26
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
27
|
+
<ValorValorDeclarado>1,50</ValorValorDeclarado>
|
28
|
+
<EntregaDomiciliar>S</EntregaDomiciliar>
|
29
|
+
<EntregaSabado>S</EntregaSabado>
|
30
|
+
<Erro>0</Erro>
|
31
|
+
<MsgErro></MsgErro>
|
32
|
+
</cServico>
|
33
|
+
</Servicos>"""
|
34
|
+
@parser = Correios::Frete::Parser.new
|
35
|
+
end
|
36
|
+
|
37
|
+
{ :pac => { :tipo => :pac,
|
38
|
+
:codigo => "41106",
|
39
|
+
:valor => 15.70,
|
40
|
+
:prazo_entrega => 3,
|
41
|
+
:valor_mao_propria => 3.75,
|
42
|
+
:valor_aviso_recebimento => 1.99,
|
43
|
+
:valor_valor_declarado => 1.50,
|
44
|
+
:entrega_domiciliar => true,
|
45
|
+
:entrega_sabado => false,
|
46
|
+
:erro => "-3",
|
47
|
+
:msg_erro => "Somente para teste" },
|
48
|
+
:sedex => { :tipo => :sedex,
|
49
|
+
:codigo => "40010",
|
50
|
+
:valor => 17.8,
|
51
|
+
:prazo_entrega => 1,
|
52
|
+
:valor_mao_propria => 3.70,
|
53
|
+
:valor_aviso_recebimento => 0.0,
|
54
|
+
:valor_valor_declarado => 1.5,
|
55
|
+
:entrega_domiciliar => true,
|
56
|
+
:entrega_sabado => true,
|
57
|
+
:erro => "0",
|
58
|
+
:msg_erro => nil }
|
59
|
+
}.each do |service, attributes|
|
60
|
+
it "returns #{service} data" do
|
61
|
+
servicos = @parser.servicos @xml
|
62
|
+
|
63
|
+
attributes.each do |attr, value|
|
64
|
+
servicos[service].send(attr).should == value
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete::Servico do
|
5
|
+
before(:each) { @servico = Correios::Frete::Servico.new }
|
6
|
+
|
7
|
+
describe "#parse" do
|
8
|
+
context "when service exists" do
|
9
|
+
before :each do
|
10
|
+
@xml = """<cServico>
|
11
|
+
<Codigo>41106</Codigo>
|
12
|
+
<Valor>15,70</Valor>
|
13
|
+
<PrazoEntrega>3</PrazoEntrega>
|
14
|
+
<ValorMaoPropria>3,75</ValorMaoPropria>
|
15
|
+
<ValorAvisoRecebimento>1,99</ValorAvisoRecebimento>
|
16
|
+
<ValorValorDeclarado>1,50</ValorValorDeclarado>
|
17
|
+
<EntregaDomiciliar>S</EntregaDomiciliar>
|
18
|
+
<EntregaSabado>N</EntregaSabado>
|
19
|
+
<Erro>-3</Erro>
|
20
|
+
<MsgErro>Somente para teste</MsgErro>
|
21
|
+
</cServico>"""
|
22
|
+
end
|
23
|
+
|
24
|
+
{ :tipo => :pac,
|
25
|
+
:codigo => "41106",
|
26
|
+
:valor => 15.70,
|
27
|
+
:prazo_entrega => 3,
|
28
|
+
:valor_mao_propria => 3.75,
|
29
|
+
:valor_aviso_recebimento => 1.99,
|
30
|
+
:valor_valor_declarado => 1.50,
|
31
|
+
:entrega_domiciliar => true,
|
32
|
+
:entrega_sabado => false,
|
33
|
+
:erro => "-3",
|
34
|
+
:msg_erro => "Somente para teste"
|
35
|
+
}.each do |attr, value|
|
36
|
+
it "sets #{attr} to #{value}" do
|
37
|
+
@servico.parse @xml
|
38
|
+
@servico.send(attr).should == value
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context "when service does not exist" do
|
44
|
+
before :each do
|
45
|
+
@xml = """<cServico>
|
46
|
+
<Codigo>99999</Codigo>
|
47
|
+
<Valor>0,00</Valor>
|
48
|
+
<PrazoEntrega>0</PrazoEntrega>
|
49
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
50
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
51
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
52
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
53
|
+
<EntregaSabado></EntregaSabado>
|
54
|
+
<Erro>-1</Erro>
|
55
|
+
<MsgErro>Codigo de servico invalido</MsgErro>
|
56
|
+
</cServico>"""
|
57
|
+
end
|
58
|
+
|
59
|
+
{ :tipo => nil,
|
60
|
+
:codigo => "99999",
|
61
|
+
:valor => 0.0,
|
62
|
+
:prazo_entrega => 0,
|
63
|
+
:valor_mao_propria => 0.0,
|
64
|
+
:valor_aviso_recebimento => 0.0,
|
65
|
+
:valor_valor_declarado => 0.0,
|
66
|
+
:entrega_domiciliar => false,
|
67
|
+
:entrega_sabado => false,
|
68
|
+
:erro => "-1",
|
69
|
+
:msg_erro => "Codigo de servico invalido"
|
70
|
+
}.each do |attr, value|
|
71
|
+
it "sets #{attr} to #{value}" do
|
72
|
+
@servico.parse @xml
|
73
|
+
@servico.send(attr).should == value
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context "when there is an unexpect error" do
|
79
|
+
before :each do
|
80
|
+
@xml = """<cServico>
|
81
|
+
<Codigo></Codigo>
|
82
|
+
<Valor></Valor>
|
83
|
+
<PrazoEntrega></PrazoEntrega>
|
84
|
+
<ValorMaoPropria></ValorMaoPropria>
|
85
|
+
<ValorAvisoRecebimento></ValorAvisoRecebimento>
|
86
|
+
<ValorValorDeclarado></ValorValorDeclarado>
|
87
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
88
|
+
<EntregaSabado></EntregaSabado>
|
89
|
+
<Erro>99</Erro>
|
90
|
+
<MsgErro>Input string was not in a correct format.</MsgErro>
|
91
|
+
</cServico>"""
|
92
|
+
end
|
93
|
+
|
94
|
+
{ :tipo => nil,
|
95
|
+
:codigo => nil,
|
96
|
+
:valor => 0.0,
|
97
|
+
:prazo_entrega => 0,
|
98
|
+
:valor_mao_propria => 0.0,
|
99
|
+
:valor_aviso_recebimento => 0.0,
|
100
|
+
:valor_valor_declarado => 0.0,
|
101
|
+
:entrega_domiciliar => false,
|
102
|
+
:entrega_sabado => false,
|
103
|
+
:erro => "99",
|
104
|
+
:msg_erro => "Input string was not in a correct format."
|
105
|
+
}.each do |attr, value|
|
106
|
+
it "sets #{attr} to #{value}" do
|
107
|
+
@servico.parse @xml
|
108
|
+
@servico.send(attr).should == value
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#success?" do
|
115
|
+
context "when does not have error" do
|
116
|
+
it "returns true" do
|
117
|
+
@servico.parse "<cServico><Erro>0</Erro><cServico>"
|
118
|
+
@servico.success?.should be_true
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
context "when has error" do
|
123
|
+
it "returns false" do
|
124
|
+
@servico.parse "<cServico><Erro>7</Erro><cServico>"
|
125
|
+
@servico.success?.should be_false
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "#error?" do
|
131
|
+
context "when has error" do
|
132
|
+
it "returns true" do
|
133
|
+
@servico.parse "<cServico><Erro>7</Erro><cServico>"
|
134
|
+
@servico.error?.should be_true
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
context "when does not have error" do
|
139
|
+
it "returns false" do
|
140
|
+
@servico.parse "<cServico><Erro>0</Erro><cServico>"
|
141
|
+
@servico.error?.should be_false
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete::WebService do
|
5
|
+
describe "#request" do
|
6
|
+
before :each do
|
7
|
+
@frete = Correios::Frete.new :cep_origem => "01000-000",
|
8
|
+
:cep_destino => "021222-222",
|
9
|
+
:peso => 0.321,
|
10
|
+
:comprimento => 12.5,
|
11
|
+
:altura => 1.4,
|
12
|
+
:largura => 4.6,
|
13
|
+
:diametro => 5.0,
|
14
|
+
:formato => :rolo_prisma,
|
15
|
+
:mao_propria => true,
|
16
|
+
:aviso_recebimento => false,
|
17
|
+
:valor_declarado => 1.99
|
18
|
+
url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?" +
|
19
|
+
"sCepOrigem=01000-000&" +
|
20
|
+
"sCepDestino=021222-222&" +
|
21
|
+
"nVlPeso=0.321&" +
|
22
|
+
"nVlComprimento=12.5&" +
|
23
|
+
"nVlAltura=1.4&" +
|
24
|
+
"nVlLargura=4.6&" +
|
25
|
+
"nVlDiametro=5.0&" +
|
26
|
+
"nCdFormato=2&" +
|
27
|
+
"sCdMaoPropria=S&" +
|
28
|
+
"sCdAvisoRecebimento=N&" +
|
29
|
+
"nVlValorDeclarado=1.99&" +
|
30
|
+
"nCdServico=41106,40010&" +
|
31
|
+
"nCdEmpresa=&" +
|
32
|
+
"sDsSenha=&" +
|
33
|
+
"StrRetorno=xml"
|
34
|
+
Net::HTTP.stub(:get).with(URI.parse(url)).and_return("<xml><fake></fake>")
|
35
|
+
@web_service = Correios::Frete::WebService.new
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns XML response" do
|
39
|
+
@web_service.request(@frete, [:pac, :sedex]).should == "<xml><fake></fake>"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete do
|
5
|
+
describe ".new" do
|
6
|
+
context "create with default value of" do
|
7
|
+
before(:each) { @frete = Correios::Frete.new }
|
8
|
+
|
9
|
+
{ :peso => 0.0,
|
10
|
+
:comprimento => 0.0,
|
11
|
+
:altura => 0.0,
|
12
|
+
:largura => 0.0,
|
13
|
+
:diametro => 0.0,
|
14
|
+
:formato => :caixa_pacote,
|
15
|
+
:mao_propria => false,
|
16
|
+
:aviso_recebimento => false,
|
17
|
+
:valor_declarado => 0.0
|
18
|
+
}.each do |attr, value|
|
19
|
+
it attr do
|
20
|
+
@frete.send(attr).should == value
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
{ :cep_origem => "01000-000",
|
26
|
+
:cep_destino => "021222-222",
|
27
|
+
:peso => 0.321,
|
28
|
+
:comprimento => 12.5,
|
29
|
+
:altura => 1.4,
|
30
|
+
:largura => 4.6,
|
31
|
+
:diametro => 5.0,
|
32
|
+
:formato => :rolo_prisma,
|
33
|
+
:mao_propria => true,
|
34
|
+
:aviso_recebimento => true,
|
35
|
+
:valor_declarado => 1.99,
|
36
|
+
:web_service => Correios::Frete::WebService.new,
|
37
|
+
:parser => Correios::Frete::Parser.new
|
38
|
+
}.each do |attr, value|
|
39
|
+
context "when #{attr} is supplied" do
|
40
|
+
it "sets #{attr}" do
|
41
|
+
@frete = Correios::Frete.new(attr => value)
|
42
|
+
@frete.send(attr).should == value
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "when #{attr} is supplied in a block" do
|
47
|
+
it "sets #{attr}" do
|
48
|
+
@frete = Correios::Frete.new { |f| f.send("#{attr}=", value) }
|
49
|
+
@frete.send(attr).should == value
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "#calcular" do
|
56
|
+
before :each do
|
57
|
+
@web_service = Correios::Frete::WebService.new
|
58
|
+
@parser = Correios::Frete::Parser.new
|
59
|
+
@frete = Correios::Frete.new(:web_service => @web_service, :parser => @parser)
|
60
|
+
end
|
61
|
+
|
62
|
+
context "to many services" do
|
63
|
+
before :each do
|
64
|
+
@xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><Servicos><cServico><Codigo>41106</Codigo><Valor>15,70</Valor><PrazoEntrega>3</PrazoEntrega><ValorMaoPropria>3,70</ValorMaoPropria><ValorAvisoRecebimento>0,00</ValorAvisoRecebimento><ValorValorDeclarado>1,50</ValorValorDeclarado><EntregaDomiciliar>S</EntregaDomiciliar><EntregaSabado>N</EntregaSabado><Erro>0</Erro><MsgErro></MsgErro></cServico><cServico><Codigo>40010</Codigo><Valor>17,80</Valor><PrazoEntrega>1</PrazoEntrega><ValorMaoPropria>3,70</ValorMaoPropria><ValorAvisoRecebimento>0,00</ValorAvisoRecebimento><ValorValorDeclarado>1,50</ValorValorDeclarado><EntregaDomiciliar>S</EntregaDomiciliar><EntregaSabado>S</EntregaSabado><Erro>0</Erro><MsgErro></MsgErro></cServico></Servicos>'
|
65
|
+
@servicos = { :pac => Correios::Frete::Servico.new, :sedex => Correios::Frete::Servico.new }
|
66
|
+
|
67
|
+
@parser.stub(:servicos).and_return(@servicos)
|
68
|
+
@web_service.stub(:request).with(@frete, [:pac, :sedex]).and_return(@xml)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "returns all services" do
|
72
|
+
@frete.calcular(:pac, :sedex).should == @servicos
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
context "to one service" do
|
77
|
+
before :each do
|
78
|
+
@xml = '<?xml version="1.0" encoding="ISO-8859-1" ?><cServico><Codigo>40010</Codigo><Valor>17,80</Valor><PrazoEntrega>1</PrazoEntrega><ValorMaoPropria>3,70</ValorMaoPropria><ValorAvisoRecebimento>0,00</ValorAvisoRecebimento><ValorValorDeclarado>1,50</ValorValorDeclarado><EntregaDomiciliar>S</EntregaDomiciliar><EntregaSabado>S</EntregaSabado><Erro>0</Erro><MsgErro></MsgErro></cServico></Servicos>'
|
79
|
+
@servico = Correios::Frete::Servico.new
|
80
|
+
|
81
|
+
@parser.stub(:servicos).and_return(:sedex => @servico)
|
82
|
+
@web_service.stub(:request).with(@frete, [:sedex]).and_return(@xml)
|
83
|
+
end
|
84
|
+
|
85
|
+
it "returns only one service" do
|
86
|
+
@frete.calcular(:sedex).should == @servico
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'correios-frete'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: correios-frete
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Prodis a.k.a. Fernando Hamasaki de Amorim
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-07-02 00:00:00 -03:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: nokogiri
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sax-machine
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "0"
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: bundler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.0.0
|
57
|
+
type: :runtime
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: jeweler
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
type: :runtime
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
description: "C\xC3\xA1lculo de frete dos Correios."
|
72
|
+
email: prodis@gmail.com
|
73
|
+
executables: []
|
74
|
+
|
75
|
+
extensions: []
|
76
|
+
|
77
|
+
extra_rdoc_files:
|
78
|
+
- README.rdoc
|
79
|
+
files:
|
80
|
+
- .document
|
81
|
+
- .rspec
|
82
|
+
- Gemfile
|
83
|
+
- Gemfile.lock
|
84
|
+
- README.rdoc
|
85
|
+
- Rakefile
|
86
|
+
- correios-frete.gemspec
|
87
|
+
- lib/correios-frete.rb
|
88
|
+
- lib/correios/frete.rb
|
89
|
+
- lib/correios/frete/parser.rb
|
90
|
+
- lib/correios/frete/servico.rb
|
91
|
+
- lib/correios/frete/version.rb
|
92
|
+
- lib/correios/frete/web_service.rb
|
93
|
+
- spec/correios/frete/parser_spec.rb
|
94
|
+
- spec/correios/frete/servico_spec.rb
|
95
|
+
- spec/correios/frete/web_service_spec.rb
|
96
|
+
- spec/correios/frete_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
has_rdoc: true
|
99
|
+
homepage: http://github.com/prodis/correios-frete
|
100
|
+
licenses:
|
101
|
+
- MIT
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
|
105
|
+
require_paths:
|
106
|
+
- lib
|
107
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
108
|
+
none: false
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
hash: -633678989
|
113
|
+
segments:
|
114
|
+
- 0
|
115
|
+
version: "0"
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: "0"
|
122
|
+
requirements: []
|
123
|
+
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 1.6.1
|
126
|
+
signing_key:
|
127
|
+
specification_version: 3
|
128
|
+
summary: "C\xC3\xA1lculo de frete dos Correios."
|
129
|
+
test_files: []
|
130
|
+
|