mooamba 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +27 -0
- data/README.markdown +36 -0
- data/Rakefile +35 -0
- data/build.sh +2 -0
- data/lib/mooamba/core_ext.rb +25 -0
- data/lib/mooamba/exceptions.rb +14 -0
- data/lib/mooamba/sedex.rb +66 -0
- data/lib/mooamba.rb +7 -0
- data/mooamba.gemspec +61 -0
- data/spec/data/invalid.xml +15 -0
- data/spec/data/invalid_destiny.xml +15 -0
- data/spec/data/invalid_origin.xml +15 -0
- data/spec/data/invalid_weight.xml +15 -0
- data/spec/data/malformed.xml +1 -0
- data/spec/data/unavailable_service.xml +15 -0
- data/spec/data/unavailable_service_for_area.xml +15 -0
- data/spec/data/valid.xml +15 -0
- data/spec/mooamba/sedex_spec.rb +80 -0
- data/spec/spec.opts +3 -0
- data/spec/spec_helper.rb +41 -0
- metadata +89 -0
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
fakeweb (1.3.0)
|
5
|
+
gemcutter (0.6.1)
|
6
|
+
git (1.2.5)
|
7
|
+
jeweler (1.4.0)
|
8
|
+
gemcutter (>= 0.1.0)
|
9
|
+
git (>= 1.2.5)
|
10
|
+
rubyforge (>= 2.0.0)
|
11
|
+
json_pure (1.4.6)
|
12
|
+
nokogiri (1.4.3.1)
|
13
|
+
rspec (1.3.0)
|
14
|
+
rubyforge (2.0.4)
|
15
|
+
json_pure (>= 1.1.7)
|
16
|
+
sax-machine (0.0.15)
|
17
|
+
nokogiri (> 0.0.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
fakeweb
|
24
|
+
jeweler
|
25
|
+
nokogiri
|
26
|
+
rspec
|
27
|
+
sax-machine
|
data/README.markdown
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
## ABOUT
|
2
|
+
|
3
|
+
Ruby API to calculate shipping cost from Correios site by CEP. Currently it supports only Sedex.
|
4
|
+
|
5
|
+
## USAGE
|
6
|
+
|
7
|
+
You must inform 3 parameters: from, to and weight (Kg)
|
8
|
+
|
9
|
+
sedex = Mooamba::Sedex.calculate(:from => "04548-040", :to => "05414-000", :weight => "5")
|
10
|
+
sedex.price
|
11
|
+
=> 14,40
|
12
|
+
|
13
|
+
## LICENSE
|
14
|
+
|
15
|
+
(The MIT License)
|
16
|
+
|
17
|
+
Copyright © 2010
|
18
|
+
|
19
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
20
|
+
a copy of this software and associated documentation files (the
|
21
|
+
‘Software’), to deal in the Software without restriction, including
|
22
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
23
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
24
|
+
permit persons to whom the Software is furnished to do so, subject to
|
25
|
+
the following conditions:
|
26
|
+
|
27
|
+
The above copyright notice and this permission notice shall be
|
28
|
+
included in all copies or substantial portions of the Software.
|
29
|
+
|
30
|
+
THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND,
|
31
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
32
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
33
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
34
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
35
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
36
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
ENV['RSPEC_COLOR'] = 'true'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gemspec|
|
9
|
+
gemspec.name = "mooamba"
|
10
|
+
gemspec.version = "0.0.1"
|
11
|
+
gemspec.summary = ""
|
12
|
+
gemspec.description = ""
|
13
|
+
gemspec.homepage = "http://github.com/gonow/mooamba"
|
14
|
+
gemspec.authors = ["GoNow"]
|
15
|
+
end
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'spec/rake/spectask'
|
21
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
22
|
+
spec.libs << 'lib' << 'spec'
|
23
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
end
|
25
|
+
|
26
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
27
|
+
spec.libs << 'lib' << 'spec'
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
end
|
31
|
+
|
32
|
+
task :spec => :check_dependencies
|
33
|
+
|
34
|
+
task :default => :spec
|
35
|
+
|
data/build.sh
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
class Object
|
2
|
+
def blank?
|
3
|
+
respond_to?(:empty?) ? empty? : !self
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class Hash
|
8
|
+
def to_query_string
|
9
|
+
self.map { |k, v| "#{k}=#{v}" }.join("&")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class String
|
14
|
+
def blank?
|
15
|
+
self !~ /\S/
|
16
|
+
end
|
17
|
+
|
18
|
+
def invalid?
|
19
|
+
self !~ /^\d{5}-\d{3}$/
|
20
|
+
end
|
21
|
+
|
22
|
+
def clean
|
23
|
+
self.gsub "-", ""
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Mooamba
|
2
|
+
module Correios
|
3
|
+
class InvalidOriginZipCode < StandardError; end
|
4
|
+
class InvalidDestinyZipCode < StandardError; end
|
5
|
+
class InvalidWeight < StandardError; end
|
6
|
+
class UnavailableServiceForArea < StandardError; end
|
7
|
+
class UnavailableService < StandardError; end
|
8
|
+
class UnknownError < StandardError; end
|
9
|
+
end
|
10
|
+
|
11
|
+
class InvalidDestinationZipCode < StandardError; end
|
12
|
+
class InvalidOriginZipCode < StandardError; end
|
13
|
+
class UnavailableAPI < StandardError; end
|
14
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Mooamba
|
2
|
+
module Correios
|
3
|
+
API_URL = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?"
|
4
|
+
end
|
5
|
+
|
6
|
+
class Sedex
|
7
|
+
include SAXMachine
|
8
|
+
|
9
|
+
CODE = 40010
|
10
|
+
|
11
|
+
DEFAULT_PARAMS = {
|
12
|
+
:nCdServico => CODE,
|
13
|
+
:StrRetorno => "XML"
|
14
|
+
}
|
15
|
+
|
16
|
+
element "Valor", :as => :price
|
17
|
+
element "Erro", :as => :error
|
18
|
+
element "cServico", :as => :service
|
19
|
+
|
20
|
+
def self.calculate(options={})
|
21
|
+
raise InvalidOriginZipCode if invalid_cep?(options[:from])
|
22
|
+
raise InvalidDestinationZipCode if invalid_cep?(options[:to])
|
23
|
+
response = response_for options
|
24
|
+
error?(response) ? (raise exception(response)) : response
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def self.parameters_from(options)
|
30
|
+
translate(options).merge DEFAULT_PARAMS
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.translate(options)
|
34
|
+
{ :sCepOrigem => options[:from].clean,
|
35
|
+
:sCepDestino => options[:to].clean,
|
36
|
+
:nVlPeso => options[:weight] }
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.response_for(options)
|
40
|
+
parse open(URI.escape(Correios::API_URL + parameters_from(options).to_query_string)).read
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.exception(response)
|
44
|
+
if response.service.nil?
|
45
|
+
Mooamba::UnavailableAPI
|
46
|
+
else
|
47
|
+
case response.error
|
48
|
+
when "-2"; Mooamba::Correios::InvalidOriginZipCode
|
49
|
+
when "-3"; Mooamba::Correios::InvalidDestinyZipCode
|
50
|
+
when "-4"; Mooamba::Correios::InvalidWeight
|
51
|
+
when "-6"; Mooamba::Correios::UnavailableServiceForArea
|
52
|
+
when "7"; Mooamba::Correios::UnavailableService
|
53
|
+
else; Mooamba::Correios::UnknownError
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.error?(response)
|
59
|
+
response.error != "0"
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.invalid_cep?(cep)
|
63
|
+
cep.blank? or cep.invalid?
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/mooamba.rb
ADDED
data/mooamba.gemspec
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{mooamba}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["GoNow"]
|
12
|
+
s.date = %q{2010-09-23}
|
13
|
+
s.description = %q{}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README.markdown"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"Gemfile",
|
20
|
+
"Gemfile.lock",
|
21
|
+
"README.markdown",
|
22
|
+
"Rakefile",
|
23
|
+
"build.sh",
|
24
|
+
"lib/mooamba.rb",
|
25
|
+
"lib/mooamba/core_ext.rb",
|
26
|
+
"lib/mooamba/exceptions.rb",
|
27
|
+
"lib/mooamba/sedex.rb",
|
28
|
+
"mooamba.gemspec",
|
29
|
+
"spec/data/invalid.xml",
|
30
|
+
"spec/data/invalid_destiny.xml",
|
31
|
+
"spec/data/invalid_origin.xml",
|
32
|
+
"spec/data/invalid_weight.xml",
|
33
|
+
"spec/data/malformed.xml",
|
34
|
+
"spec/data/unavailable_service.xml",
|
35
|
+
"spec/data/unavailable_service_for_area.xml",
|
36
|
+
"spec/data/valid.xml",
|
37
|
+
"spec/mooamba/sedex_spec.rb",
|
38
|
+
"spec/spec.opts",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/gonow/mooamba}
|
42
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.3.7}
|
45
|
+
s.summary = %q{}
|
46
|
+
s.test_files = [
|
47
|
+
"spec/mooamba/sedex_spec.rb",
|
48
|
+
"spec/spec_helper.rb"
|
49
|
+
]
|
50
|
+
|
51
|
+
if s.respond_to? :specification_version then
|
52
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
53
|
+
s.specification_version = 3
|
54
|
+
|
55
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
+
else
|
57
|
+
end
|
58
|
+
else
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>-10</Erro>
|
13
|
+
<MsgErro>Precificação indisponível para o trecho informado.</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>-3</Erro>
|
13
|
+
<MsgErro>CEP de destino invalido.</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>-2</Erro>
|
13
|
+
<MsgErro>CEP de origem invalido.</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>-4</Erro>
|
13
|
+
<MsgErro>Peso excedido.</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1 @@
|
|
1
|
+
-
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>7</Erro>
|
13
|
+
<MsgErro>Serviço indisponível, tente mais tarde.</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>0,00</Valor>
|
6
|
+
<PrazoEntrega>0</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar></EntregaDomiciliar>
|
11
|
+
<EntregaSabado></EntregaSabado>
|
12
|
+
<Erro>-6</Erro>
|
13
|
+
<MsgErro>Serviço indisponível para o trecho informado</MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
data/spec/data/valid.xml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
<?xml version="1.0" encoding="ISO-8859-1" ?>
|
2
|
+
<Servicos>
|
3
|
+
<cServico>
|
4
|
+
<Codigo>40010</Codigo>
|
5
|
+
<Valor>11,80</Valor>
|
6
|
+
<PrazoEntrega>1</PrazoEntrega>
|
7
|
+
<ValorMaoPropria>0,00</ValorMaoPropria>
|
8
|
+
<ValorAvisoRecebimento>0,00</ValorAvisoRecebimento>
|
9
|
+
<ValorValorDeclarado>0,00</ValorValorDeclarado>
|
10
|
+
<EntregaDomiciliar>S</EntregaDomiciliar>
|
11
|
+
<EntregaSabado>S</EntregaSabado>
|
12
|
+
<Erro>0</Erro>
|
13
|
+
<MsgErro></MsgErro>
|
14
|
+
</cServico>
|
15
|
+
</Servicos>
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Mooamba::Sedex do
|
4
|
+
before :all do
|
5
|
+
@default_parameters = { :from => VALID_ZIP_CODES.first,
|
6
|
+
:to => VALID_ZIP_CODES.last,
|
7
|
+
:weight => 1 }
|
8
|
+
map_parameters_to_file(@default_parameters, "valid")
|
9
|
+
end
|
10
|
+
|
11
|
+
context "on .calculdate" do
|
12
|
+
it "should use the Sedex service" do
|
13
|
+
Mooamba::Sedex.should_receive(:open).with(/nCdServico\=40010/).and_return(valid_xml_file)
|
14
|
+
Mooamba::Sedex.calculate(@default_parameters)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return the price as #price" do
|
18
|
+
Mooamba::Sedex.calculate(@default_parameters).price.should eql "11,80"
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should raise Mooamba::InvalidOriginZipCode when there's no origin zip code" do
|
22
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.except(:from)) }.should raise_error(Mooamba::InvalidOriginZipCode)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should raise Mooamba::InvalidDestinationZipCode when there's no destiny zip code" do
|
26
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.except(:to)) }.should raise_error(Mooamba::InvalidDestinationZipCode)
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should raise Mooamba::InvalidOriginZipCode when the origin zip code is blank" do
|
30
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:from => "")) }.should raise_error(Mooamba::InvalidOriginZipCode)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise Mooamba::InvalidDestinationZipCode when the destiny zip code is blank" do
|
34
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:to => "")) }.should raise_error(Mooamba::InvalidDestinationZipCode)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should raise Mooamba::InvalidOriginZipCode when the origin zip code is invalid" do
|
38
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:from => INVALID_ZIP_CODES.last)) }.should raise_error(Mooamba::InvalidOriginZipCode)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should raise Mooamba::InvalidDestinationZipCode when the destiny zip code is invalid" do
|
42
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters.merge(:to => INVALID_ZIP_CODES.first)) }.should raise_error(Mooamba::InvalidDestinationZipCode)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should raise Mooamba::UnavailableAPI when api returns unexpected content" do
|
46
|
+
map_parameters_to_file(@default_parameters, "malformed")
|
47
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::UnavailableAPI)
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should raise Mooamba::Correios::UnavaibleService when api returns service unavailable" do
|
51
|
+
map_parameters_to_file(@default_parameters, "unavailable_service")
|
52
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnavailableService)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should raise Mooamba::Correios::InvalidOriginZipCode when the invalid origin zip code doesn't exist" do
|
56
|
+
map_parameters_to_file(@default_parameters, "invalid_origin")
|
57
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidOriginZipCode)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should raise Mooamba::Correios::InvalidDestinyZipCode when the invalid destiny zip code doesn't exist" do
|
61
|
+
map_parameters_to_file(@default_parameters, "invalid_destiny")
|
62
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidDestinyZipCode)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should raise Mooamba::Correios::InvalidWeight when the weight if excessive" do
|
66
|
+
map_parameters_to_file(@default_parameters, "invalid_weight")
|
67
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::InvalidWeight)
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should raise Mooamba::Correios::UnavailableServiceForArea when the service is unavailable for the current area" do
|
71
|
+
map_parameters_to_file(@default_parameters, "unavailable_service_for_area")
|
72
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnavailableServiceForArea)
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should raise Mooamba::Correios::UnknownError when an unknown error happened" do
|
76
|
+
map_parameters_to_file(@default_parameters, "invalid")
|
77
|
+
lambda { Mooamba::Sedex.calculate(@default_parameters) }.should raise_error(Mooamba::Correios::UnknownError)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'rubygems'
|
4
|
+
require 'spec'
|
5
|
+
require 'spec/autorun'
|
6
|
+
require 'fakeweb'
|
7
|
+
require 'mooamba'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
FakeWeb.allow_net_connect = false
|
11
|
+
end
|
12
|
+
|
13
|
+
VALID_ZIP_CODES = %w(04548-040 01310-100)
|
14
|
+
INVALID_ZIP_CODES = %w(0 00)
|
15
|
+
|
16
|
+
def map_parameters_to_file(options, file)
|
17
|
+
parameters = {
|
18
|
+
:sCepOrigem => options[:from].gsub("-", ""),
|
19
|
+
:sCepDestino => options[:to].gsub("-", ""),
|
20
|
+
:nVlPeso => options[:weight],
|
21
|
+
:StrRetorno => "XML"
|
22
|
+
}.merge Mooamba::Sedex::DEFAULT_PARAMS
|
23
|
+
|
24
|
+
FakeWeb.register_uri(:get, URI.escape(Mooamba::Correios::API_URL + parameters.to_query_string), :body => data(file).read)
|
25
|
+
end
|
26
|
+
|
27
|
+
def valid_xml_file
|
28
|
+
data "valid"
|
29
|
+
end
|
30
|
+
|
31
|
+
def data(name)
|
32
|
+
File.open("#{File.dirname(__FILE__)}/data/#{name}.xml")
|
33
|
+
end
|
34
|
+
|
35
|
+
class Hash
|
36
|
+
def except(*blacklist)
|
37
|
+
{}.tap do |h|
|
38
|
+
(keys - blacklist).each { |k| h[k] = self[k] }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mooamba
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- GoNow
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-09-23 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: ""
|
23
|
+
email:
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files:
|
29
|
+
- README.markdown
|
30
|
+
files:
|
31
|
+
- .gitignore
|
32
|
+
- Gemfile
|
33
|
+
- Gemfile.lock
|
34
|
+
- README.markdown
|
35
|
+
- Rakefile
|
36
|
+
- build.sh
|
37
|
+
- lib/mooamba.rb
|
38
|
+
- lib/mooamba/core_ext.rb
|
39
|
+
- lib/mooamba/exceptions.rb
|
40
|
+
- lib/mooamba/sedex.rb
|
41
|
+
- mooamba.gemspec
|
42
|
+
- spec/data/invalid.xml
|
43
|
+
- spec/data/invalid_destiny.xml
|
44
|
+
- spec/data/invalid_origin.xml
|
45
|
+
- spec/data/invalid_weight.xml
|
46
|
+
- spec/data/malformed.xml
|
47
|
+
- spec/data/unavailable_service.xml
|
48
|
+
- spec/data/unavailable_service_for_area.xml
|
49
|
+
- spec/data/valid.xml
|
50
|
+
- spec/mooamba/sedex_spec.rb
|
51
|
+
- spec/spec.opts
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/gonow/mooamba
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --charset=UTF-8
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project:
|
83
|
+
rubygems_version: 1.3.7
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: ""
|
87
|
+
test_files:
|
88
|
+
- spec/mooamba/sedex_spec.rb
|
89
|
+
- spec/spec_helper.rb
|