Class: Ship::Correios
Constant Summary collapse
- PAC_SEM_CONTRATO =
0- SEDEX_SEM_CONTRATO =
1- SEDEX_A_COBRAR_SEM_CONTRATO =
2- SEDEX_10_SEM_CONTRATO =
3- SEDEX_HOJE_SEM_CONTRATO =
4- SEDEX_COM_CONTRATO =
5- ESEDEX_COM_CONTRATO =
8- PAC_COM_CONTRATO =
9- CAIXA_PACOTE =
0- ROLO_PRISMA =
1
Instance Attribute Summary
Attributes inherited from Provider
#countries, #formats, #name, #services
Class Method Summary collapse
Instance Method Summary collapse
- #cost(shipping) ⇒ Object
-
#initialize ⇒ Correios
constructor
A new instance of Correios.
- #where(shipping) ⇒ Object
Methods inherited from Provider
Constructor Details
#initialize ⇒ Correios
Returns a new instance of Correios.
22 23 24 |
# File 'lib/correios.rb', line 22 def initialize super("Correios",self.class.countries,self.class.services,self.class.formats) end |
Class Method Details
.countries ⇒ Object
107 108 109 |
# File 'lib/correios.rb', line 107 def self.countries [Country.new("Brasil","BRA")] end |
.formats ⇒ Object
126 127 128 129 130 131 |
# File 'lib/correios.rb', line 126 def self.formats [ Format.new("Caixa/pacote",1), Format.new("Rolo/prisma",2) ] end |
.services ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/correios.rb', line 111 def self.services [ Service.new("PAC sem contrato" ,41106,false), Service.new("SEDEX sem contrato" ,40010,false), Service.new("SEDEX a cobrar, sem contrato",40045,false), Service.new("SEDEX 10, sem contrato" ,40215,false), Service.new("SEDEX Hoje, sem contrato" ,40290,false), Service.new("SEDEX com contrato" ,40096,true ), Service.new("SEDEX com contrato" ,40436,true ), Service.new("SEDEX com contrato" ,40444,true ), Service.new("e-SEDEX, com contrato" ,81019,true ), Service.new("PAC com contrato" ,41068,true) ] end |
Instance Method Details
#cost(shipping) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/correios.rb', line 26 def cost(shipping) params = {} params["nCdEmpresa"] = shipping.user || "" params["sDsSenha"] = shipping.password || "" params["sCepOrigem"] = shipping.from || "" params["sCepDestino"] = shipping.to || "" params["nVlPeso"] = shipping.weight || 0 params["nVlComprimento"] = shipping.length || 0 params["nVlAltura"] = shipping.height || 0 params["nVlLargura"] = shipping.width || 0 params["nVlValorDeclarado"] = shipping.value ? shipping.value.round : 0 params["sCdMaoPropria"] = shipping.in_hands ? "s" : "n" params["sCdAvisoRecebimento"] = shipping.return_receipt ? "s" : "n" params["nCdFormato"] = shipping.format ? shipping.format.code : 0 params["nVlDiametro"] = shipping.diameter || 0 params["StrRetorno"] = "xml" if shipping.service.kind_of?(Array) params["nCdServico"] = shipping.service.map {|s| s.code}.join(",") else params["nCdServico"] = shipping.service ? shipping.service.code : "" end params_str = params.map {|key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}"}.join("&") url = URI.parse("http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo?#{params_str}") data = open(url.to_s) raise Exception.new("Could not get an answer from URL: #{full_path}") if data.status[0]!="200" data = data.read error_msg = data.scan(/(<Erro>)([0-9-]{1,5})(<\/Erro>)(<MsgErro>)([a-z0-9 ]+)(<\/MsgErro>)(.*)/i) raise Exception.new(error_msg[0][4]) if error_msg.size>0 && error_msg[0][1]!="0" xml = Nokogiri::XML(data) rtn = {} xml.search("Servicos/cServico").each do |cServico| values = { "Codigo"=>0, "Valor"=>0, "PrazoEntrega"=>0, "ValorMaoPropria"=>0, "ValorAvisoRecebimento"=>0, "ValorValorDeclarado"=>0, "EntregaDomiciliar"=>0, "EntregaSabado"=>0 } values.each {|k,v| values[k] = cServico.search("#{k}").first.text} rtn[values["Codigo"]] = values end return rtn end |
#where(shipping) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/correios.rb', line 78 def where(shipping) raise Exception.new("No shipping info") if shipping.nil? || shipping.id.nil? parms = { "p_itemcode" => "", "p_lingua" => "001", "p_teste=" => "", "p_tipo" => "003", "z_action" => "", "p_cod_lis" => shipping.id } url = "http://websro.correios.com.br/sro_bin/txect01$.inexistente?"+parms.map {|k,v| "#{k}=#{v}"}.join("&") data = Net::HTTP.get(URI.parse(url)) raise Exception.new("Could not get an answer from URL") if data.size<1 rst = [] rows = data.gsub("\n","").split(/<[\/]?tr>/).select {|r| r =~ /^<td/} rows.shift # discard ldate, ldesc, lstatus = nil, nil, nil rows.each do |row| date, desc, status = row.scan(/(>)([^<]+)(<)/).flatten.reject {|i| i =~ />|</} if !desc.nil? ldate, ldesc, lstatus = date, desc, status else desc, date, status = date, ldate, lstatus end rst << {:date=>date.strip,:desc=>desc.strip,:status=>status.strip} end rst.reverse end |