lib/mooamba/sedex.rb in mooamba-0.0.1 vs lib/mooamba/sedex.rb in mooamba-0.0.2
- old
+ new
@@ -1,6 +1,6 @@
-module Mooamba
+module Mooamba
module Correios
API_URL = "http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?"
end
class Sedex
@@ -18,10 +18,11 @@
element "cServico", :as => :service
def self.calculate(options={})
raise InvalidOriginZipCode if invalid_cep?(options[:from])
raise InvalidDestinationZipCode if invalid_cep?(options[:to])
+ raise InvalidWeight if invalid_weight?(options[:weight])
response = response_for options
error?(response) ? (raise exception(response)) : response
end
private
@@ -40,17 +41,18 @@
parse open(URI.escape(Correios::API_URL + parameters_from(options).to_query_string)).read
end
def self.exception(response)
if response.service.nil?
- Mooamba::UnavailableAPI
+ Mooamba::UnavailableAPI
else
case response.error
when "-2"; Mooamba::Correios::InvalidOriginZipCode
when "-3"; Mooamba::Correios::InvalidDestinyZipCode
- when "-4"; Mooamba::Correios::InvalidWeight
+ when "-4"; Mooamba::Correios::ExcessiveWeight
when "-6"; Mooamba::Correios::UnavailableServiceForArea
+ when "-10"; Mooamba::Correios::UnavailableServiceForArea
when "7"; Mooamba::Correios::UnavailableService
else; Mooamba::Correios::UnknownError
end
end
end
@@ -59,8 +61,12 @@
response.error != "0"
end
def self.invalid_cep?(cep)
cep.blank? or cep.invalid?
+ end
+
+ def self.invalid_weight?(weight)
+ weight.blank? or weight.to_i <= 0
end
end
end