correios-frete 1.4.0 → 1.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +4 -0
- data/README.rdoc +96 -12
- data/Rakefile +1 -1
- data/correios-frete.gemspec +8 -6
- data/lib/correios-frete.rb +2 -0
- data/lib/correios/frete/calculador.rb +10 -4
- data/lib/correios/frete/pacote.rb +67 -0
- data/lib/correios/frete/pacote_item.rb +27 -0
- data/lib/correios/frete/version.rb +1 -1
- data/lib/correios/frete/web_service.rb +9 -5
- data/spec/correios/frete/calculador_spec.rb +54 -10
- data/spec/correios/frete/pacote_item_spec.rb +47 -0
- data/spec/correios/frete/pacote_spec.rb +160 -0
- metadata +10 -5
- data/README_v0.3.0.rdoc +0 -168
data/CHANGELOG.rdoc
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
== Versão 1.5.0
|
2
|
+
- Novo: {Issue #3: Cálculo de frete para múltiplos itens}[https://github.com/prodis/correios-frete/issues/3] utilizando pacotes.
|
3
|
+
- Correção: Formatação de separador de casas decimais com vírgula na requisição do Web Service dos Correios.
|
4
|
+
|
1
5
|
== Versão 1.4.0
|
2
6
|
- Novo: Suporte para cálculo de e-SEDEX Prioritário e e-SEDEX Express.
|
3
7
|
|
data/README.rdoc
CHANGED
@@ -1,10 +1,5 @@
|
|
1
1
|
= correios-frete
|
2
2
|
|
3
|
-
---
|
4
|
-
Esta documentação é das <b>versões >= 1.0.0</b>.
|
5
|
-
Para a versão 0.3.0, por favor veja {README_v0.3.0.rdoc}[https://github.com/prodis/correios-frete/blob/master/README_v0.3.0.rdoc]
|
6
|
-
---
|
7
|
-
|
8
3
|
Cálculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).
|
9
4
|
|
10
5
|
Os serviços de frete suportados são <b>PAC</b>, <b>SEDEX</b>, <b>SEDEX a Cobrar</b> (necessário informar o valor declarado), <b>SEDEX 10</b>, <b>SEDEX Hoje</b> e <b>e-SEDEX</b>. Para os serviços com contrato é necessário informar código de empresa e senha.
|
@@ -78,7 +73,7 @@ Verificação de sucesso e erro:
|
|
78
73
|
servico = frete.calcular_sedex
|
79
74
|
servico.sucesso? # => false
|
80
75
|
servico.erro? # => true
|
81
|
-
servico.msg_erro # => "A altura nao pode ser maior que
|
76
|
+
servico.msg_erro # => "A altura nao pode ser maior que o comprimento."
|
82
77
|
|
83
78
|
Usando a interface pública em inglês:
|
84
79
|
|
@@ -88,13 +83,73 @@ Usando a interface pública em inglês:
|
|
88
83
|
servico.success? # => true
|
89
84
|
servico.error? # => false
|
90
85
|
|
86
|
+
|
87
|
+
=== Usando pacotes
|
88
|
+
|
89
|
+
Você pode "montar" um pacote com vários itens. Com o cálculo do volume dos itens adicionados, o pacote único será gerado em formato de cubo.
|
90
|
+
|
91
|
+
item1 = Correios::Frete::PacoteItem.new :peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2
|
92
|
+
item2 = Correios::Frete::PacoteItem.new :peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3
|
93
|
+
|
94
|
+
pacote = Correios::Frete::Pacote.new
|
95
|
+
pacote.adicionar_item(item1)
|
96
|
+
pacote.adicionar_item(item2)
|
97
|
+
|
98
|
+
pacote.peso # => 1.0
|
99
|
+
pacote.comprimento # => 18.32138799447962
|
100
|
+
pacote.largura # => 18.32138799447962
|
101
|
+
pacote.altura # => 18.32138799447962
|
102
|
+
pacote.volume # => 6150.0
|
103
|
+
pacote.formato # => :caixa_pacote
|
104
|
+
pacote.itens.size # => 2
|
105
|
+
|
106
|
+
Caso alguma dimensão do pacote montado seja menor que o tamanho mínimo exigido pelos Correios, o valor mínimo será atribuído à dimensão.
|
107
|
+
|
108
|
+
item1 = Correios::Frete::PacoteItem.new :peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2
|
109
|
+
item2 = Correios::Frete::PacoteItem.new :peso => 0.7, :comprimento => 40, :largura => 10, :altura => 3
|
110
|
+
|
111
|
+
pacote = Correios::Frete::Pacote.new
|
112
|
+
pacote.adicionar_item(item1)
|
113
|
+
pacote.adicionar_item(item2)
|
114
|
+
|
115
|
+
pacote.comprimento # => 16.0
|
116
|
+
pacote.largura # => 12.80579164987494
|
117
|
+
pacote.altura # => 12.80579164987494
|
118
|
+
|
119
|
+
Montado o pacote, basta passá-lo pelo parâmetro <b>encomenda</b> no construtor de Correios::Frete::Calculador.
|
120
|
+
|
121
|
+
frete = Correios::Frete::Calculador.new :cep_origem => "04094-050",
|
122
|
+
:cep_destino => "90619-900",
|
123
|
+
:encomenda => pacote
|
124
|
+
|
125
|
+
servicos = frete.calcular :sedex, :pac
|
126
|
+
|
127
|
+
servicos[:sedex].valor # => 29.2
|
128
|
+
servicos[:sedex].prazo_entrega # => 1
|
129
|
+
|
130
|
+
servicos[:pac].valor # => 13.3
|
131
|
+
servicos[:pac].prazo_entrega # => 5
|
132
|
+
|
133
|
+
<b>Observação:</b> Quando uma encomenda é fornecida ao calculador de frete, os parâmetros <b>peso</b>, <b>comprimento</b>, <b>largura</b>, <b>altura</b> e <b>formato</b> serão ignorados, sendo utilizados os valores da encomenda.
|
134
|
+
|
135
|
+
Usando a interface pública em inglês:
|
136
|
+
|
137
|
+
item1 = Correios::Frete::PacoteItem.new :peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2
|
138
|
+
item2 = Correios::Frete::PacoteItem.new :peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3
|
139
|
+
|
140
|
+
pacote = Correios::Frete::Pacote.new
|
141
|
+
pacote.add_item(item1)
|
142
|
+
pacote.add_item(item2)
|
143
|
+
pacote.items.size # => 2
|
144
|
+
|
145
|
+
|
91
146
|
== Log
|
92
147
|
|
93
148
|
Por padrão, cada chamada ao Web Service dos Correios é logada em STDOUT, com nível de log <b>:info</b>, usando a gem {LogMe}[http://github.com/prodis/log-me].
|
94
149
|
|
95
150
|
Exemplo de log:
|
96
151
|
I, [2011-10-01T00:26:16.864990 #5631] INFO -- : Correios-Frete Request:
|
97
|
-
http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?sCepOrigem=04094-050&sCepDestino=90619-900&nVlPeso=0.3&nVlComprimento=30&nVlAltura=2&nVlLargura=15&nVlDiametro=0.0&nCdFormato=1&sCdMaoPropria=N&sCdAvisoRecebimento=N&nVlValorDeclarado=0
|
152
|
+
http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?sCepOrigem=04094-050&sCepDestino=90619-900&nVlPeso=0.3&nVlComprimento=30&nVlAltura=2&nVlLargura=15&nVlDiametro=0.0&nCdFormato=1&sCdMaoPropria=N&sCdAvisoRecebimento=N&nVlValorDeclarado=0,00&nCdServico=41106&nCdEmpresa=&sDsSenha=&StrRetorno=xml
|
98
153
|
|
99
154
|
I, [2011-10-01T00:26:17.121822 #5631] INFO -- : Correios-Frete Response:
|
100
155
|
HTTP/1.1 200 OK
|
@@ -103,7 +158,7 @@ Exemplo de log:
|
|
103
158
|
|
104
159
|
Se você configurar o nível de log como <b>:debug</b>, serão logados também todos os cabeçalhos HTTP da resposta:
|
105
160
|
D, [2011-10-01T00:27:50.597961 #5631] DEBUG -- : Correios-Frete Request:
|
106
|
-
http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?sCepOrigem=04094-050&sCepDestino=90619-900&nVlPeso=0.3&nVlComprimento=30&nVlAltura=2&nVlLargura=15&nVlDiametro=0.0&nCdFormato=1&sCdMaoPropria=N&sCdAvisoRecebimento=N&nVlValorDeclarado=0
|
161
|
+
http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?sCepOrigem=04094-050&sCepDestino=90619-900&nVlPeso=0.3&nVlComprimento=30&nVlAltura=2&nVlLargura=15&nVlDiametro=0.0&nCdFormato=1&sCdMaoPropria=N&sCdAvisoRecebimento=N&nVlValorDeclarado=0,00&nCdServico=41106&nCdEmpresa=&sDsSenha=&StrRetorno=xml
|
107
162
|
|
108
163
|
D, [2011-10-01T00:27:50.812046 #5631] DEBUG -- : Correios-Frete Response:
|
109
164
|
HTTP/1.1 200 OK
|
@@ -170,21 +225,50 @@ Para desabilitar o log, mudar o nível do log ou configurar um outro mecanismo d
|
|
170
225
|
f.altura = 2
|
171
226
|
end
|
172
227
|
|
228
|
+
=== Maneiras de adicionar itens em Correios::Frete::Pacote
|
229
|
+
|
230
|
+
==== Com instâncias de Correios::Frete::PacoteItem
|
231
|
+
|
232
|
+
item1 = Correios::Frete::PacoteItem.new :peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2
|
233
|
+
item2 = Correios::Frete::PacoteItem.new :peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3
|
234
|
+
|
235
|
+
pacote = Correios::Frete::Pacote.new
|
236
|
+
pacote.adicionar_item(item1)
|
237
|
+
pacote.adicionar_item(item2)
|
238
|
+
|
239
|
+
==== Com parâmetros dos itens
|
240
|
+
pacote = Correios::Frete::Pacote.new
|
241
|
+
pacote.adicionar_item(:peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2)
|
242
|
+
pacote.adicionar_item(:peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3)
|
243
|
+
|
173
244
|
|
174
245
|
=== Atributos de Correios::Frete::Calculador
|
175
246
|
|
176
247
|
==== String
|
177
248
|
cep_origem, cep_destino, codigo_empresa, senha
|
178
249
|
==== Float
|
179
|
-
peso, valor_declarado
|
180
|
-
==== Fixnum
|
181
|
-
comprimento, largura, altura, diametro
|
250
|
+
peso, comprimento, largura, altura, diametro, valor_declarado
|
182
251
|
==== Boolean
|
183
252
|
mao_propria, aviso_recebimento
|
184
253
|
==== Symbol
|
185
254
|
formato (:caixa_pacote, :rolo_prisma)
|
186
255
|
|
187
256
|
|
257
|
+
=== Atributos de Correios::Frete::Pacote
|
258
|
+
|
259
|
+
==== Float
|
260
|
+
peso, comprimento, largura, altura, volume
|
261
|
+
==== Array de Correios::Frete::PacoteItem
|
262
|
+
itens
|
263
|
+
==== Symbol
|
264
|
+
formato (:caixa_pacote)
|
265
|
+
|
266
|
+
=== Atributos de Correios::Frete::PacoteItem
|
267
|
+
|
268
|
+
==== Float
|
269
|
+
peso, comprimento, largura, altura, volume
|
270
|
+
|
271
|
+
|
188
272
|
=== Atributos de Correios::Frete::Servico
|
189
273
|
|
190
274
|
==== String
|
@@ -214,7 +298,7 @@ Para desabilitar o log, mudar o nível do log ou configurar um outro mecanismo d
|
|
214
298
|
|
215
299
|
{Prodis a.k.a. Fernando Hamasaki}[http://prodis.blog.br]
|
216
300
|
|
217
|
-
Copyright (c) 2011 Prodis
|
301
|
+
Copyright (c) 2011-2012 Prodis
|
218
302
|
|
219
303
|
Permission is hereby granted, free of charge, to any person obtaining
|
220
304
|
a copy of this software and associated documentation files (the
|
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.homepage = "http://prodis.blog.br/2011/07/03/gem-para-calculo-de-frete-dos-correios"
|
19
19
|
gem.license = "MIT"
|
20
20
|
gem.summary = %Q{Calculo de frete dos Correios.}
|
21
|
-
gem.description = %Q{Calculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices)
|
21
|
+
gem.description = %Q{Calculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).\n\nOs servicos de frete suportados sao PAC, SEDEX, SEDEX a Cobrar, SEDEX 10, SEDEX Hoje e e-SEDEX.}
|
22
22
|
gem.email = "prodis@gmail.com"
|
23
23
|
gem.authors = ["Prodis a.k.a. Fernando Hamasaki"]
|
24
24
|
gem.version = Correios::Frete::Version::VERSION
|
data/correios-frete.gemspec
CHANGED
@@ -5,16 +5,15 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "correios-frete"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.5.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Prodis a.k.a. Fernando Hamasaki"]
|
12
|
-
s.date = "2012-01-
|
13
|
-
s.description = "Calculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices)
|
12
|
+
s.date = "2012-01-25"
|
13
|
+
s.description = "Calculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).\n\nOs servicos de frete suportados sao PAC, SEDEX, SEDEX a Cobrar, SEDEX 10, SEDEX Hoje e e-SEDEX."
|
14
14
|
s.email = "prodis@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
|
-
"README.rdoc"
|
17
|
-
"README_v0.3.0.rdoc"
|
16
|
+
"README.rdoc"
|
18
17
|
]
|
19
18
|
s.files = [
|
20
19
|
".document",
|
@@ -23,17 +22,20 @@ Gem::Specification.new do |s|
|
|
23
22
|
"Gemfile",
|
24
23
|
"Gemfile.lock",
|
25
24
|
"README.rdoc",
|
26
|
-
"README_v0.3.0.rdoc",
|
27
25
|
"Rakefile",
|
28
26
|
"correios-frete.gemspec",
|
29
27
|
"lib/correios-frete.rb",
|
30
28
|
"lib/correios/frete.rb",
|
31
29
|
"lib/correios/frete/calculador.rb",
|
30
|
+
"lib/correios/frete/pacote.rb",
|
31
|
+
"lib/correios/frete/pacote_item.rb",
|
32
32
|
"lib/correios/frete/parser.rb",
|
33
33
|
"lib/correios/frete/servico.rb",
|
34
34
|
"lib/correios/frete/version.rb",
|
35
35
|
"lib/correios/frete/web_service.rb",
|
36
36
|
"spec/correios/frete/calculador_spec.rb",
|
37
|
+
"spec/correios/frete/pacote_item_spec.rb",
|
38
|
+
"spec/correios/frete/pacote_spec.rb",
|
37
39
|
"spec/correios/frete/parser_spec.rb",
|
38
40
|
"spec/correios/frete/servico_spec.rb",
|
39
41
|
"spec/correios/frete/web_service_spec.rb",
|
data/lib/correios-frete.rb
CHANGED
@@ -3,15 +3,15 @@ module Correios
|
|
3
3
|
module Frete
|
4
4
|
class Calculador
|
5
5
|
attr_accessor :cep_origem, :cep_destino
|
6
|
-
attr_accessor :
|
7
|
-
attr_accessor :
|
8
|
-
|
6
|
+
attr_accessor :diametro, :mao_propria, :aviso_recebimento, :valor_declarado
|
7
|
+
attr_accessor :codigo_empresa, :senha, :encomenda
|
8
|
+
attr_writer :peso, :comprimento, :largura, :altura, :formato
|
9
9
|
|
10
10
|
DEFAULT_OPTIONS = {
|
11
11
|
:peso => 0.0,
|
12
12
|
:comprimento => 0.0,
|
13
|
-
:altura => 0.0,
|
14
13
|
:largura => 0.0,
|
14
|
+
:altura => 0.0,
|
15
15
|
:diametro => 0.0,
|
16
16
|
:formato => :caixa_pacote,
|
17
17
|
:mao_propria => false,
|
@@ -27,6 +27,12 @@ module Correios
|
|
27
27
|
yield self if block_given?
|
28
28
|
end
|
29
29
|
|
30
|
+
[:peso, :comprimento, :largura, :altura, :formato].each do |method|
|
31
|
+
define_method method do
|
32
|
+
@encomenda ? @encomenda.send(method) : instance_variable_get("@#{method}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
30
36
|
def calcular(*service_types)
|
31
37
|
response = web_service(service_types).request!
|
32
38
|
services = parser.servicos(response)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Correios
|
3
|
+
module Frete
|
4
|
+
class Pacote
|
5
|
+
attr_reader :peso, :comprimento, :altura, :largura, :volume
|
6
|
+
|
7
|
+
MIN_DIMENSIONS = {
|
8
|
+
:comprimento => 16.0,
|
9
|
+
:largura => 11.0,
|
10
|
+
:altura => 2.0
|
11
|
+
}
|
12
|
+
|
13
|
+
def initialize()
|
14
|
+
@peso = @comprimento = @largura = @altura = @volume = 0.0
|
15
|
+
@itens = []
|
16
|
+
end
|
17
|
+
|
18
|
+
def formato
|
19
|
+
:caixa_pacote
|
20
|
+
end
|
21
|
+
|
22
|
+
def itens
|
23
|
+
@itens
|
24
|
+
end
|
25
|
+
alias items itens
|
26
|
+
|
27
|
+
def adicionar_item(item)
|
28
|
+
return unless item
|
29
|
+
|
30
|
+
item = Correios::Frete::PacoteItem.new(item) if item.is_a?(Hash)
|
31
|
+
@itens << item
|
32
|
+
|
33
|
+
calcular_medidas(item)
|
34
|
+
item
|
35
|
+
end
|
36
|
+
alias add_item adicionar_item
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def calcular_medidas(item)
|
41
|
+
@peso += item.peso
|
42
|
+
@volume += item.volume
|
43
|
+
|
44
|
+
if @itens.size == 1
|
45
|
+
@comprimento = item.comprimento
|
46
|
+
@largura = item.largura
|
47
|
+
@altura = item.altura
|
48
|
+
else
|
49
|
+
dimensao = @volume.to_f**(1.0/3)
|
50
|
+
@comprimento = @largura = @altura = dimensao
|
51
|
+
end
|
52
|
+
|
53
|
+
min_dimension_values
|
54
|
+
end
|
55
|
+
|
56
|
+
def min_dimension_values()
|
57
|
+
@comprimento = min(@comprimento, MIN_DIMENSIONS[:comprimento])
|
58
|
+
@largura = min(@largura, MIN_DIMENSIONS[:largura])
|
59
|
+
@altura = min(@altura, MIN_DIMENSIONS[:altura])
|
60
|
+
end
|
61
|
+
|
62
|
+
def min(value, minimum)
|
63
|
+
(value < minimum) ? minimum : value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module Correios
|
3
|
+
module Frete
|
4
|
+
class PacoteItem
|
5
|
+
attr_accessor :peso, :comprimento, :largura, :altura
|
6
|
+
|
7
|
+
DEFAULT_OPTIONS = {
|
8
|
+
:peso => 0.0,
|
9
|
+
:comprimento => 0.0,
|
10
|
+
:largura => 0.0,
|
11
|
+
:altura => 0.0
|
12
|
+
}
|
13
|
+
|
14
|
+
def initialize(options = {})
|
15
|
+
DEFAULT_OPTIONS.merge(options).each do |attr, value|
|
16
|
+
self.send("#{attr}=", value)
|
17
|
+
end
|
18
|
+
|
19
|
+
yield self if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
def volume
|
23
|
+
@comprimento * @largura * @altura
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -24,20 +24,24 @@ module Correios
|
|
24
24
|
"sCepOrigem=#{frete.cep_origem}&" +
|
25
25
|
"sCepDestino=#{frete.cep_destino}&" +
|
26
26
|
"nVlPeso=#{frete.peso}&" +
|
27
|
-
"nVlComprimento=#{frete.comprimento}&" +
|
28
|
-
"
|
29
|
-
"
|
30
|
-
"nVlDiametro=#{frete.diametro}&" +
|
27
|
+
"nVlComprimento=#{format_decimal(frete.comprimento)}&" +
|
28
|
+
"nVlLargura=#{format_decimal(frete.largura)}&" +
|
29
|
+
"nVlAltura=#{format_decimal(frete.altura)}&" +
|
30
|
+
"nVlDiametro=#{format_decimal(frete.diametro)}&" +
|
31
31
|
"nCdFormato=#{FORMATS[frete.formato]}&" +
|
32
32
|
"sCdMaoPropria=#{CONDITIONS[frete.mao_propria]}&" +
|
33
33
|
"sCdAvisoRecebimento=#{CONDITIONS[frete.aviso_recebimento]}&" +
|
34
|
-
"nVlValorDeclarado=#{format("%.2f" % frete.valor_declarado)}&" +
|
34
|
+
"nVlValorDeclarado=#{format_decimal(format("%.2f" % frete.valor_declarado))}&" +
|
35
35
|
"nCdServico=#{service_codes_for(service_types)}&" +
|
36
36
|
"nCdEmpresa=#{frete.codigo_empresa}&" +
|
37
37
|
"sDsSenha=#{frete.senha}&" +
|
38
38
|
"StrRetorno=xml"
|
39
39
|
end
|
40
40
|
|
41
|
+
def format_decimal(value)
|
42
|
+
value.to_s.gsub(".", ",")
|
43
|
+
end
|
44
|
+
|
41
45
|
def service_codes_for(service_types)
|
42
46
|
service_types.map { |type| Correios::Frete::Servico.code_from_type(type) }.join(",")
|
43
47
|
end
|
@@ -8,8 +8,8 @@ describe Correios::Frete::Calculador do
|
|
8
8
|
|
9
9
|
{ :peso => 0.0,
|
10
10
|
:comprimento => 0.0,
|
11
|
-
:altura => 0.0,
|
12
11
|
:largura => 0.0,
|
12
|
+
:altura => 0.0,
|
13
13
|
:diametro => 0.0,
|
14
14
|
:formato => :caixa_pacote,
|
15
15
|
:mao_propria => false,
|
@@ -25,27 +25,71 @@ describe Correios::Frete::Calculador do
|
|
25
25
|
{ :cep_origem => "01000-000",
|
26
26
|
:cep_destino => "021222-222",
|
27
27
|
:peso => 0.321,
|
28
|
-
:comprimento =>
|
29
|
-
:
|
30
|
-
:
|
31
|
-
:diametro => 5
|
28
|
+
:comprimento => 30,
|
29
|
+
:largura => 15,
|
30
|
+
:altura => 2,
|
31
|
+
:diametro => 5,
|
32
32
|
:formato => :rolo_prisma,
|
33
33
|
:mao_propria => true,
|
34
34
|
:aviso_recebimento => true,
|
35
35
|
:valor_declarado => 1.99,
|
36
36
|
:codigo_empresa => "1234567890",
|
37
37
|
:senha => "senha",
|
38
|
+
:encomenda => Correios::Frete::Pacote.new
|
38
39
|
}.each do |attr, value|
|
39
40
|
context "when #{attr} is supplied" do
|
40
41
|
it "sets #{attr}" do
|
41
|
-
|
42
|
-
|
42
|
+
frete = Correios::Frete::Calculador.new(attr => value)
|
43
|
+
frete.send(attr).should == value
|
43
44
|
end
|
44
45
|
end
|
45
46
|
|
46
47
|
context "when #{attr} is supplied in a block" do
|
47
48
|
it "sets #{attr}" do
|
48
|
-
|
49
|
+
frete = Correios::Frete::Calculador.new { |f| f.send("#{attr}=", value) }
|
50
|
+
frete.send(attr).should == value
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "#encomenda" do
|
57
|
+
context "when encomenda is supplied" do
|
58
|
+
before :each do
|
59
|
+
@encomenda = Correios::Frete::Pacote.new
|
60
|
+
@encomenda.adicionar_item(:peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2)
|
61
|
+
@encomenda.adicionar_item(:peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3)
|
62
|
+
@frete = Correios::Frete::Calculador.new :peso => 10.5,
|
63
|
+
:comprimento => 105,
|
64
|
+
:largura => 50,
|
65
|
+
:altura => 10,
|
66
|
+
:formato => :rolo_prisma,
|
67
|
+
:encomenda => @encomenda
|
68
|
+
end
|
69
|
+
|
70
|
+
[:peso, :comprimento, :largura, :altura, :formato].each do |attr|
|
71
|
+
it "#{attr} returns encomenda #{attr}" do
|
72
|
+
@frete.send(attr).should == @encomenda.send(attr)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "when encomenda is not supplied" do
|
78
|
+
before :each do
|
79
|
+
@frete = Correios::Frete::Calculador.new :peso => 10.5,
|
80
|
+
:comprimento => 105,
|
81
|
+
:largura => 50,
|
82
|
+
:altura => 10,
|
83
|
+
:formato => :rolo_prisma
|
84
|
+
end
|
85
|
+
|
86
|
+
{ :peso => 10.5,
|
87
|
+
:comprimento => 105,
|
88
|
+
:largura => 50,
|
89
|
+
:altura => 10,
|
90
|
+
:formato => :rolo_prisma
|
91
|
+
}.each do |attr, value|
|
92
|
+
it "#{attr} returns supplied #{attr}" do
|
49
93
|
@frete.send(attr).should == value
|
50
94
|
end
|
51
95
|
end
|
@@ -112,7 +156,7 @@ describe Correios::Frete::Calculador do
|
|
112
156
|
end
|
113
157
|
|
114
158
|
it "returns true in respond_to?" do
|
115
|
-
|
159
|
+
@frete.respond_to?("#{method_name}_#{service[:type]}").should be_true
|
116
160
|
end
|
117
161
|
end
|
118
162
|
end
|
@@ -125,7 +169,7 @@ describe Correios::Frete::Calculador do
|
|
125
169
|
end
|
126
170
|
|
127
171
|
it "returns false in respond_to?" do
|
128
|
-
|
172
|
+
@frete.respond_to?("#{method_name}_servico_que_nao_existe").should be_false
|
129
173
|
end
|
130
174
|
end
|
131
175
|
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete::PacoteItem do
|
5
|
+
describe ".new" do
|
6
|
+
context "create with default value of" do
|
7
|
+
before(:each) { @item = Correios::Frete::PacoteItem.new }
|
8
|
+
|
9
|
+
{ :peso => 0.0,
|
10
|
+
:comprimento => 0.0,
|
11
|
+
:largura => 0.0,
|
12
|
+
:altura => 0.0
|
13
|
+
}.each do |attr, value|
|
14
|
+
it attr do
|
15
|
+
@item.send(attr).should == value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
{ :peso => 0.3,
|
21
|
+
:comprimento => 30,
|
22
|
+
:largura => 15,
|
23
|
+
:altura => 2,
|
24
|
+
}.each do |attr, value|
|
25
|
+
context "when #{attr} is supplied" do
|
26
|
+
it "sets #{attr}" do
|
27
|
+
item = Correios::Frete::PacoteItem.new(attr => value)
|
28
|
+
item.send(attr).should == value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
context "when #{attr} is supplied in a block" do
|
33
|
+
it "sets #{attr}" do
|
34
|
+
item = Correios::Frete::PacoteItem.new { |f| f.send("#{attr}=", value) }
|
35
|
+
item.send(attr).should == value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "#volume" do
|
42
|
+
it "calculates item volume" do
|
43
|
+
item = Correios::Frete::PacoteItem.new(:comprimento => 16, :largura => 11, :altura => 2)
|
44
|
+
item.volume.should == 16 * 11 * 2
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Correios::Frete::Pacote do
|
5
|
+
before(:each) { @pacote = Correios::Frete::Pacote.new }
|
6
|
+
|
7
|
+
describe ".new" do
|
8
|
+
context "create with default value of" do
|
9
|
+
{ :peso => 0.0,
|
10
|
+
:comprimento => 0.0,
|
11
|
+
:altura => 0.0,
|
12
|
+
:largura => 0.0
|
13
|
+
}.each do |attr, value|
|
14
|
+
it attr do
|
15
|
+
@pacote.send(attr).should == value
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "itens" do
|
20
|
+
@pacote.itens.should be_empty
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#formato" do
|
26
|
+
it "is caixa/pacote" do
|
27
|
+
@pacote.formato.should == :caixa_pacote
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#adicionar_item" do
|
32
|
+
context "when adds a package item" do
|
33
|
+
it "adds in items" do
|
34
|
+
item = Correios::Frete::PacoteItem.new
|
35
|
+
@pacote.adicionar_item(item)
|
36
|
+
@pacote.itens.first.should == item
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when adds package item params" do
|
41
|
+
it "adds new item" do
|
42
|
+
params = { :peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2 }
|
43
|
+
@pacote.adicionar_item(params)
|
44
|
+
|
45
|
+
@pacote.itens.first.peso.should == params[:peso]
|
46
|
+
@pacote.itens.first.comprimento.should == params[:comprimento]
|
47
|
+
@pacote.itens.first.altura.should == params[:altura]
|
48
|
+
@pacote.itens.first.largura.should == params[:largura]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context "when adds nil item" do
|
53
|
+
it "does not add" do
|
54
|
+
@pacote.adicionar_item(nil)
|
55
|
+
@pacote.itens.should be_empty
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "calculations" do
|
61
|
+
context "when adds one package item" do
|
62
|
+
before :each do
|
63
|
+
@item = Correios::Frete::PacoteItem.new(:peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2)
|
64
|
+
@pacote.adicionar_item(@item)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "calculates package weight" do
|
68
|
+
@pacote.peso.should == @item.peso
|
69
|
+
end
|
70
|
+
|
71
|
+
it "calculates package volume" do
|
72
|
+
@pacote.volume.should == @item.volume
|
73
|
+
end
|
74
|
+
|
75
|
+
it "calculates package length" do
|
76
|
+
@pacote.comprimento.should == @item.comprimento
|
77
|
+
end
|
78
|
+
|
79
|
+
it "calculates package width" do
|
80
|
+
@pacote.largura.should == @item.largura
|
81
|
+
end
|
82
|
+
|
83
|
+
it "calculates package height" do
|
84
|
+
@pacote.altura.should == @item.altura
|
85
|
+
end
|
86
|
+
|
87
|
+
context "with dimensions less than each minimum" do
|
88
|
+
before :each do
|
89
|
+
item = Correios::Frete::PacoteItem.new(:peso => 0.3, :comprimento => 15, :largura => 10, :altura => 1)
|
90
|
+
@pacote = Correios::Frete::Pacote.new
|
91
|
+
@pacote.adicionar_item(item)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "sets minimum length value" do
|
95
|
+
@pacote.comprimento.should == 16
|
96
|
+
end
|
97
|
+
|
98
|
+
it "sets minimum width value" do
|
99
|
+
@pacote.largura.should == 11
|
100
|
+
end
|
101
|
+
|
102
|
+
it "sets minimum height value" do
|
103
|
+
@pacote.altura.should == 2
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
context "when adds more than one package item" do
|
109
|
+
before :each do
|
110
|
+
@item1 = Correios::Frete::PacoteItem.new(:peso => 0.3, :comprimento => 30, :largura => 15, :altura => 2)
|
111
|
+
@item2 = Correios::Frete::PacoteItem.new(:peso => 0.7, :comprimento => 70, :largura => 25, :altura => 3)
|
112
|
+
@expected_dimension = (@item1.volume + @item2.volume).to_f**(1.0/3)
|
113
|
+
|
114
|
+
@pacote.adicionar_item(@item1)
|
115
|
+
@pacote.adicionar_item(@item2)
|
116
|
+
end
|
117
|
+
|
118
|
+
it "calculates package weight" do
|
119
|
+
@pacote.peso.should == @item1.peso + @item2.peso
|
120
|
+
end
|
121
|
+
|
122
|
+
it "calculates package volume" do
|
123
|
+
@pacote.volume.should == @item1.volume + @item2.volume
|
124
|
+
end
|
125
|
+
|
126
|
+
it "calculates package length" do
|
127
|
+
@pacote.comprimento.should == @expected_dimension
|
128
|
+
end
|
129
|
+
|
130
|
+
it "calculates package width" do
|
131
|
+
@pacote.largura.should == @expected_dimension
|
132
|
+
end
|
133
|
+
|
134
|
+
it "calculates package height" do
|
135
|
+
@pacote.altura.should == @expected_dimension
|
136
|
+
end
|
137
|
+
|
138
|
+
context "with dimensions less than each minimum" do
|
139
|
+
before :each do
|
140
|
+
item = Correios::Frete::PacoteItem.new(:peso => 0.3, :comprimento => 3, :largura => 1, :altura => 1)
|
141
|
+
@pacote = Correios::Frete::Pacote.new
|
142
|
+
@pacote.adicionar_item(item)
|
143
|
+
@pacote.adicionar_item(item)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "sets minimum length value" do
|
147
|
+
@pacote.comprimento.should == 16
|
148
|
+
end
|
149
|
+
|
150
|
+
it "sets minimum width value" do
|
151
|
+
@pacote.largura.should == 11
|
152
|
+
end
|
153
|
+
|
154
|
+
it "sets minimum height value" do
|
155
|
+
@pacote.altura.should == 2
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: correios-frete
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.
|
5
|
+
version: 1.5.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Prodis a.k.a. Fernando Hamasaki
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-01-
|
13
|
+
date: 2012-01-25 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: log-me
|
@@ -111,7 +111,10 @@ dependencies:
|
|
111
111
|
type: :development
|
112
112
|
prerelease: false
|
113
113
|
version_requirements: *id009
|
114
|
-
description:
|
114
|
+
description: |-
|
115
|
+
Calculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).
|
116
|
+
|
117
|
+
Os servicos de frete suportados sao PAC, SEDEX, SEDEX a Cobrar, SEDEX 10, SEDEX Hoje e e-SEDEX.
|
115
118
|
email: prodis@gmail.com
|
116
119
|
executables: []
|
117
120
|
|
@@ -119,7 +122,6 @@ extensions: []
|
|
119
122
|
|
120
123
|
extra_rdoc_files:
|
121
124
|
- README.rdoc
|
122
|
-
- README_v0.3.0.rdoc
|
123
125
|
files:
|
124
126
|
- .document
|
125
127
|
- .rspec
|
@@ -127,17 +129,20 @@ files:
|
|
127
129
|
- Gemfile
|
128
130
|
- Gemfile.lock
|
129
131
|
- README.rdoc
|
130
|
-
- README_v0.3.0.rdoc
|
131
132
|
- Rakefile
|
132
133
|
- correios-frete.gemspec
|
133
134
|
- lib/correios-frete.rb
|
134
135
|
- lib/correios/frete.rb
|
135
136
|
- lib/correios/frete/calculador.rb
|
137
|
+
- lib/correios/frete/pacote.rb
|
138
|
+
- lib/correios/frete/pacote_item.rb
|
136
139
|
- lib/correios/frete/parser.rb
|
137
140
|
- lib/correios/frete/servico.rb
|
138
141
|
- lib/correios/frete/version.rb
|
139
142
|
- lib/correios/frete/web_service.rb
|
140
143
|
- spec/correios/frete/calculador_spec.rb
|
144
|
+
- spec/correios/frete/pacote_item_spec.rb
|
145
|
+
- spec/correios/frete/pacote_spec.rb
|
141
146
|
- spec/correios/frete/parser_spec.rb
|
142
147
|
- spec/correios/frete/servico_spec.rb
|
143
148
|
- spec/correios/frete/web_service_spec.rb
|
data/README_v0.3.0.rdoc
DELETED
@@ -1,168 +0,0 @@
|
|
1
|
-
= correios-frete
|
2
|
-
|
3
|
-
---
|
4
|
-
Esta documentação é da <b>versão 0.3.0</b>.
|
5
|
-
Para a versão 1.0.0 ou acima, por favor veja {README.rdoc}[https://github.com/prodis/correios-frete/blob/master/README.rdoc]
|
6
|
-
---
|
7
|
-
|
8
|
-
Cálculo de frete utilizando o Web Service dos Correios (http://www.correios.com.br/webservices).
|
9
|
-
|
10
|
-
Os serviços de frete suportados são <b>PAC</b>, <b>SEDEX</b>, <b>SEDEX 10</b>, <b>SEDEX Hoje</b> e <b>e-SEDEX</b> (necessário informar código de empresa e senha).
|
11
|
-
|
12
|
-
== Instalando
|
13
|
-
|
14
|
-
=== Gemfile
|
15
|
-
gem 'correios-frete'
|
16
|
-
|
17
|
-
=== Instalação direta
|
18
|
-
$ gem install correios-frete
|
19
|
-
|
20
|
-
|
21
|
-
== Usando
|
22
|
-
|
23
|
-
require 'correios-frete'
|
24
|
-
|
25
|
-
frete = Correios::Frete.new :cep_origem => "04094-050",
|
26
|
-
:cep_destino => "90619-900",
|
27
|
-
:peso => 0.3,
|
28
|
-
:comprimento => 30,
|
29
|
-
:largura => 15,
|
30
|
-
:altura => 2
|
31
|
-
|
32
|
-
Cálculo de vários serviços ao mesmo tempo:
|
33
|
-
|
34
|
-
servicos = frete.calcular :sedex, :pac
|
35
|
-
|
36
|
-
servicos[:sedex].nome # => "SEDEX"
|
37
|
-
servicos[:sedex].valor # => 26.2
|
38
|
-
servicos[:sedex].prazo_entrega # => 1
|
39
|
-
|
40
|
-
servicos[:pac].nome # => "PAC"
|
41
|
-
servicos[:pac].valor # => 10.0
|
42
|
-
servicos[:pac].prazo_entrega # => 5
|
43
|
-
|
44
|
-
Cálculo de um serviço de frete passando o serviço para parâmetro:
|
45
|
-
|
46
|
-
servico = frete.calcular :sedex
|
47
|
-
service.nome # => "SEDEX"
|
48
|
-
servico.valor # => 26.2
|
49
|
-
servico.prazo_entrega # => 1
|
50
|
-
|
51
|
-
servico = frete.calcular :pac
|
52
|
-
service.nome # => "PAC"
|
53
|
-
servico.valor # => 10.0
|
54
|
-
servico.prazo_entrega # => 5
|
55
|
-
|
56
|
-
Cálculo de um serviço de frete chamando o método direto do serviço:
|
57
|
-
|
58
|
-
servico = frete.calcular_sedex
|
59
|
-
service.nome # => "SEDEX"
|
60
|
-
servico.valor # => 26.2
|
61
|
-
servico.prazo_entrega # => 1
|
62
|
-
|
63
|
-
servico = frete.calcular_pac
|
64
|
-
service.nome # => "PAC"
|
65
|
-
servico.valor # => 10.0
|
66
|
-
servico.prazo_entrega # => 5
|
67
|
-
|
68
|
-
Verificação de sucesso e erro:
|
69
|
-
|
70
|
-
frete.altura = 100
|
71
|
-
|
72
|
-
servico = frete.calcular_sedex
|
73
|
-
servico.sucesso? # => false
|
74
|
-
servico.erro? # => true
|
75
|
-
servico.msg_erro # => "A altura nao pode ser maior que 90 cm."
|
76
|
-
|
77
|
-
Usando a interface pública em inglês:
|
78
|
-
|
79
|
-
servicos = frete.calculate :sedex, :pac
|
80
|
-
servico = frete.calculate_sedex
|
81
|
-
servico.success? # => true
|
82
|
-
servico.error? # => false
|
83
|
-
|
84
|
-
== Informações adicionais
|
85
|
-
|
86
|
-
=== Serviços suportados
|
87
|
-
:pac
|
88
|
-
:sedex
|
89
|
-
:sedex_10
|
90
|
-
:sedex_hoje
|
91
|
-
:e_sedex
|
92
|
-
|
93
|
-
|
94
|
-
=== Maneiras de configurar atributos no construtor de Correios::Frete
|
95
|
-
|
96
|
-
==== Com um hash
|
97
|
-
frete = Correios::Frete.new :cep_origem => "04094-050",
|
98
|
-
:cep_destino => "90619-900",
|
99
|
-
:peso => 0.3,
|
100
|
-
:comprimento => 30,
|
101
|
-
:largura => 15,
|
102
|
-
:altura => 2
|
103
|
-
|
104
|
-
==== Com um bloco
|
105
|
-
frete = Correios::Frete.new do |f|
|
106
|
-
f.cep_origem = "04094-050"
|
107
|
-
f.cep_destino = "90619-900"
|
108
|
-
f.peso = 0.3
|
109
|
-
f.comprimento = 30
|
110
|
-
f.largura = 15
|
111
|
-
f.altura = 2
|
112
|
-
end
|
113
|
-
|
114
|
-
|
115
|
-
=== Atributos de Correios::Frete
|
116
|
-
|
117
|
-
==== String
|
118
|
-
cep_origem, cep_destino, codigo_empresa, senha
|
119
|
-
==== Float
|
120
|
-
peso, valor_declarado
|
121
|
-
==== Fixnum
|
122
|
-
comprimento, largura, altura, diametro
|
123
|
-
==== Boolean
|
124
|
-
mao_propria, aviso_recebimento
|
125
|
-
==== Symbol
|
126
|
-
formato (:caixa_pacote, :rolo_prisma)
|
127
|
-
|
128
|
-
|
129
|
-
=== Atributos de Correios::Frete::Servico
|
130
|
-
|
131
|
-
==== String
|
132
|
-
codigo, erro, msg_erro, nome
|
133
|
-
==== Float
|
134
|
-
valor, valor_mao_propria, valor_aviso_recebimento, valor_valor_declarado
|
135
|
-
==== Fixnum
|
136
|
-
prazo_entrega
|
137
|
-
==== Boolean
|
138
|
-
entrega_domiciliar, entrega_sabado
|
139
|
-
==== Symbol
|
140
|
-
tipo (:pac, :sedex, :sedex_10, :sedex_hoje, :e_sedex)
|
141
|
-
|
142
|
-
== Copyright
|
143
|
-
|
144
|
-
(The MIT License)
|
145
|
-
|
146
|
-
{Prodis a.k.a. Fernando Hamasaki}[http://prodis.blog.br]
|
147
|
-
|
148
|
-
Copyright (c) 2011 Prodis
|
149
|
-
|
150
|
-
Permission is hereby granted, free of charge, to any person obtaining
|
151
|
-
a copy of this software and associated documentation files (the
|
152
|
-
"Software"), to deal in the Software without restriction, including
|
153
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
154
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
155
|
-
permit persons to whom the Software is furnished to do so, subject to
|
156
|
-
the following conditions:
|
157
|
-
|
158
|
-
The above copyright notice and this permission notice shall be
|
159
|
-
included in all copies or substantial portions of the Software.
|
160
|
-
|
161
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
162
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
163
|
-
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
164
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
165
|
-
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
166
|
-
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
167
|
-
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
168
|
-
|