correios 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/Rakefile +8 -13
- data/correios.gemspec +16 -27
- data/lib/correios.rb +19 -10
- data/lib/correios/version.rb +3 -0
- data/test/test_correios.rb +71 -0
- metadata +22 -41
- data/CHANGELOG +0 -0
- data/Manifest +0 -6
- data/init.rb +0 -1
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
CHANGED
@@ -1,14 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require 'rake'
|
3
|
-
|
1
|
+
require 'bundler'
|
2
|
+
require 'rake/testtask'
|
3
|
+
Bundler::GemHelper.install_tasks
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
p.ignore_pattern = ["tmp/*", "script/*"]
|
11
|
-
p.development_dependencies = ['xml-simple']
|
12
|
-
end
|
13
|
-
|
14
|
-
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
5
|
+
# just 'rake test'
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << "lib"
|
8
|
+
t.test_files = Dir["test/**/test*.rb"]
|
9
|
+
end
|
data/correios.gemspec
CHANGED
@@ -1,33 +1,22 @@
|
|
1
1
|
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "correios/version"
|
2
4
|
|
3
5
|
Gem::Specification.new do |s|
|
4
|
-
s.name
|
5
|
-
s.version
|
6
|
+
s.name = "correios"
|
7
|
+
s.version = Correios::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Bruno Frank", "kurko"]
|
10
|
+
s.email = ["bfscordeiro@gmail.com"]
|
11
|
+
s.homepage = "https://github.com/brunofrank/correios"
|
12
|
+
s.summary = %q{Calcula o valor do frete dos Correios.}
|
13
|
+
s.description = %q{Gem para calculo de valor e restreamento dos Correios.}
|
14
|
+
s.add_dependency("xml-simple")
|
6
15
|
|
7
|
-
s.
|
8
|
-
s.authors = ["Bruno Frank"]
|
9
|
-
s.date = %q{2011-05-29}
|
10
|
-
s.description = %q{Gem para calculo de valor e restreamento dos correios}
|
11
|
-
s.email = %q{bfscordeiro@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["CHANGELOG", "README.rdoc", "lib/correios.rb"]
|
13
|
-
s.files = ["CHANGELOG", "README.rdoc", "Rakefile", "init.rb", "lib/correios.rb", "Manifest", "correios.gemspec"]
|
14
|
-
s.homepage = %q{https://github.com/brunofrank/correios}
|
15
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Correios", "--main", "README.rdoc"]
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
s.rubyforge_project = %q{correios}
|
18
|
-
s.rubygems_version = %q{1.3.7}
|
19
|
-
s.summary = %q{Gem para calculo de valor e restreamento dos correios}
|
20
|
-
|
21
|
-
if s.respond_to? :specification_version then
|
22
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
-
s.specification_version = 3
|
16
|
+
s.rubyforge_project = "correios"
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
30
|
-
else
|
31
|
-
s.add_dependency(%q<xml-simple>, [">= 0"])
|
32
|
-
end
|
18
|
+
s.files = `git ls-files`.split("\n")
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
33
22
|
end
|
data/lib/correios.rb
CHANGED
@@ -21,11 +21,11 @@ class Correios
|
|
21
21
|
attr_reader :servico, :valor, :prazo, :erro, :message
|
22
22
|
|
23
23
|
def initialize(servico)
|
24
|
-
@servico = SERVICOS[servico["Codigo"].
|
25
|
-
@valor = servico["Valor"].
|
26
|
-
@prazo = servico["PrazoEntrega"].
|
27
|
-
@erro = servico["Erro"].
|
28
|
-
@message = servico["MsgErro"].
|
24
|
+
@servico = SERVICOS[servico["Codigo"].first.to_i]
|
25
|
+
@valor = servico["Valor"].first.gsub(/,/, '.').to_f
|
26
|
+
@prazo = servico["PrazoEntrega"].first.to_i
|
27
|
+
@erro = servico["Erro"].first.to_i
|
28
|
+
@message = servico["MsgErro"].first
|
29
29
|
end
|
30
30
|
|
31
31
|
def valid?
|
@@ -46,11 +46,14 @@ class Correios
|
|
46
46
|
host = 'http://ws.correios.com.br'
|
47
47
|
path = '/calculador/CalcPrecoPrazo.aspx'
|
48
48
|
|
49
|
+
identificadores = servicos
|
50
|
+
identificadores = servicos.to_a.join(',') unless servicos.kind_of? Fixnum
|
51
|
+
|
49
52
|
params = {
|
50
53
|
:nCdEmpresa => '',
|
51
54
|
:sDsSenha => '',
|
52
55
|
:StrRetorno => "xml",
|
53
|
-
:nCdServico =>
|
56
|
+
:nCdServico => identificadores,
|
54
57
|
:sCepOrigem => @cep_origem,
|
55
58
|
:sCepDestino => @cep_destino,
|
56
59
|
:nVlPeso => peso,
|
@@ -62,12 +65,19 @@ class Correios
|
|
62
65
|
:sCdMaoPropria => mao_propria,
|
63
66
|
:nVlValorDeclarado => valor_declarado,
|
64
67
|
:sCdAvisoRecebimento => aviso_recebimento
|
65
|
-
}
|
68
|
+
}
|
66
69
|
|
67
70
|
params = params.to_a.map {|item| item.to_a.join('=')} .join('&')
|
68
|
-
|
69
71
|
xml = XmlSimple.xml_in(open("#{host}#{path}?#{params}").read)
|
70
72
|
|
73
|
+
setup_services xml, servicos
|
74
|
+
end
|
75
|
+
|
76
|
+
def get_xml url
|
77
|
+
XmlSimple.xml_in(open(url).read)
|
78
|
+
end
|
79
|
+
|
80
|
+
def setup_services xml, servicos
|
71
81
|
if xml["cServico"].size > 1
|
72
82
|
servicos = {}
|
73
83
|
xml["cServico"].each do |servico|
|
@@ -76,7 +86,6 @@ class Correios
|
|
76
86
|
else
|
77
87
|
servicos = Servico.new(xml["cServico"].first)
|
78
88
|
end
|
79
|
-
|
80
|
-
return servicos
|
89
|
+
servicos
|
81
90
|
end
|
82
91
|
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require "test/unit"
|
3
|
+
require "correios"
|
4
|
+
|
5
|
+
class CorreiosTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@obj = Correios.new(76410000, 74932180)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_get_xml_for_one_service
|
12
|
+
url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&StrRetorno=xml&nCdServico=41106&sCepOrigem=76410000&sCepDestino=74932180&nVlPeso=0.4&nCdFormato=1&nVlComprimento=17&nVlAltura=16&nVlLargura=16&nVlDiametro=0&sCdMaoPropria=N&nVlValorDeclarado=0&sCdAvisoRecebimento=N"
|
13
|
+
xml = @obj.get_xml(url)
|
14
|
+
assert xml.has_key? "cServico"
|
15
|
+
first_service = xml["cServico"].first
|
16
|
+
assert_equal ["41106"], first_service["Codigo"], "Codigo"
|
17
|
+
assert_equal ["11,50"], first_service["Valor"], "Valor"
|
18
|
+
assert_equal ["5"], first_service["PrazoEntrega"], "PrazoEntrega"
|
19
|
+
assert_equal ["0,00"], first_service["ValorMaoPropria"], "ValorMaoPropria"
|
20
|
+
assert_equal ["0,00"], first_service["ValorAvisoRecebimento"], "ValorAvisoRecebimento"
|
21
|
+
assert_equal ["0,00"], first_service["ValorValorDeclarado"], "ValorValorDeclarado"
|
22
|
+
assert_equal ["N"], first_service["EntregaSabado"], "EntregaSabado"
|
23
|
+
assert_equal ["S"], first_service["EntregaDomiciliar"], "EntregaDomiciliar"
|
24
|
+
assert_equal ["0"], first_service["Erro"], "Erro"
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_get_xml_with_multiple_service
|
28
|
+
url = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&sDsSenha=&StrRetorno=xml&nCdServico=41106,40010&sCepOrigem=76410000&sCepDestino=74932180&nVlPeso=0.4&nCdFormato=1&nVlComprimento=17&nVlAltura=16&nVlLargura=16&nVlDiametro=0&sCdMaoPropria=N&nVlValorDeclarado=0&sCdAvisoRecebimento=N"
|
29
|
+
xml = @obj.get_xml(url)
|
30
|
+
assert xml.has_key? "cServico"
|
31
|
+
first_service = xml["cServico"].first
|
32
|
+
second_service = xml["cServico"].fetch(1)
|
33
|
+
|
34
|
+
# first service
|
35
|
+
assert_equal ["41106"], first_service["Codigo"], "Codigo"
|
36
|
+
assert_equal ["11,50"], first_service["Valor"], "Valor"
|
37
|
+
assert_equal ["5"], first_service["PrazoEntrega"], "PrazoEntrega"
|
38
|
+
assert_equal ["0,00"], first_service["ValorMaoPropria"], "ValorMaoPropria"
|
39
|
+
assert_equal ["0,00"], first_service["ValorAvisoRecebimento"], "ValorAvisoRecebimento"
|
40
|
+
assert_equal ["0,00"], first_service["ValorValorDeclarado"], "ValorValorDeclarado"
|
41
|
+
assert_equal ["N"], first_service["EntregaSabado"], "EntregaSabado"
|
42
|
+
assert_equal ["S"], first_service["EntregaDomiciliar"], "EntregaDomiciliar"
|
43
|
+
assert_equal ["0"], first_service["Erro"], "Erro"
|
44
|
+
# second service
|
45
|
+
assert_equal ["40010"], second_service["Codigo"], "Codigo"
|
46
|
+
assert_equal ["14,70"], second_service["Valor"], "Valor"
|
47
|
+
assert_equal ["3"], second_service["PrazoEntrega"], "PrazoEntrega"
|
48
|
+
assert_equal ["0,00"], second_service["ValorMaoPropria"], "ValorMaoPropria"
|
49
|
+
assert_equal ["0,00"], second_service["ValorAvisoRecebimento"], "ValorAvisoRecebimento"
|
50
|
+
assert_equal ["0,00"], second_service["ValorValorDeclarado"], "ValorValorDeclarado"
|
51
|
+
assert_equal ["S"], second_service["EntregaSabado"], "EntregaSabado"
|
52
|
+
assert_equal ["S"], second_service["EntregaDomiciliar"], "EntregaDomiciliar"
|
53
|
+
assert_equal ["0"], second_service["Erro"], "Erro"
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_integration
|
57
|
+
correios = Correios.new(76410000, 74932180)
|
58
|
+
frete = correios.calcular_frete(Correios::Servico::PAC, 0.4, 17, 16, 16)
|
59
|
+
assert_equal :pac, frete.servico, "Servico"
|
60
|
+
assert_equal 11.5, frete.valor, "Valor"
|
61
|
+
assert_equal 5, frete.prazo, "Prazo"
|
62
|
+
assert_equal 0, frete.erro, "Erro?"
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_force_error
|
66
|
+
correios = Correios.new(76410000, 74932180)
|
67
|
+
frete = correios.calcular_frete(99999, 0.4, 17, 16, 16)
|
68
|
+
assert_equal -1, frete.erro, "Código de erro incorreto"
|
69
|
+
assert_equal "Codigo de servico invalido.", frete.message, "Servico inválido"
|
70
|
+
end
|
71
|
+
end
|
metadata
CHANGED
@@ -1,21 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: correios
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 5
|
9
|
-
- 0
|
10
|
-
version: 0.5.0
|
4
|
+
prerelease:
|
5
|
+
version: 0.6.0
|
11
6
|
platform: ruby
|
12
7
|
authors:
|
13
8
|
- Bruno Frank
|
9
|
+
- kurko
|
14
10
|
autorequire:
|
15
11
|
bindir: bin
|
16
12
|
cert_chain: []
|
17
13
|
|
18
|
-
date: 2011-
|
14
|
+
date: 2011-06-06 00:00:00 -03:00
|
19
15
|
default_executable:
|
20
16
|
dependencies:
|
21
17
|
- !ruby/object:Gem::Dependency
|
@@ -26,42 +22,34 @@ dependencies:
|
|
26
22
|
requirements:
|
27
23
|
- - ">="
|
28
24
|
- !ruby/object:Gem::Version
|
29
|
-
hash: 3
|
30
|
-
segments:
|
31
|
-
- 0
|
32
25
|
version: "0"
|
33
|
-
type: :
|
26
|
+
type: :runtime
|
34
27
|
version_requirements: *id001
|
35
|
-
description: Gem para calculo de valor e restreamento dos
|
36
|
-
email:
|
28
|
+
description: Gem para calculo de valor e restreamento dos Correios.
|
29
|
+
email:
|
30
|
+
- bfscordeiro@gmail.com
|
37
31
|
executables: []
|
38
32
|
|
39
33
|
extensions: []
|
40
34
|
|
41
|
-
extra_rdoc_files:
|
42
|
-
|
43
|
-
- README.rdoc
|
44
|
-
- lib/correios.rb
|
35
|
+
extra_rdoc_files: []
|
36
|
+
|
45
37
|
files:
|
46
|
-
-
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
47
40
|
- README.rdoc
|
48
41
|
- Rakefile
|
49
|
-
- init.rb
|
50
|
-
- lib/correios.rb
|
51
|
-
- Manifest
|
52
42
|
- correios.gemspec
|
43
|
+
- lib/correios.rb
|
44
|
+
- lib/correios/version.rb
|
45
|
+
- test/test_correios.rb
|
53
46
|
has_rdoc: true
|
54
47
|
homepage: https://github.com/brunofrank/correios
|
55
48
|
licenses: []
|
56
49
|
|
57
50
|
post_install_message:
|
58
|
-
rdoc_options:
|
59
|
-
|
60
|
-
- --inline-source
|
61
|
-
- --title
|
62
|
-
- Correios
|
63
|
-
- --main
|
64
|
-
- README.rdoc
|
51
|
+
rdoc_options: []
|
52
|
+
|
65
53
|
require_paths:
|
66
54
|
- lib
|
67
55
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -69,26 +57,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
57
|
requirements:
|
70
58
|
- - ">="
|
71
59
|
- !ruby/object:Gem::Version
|
72
|
-
hash: 3
|
73
|
-
segments:
|
74
|
-
- 0
|
75
60
|
version: "0"
|
76
61
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
62
|
none: false
|
78
63
|
requirements:
|
79
64
|
- - ">="
|
80
65
|
- !ruby/object:Gem::Version
|
81
|
-
|
82
|
-
segments:
|
83
|
-
- 1
|
84
|
-
- 2
|
85
|
-
version: "1.2"
|
66
|
+
version: "0"
|
86
67
|
requirements: []
|
87
68
|
|
88
69
|
rubyforge_project: correios
|
89
|
-
rubygems_version: 1.
|
70
|
+
rubygems_version: 1.6.2
|
90
71
|
signing_key:
|
91
72
|
specification_version: 3
|
92
|
-
summary:
|
93
|
-
test_files:
|
94
|
-
|
73
|
+
summary: Calcula o valor do frete dos Correios.
|
74
|
+
test_files:
|
75
|
+
- test/test_correios.rb
|
data/CHANGELOG
DELETED
File without changes
|
data/Manifest
DELETED
data/init.rb
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require 'correios'
|