ship 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/correios.rb +58 -50
  2. data/lib/provider.rb +5 -4
  3. metadata +6 -6
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "cgi"
2
4
  require "net/http"
3
5
  require "uri"
@@ -7,17 +9,17 @@ require "provider.rb"
7
9
 
8
10
  module Ship
9
11
  class Correios < Ship::Provider
10
- PAC_SEM_CONTRATO = 0
11
- SEDEX_SEM_CONTRATO = 1
12
- SEDEX_A_COBRAR_SEM_CONTRATO = 2
13
- SEDEX_10_SEM_CONTRATO = 3
14
- SEDEX_HOJE_SEM_CONTRATO = 4
15
- SEDEX_COM_CONTRATO = 5
16
- ESEDEX_COM_CONTRATO = 8
17
- PAC_COM_CONTRATO = 9
12
+ PAC_SEM_CONTRATO = 0
13
+ SEDEX_SEM_CONTRATO = 1
14
+ SEDEX_A_COBRAR_SEM_CONTRATO = 2
15
+ SEDEX_10_SEM_CONTRATO = 3
16
+ SEDEX_HOJE_SEM_CONTRATO = 4
17
+ SEDEX_COM_CONTRATO = 5
18
+ ESEDEX_COM_CONTRATO = 8
19
+ PAC_COM_CONTRATO = 9
18
20
 
19
- CAIXA_PACOTE = 0
20
- ROLO_PRISMA = 1
21
+ CAIXA_PACOTE = 0
22
+ ROLO_PRISMA = 1
21
23
 
22
24
  def initialize
23
25
  super("Correios",self.class.countries,self.class.services,self.class.formats)
@@ -25,20 +27,20 @@ module Ship
25
27
 
26
28
  def cost(shipping)
27
29
  params = {}
28
- params["nCdEmpresa"] = shipping.user || ""
29
- params["sDsSenha"] = shipping.password || ""
30
- params["sCepOrigem"] = shipping.from || ""
31
- params["sCepDestino"] = shipping.to || ""
32
- params["nVlPeso"] = shipping.weight || 0
33
- params["nVlComprimento"] = shipping.length || 0
34
- params["nVlAltura"] = shipping.height || 0
35
- params["nVlLargura"] = shipping.width || 0
36
- params["nVlValorDeclarado"] = shipping.value ? shipping.value.round : 0
37
- params["sCdMaoPropria"] = shipping.in_hands ? "s" : "n"
38
- params["sCdAvisoRecebimento"] = shipping.return_receipt ? "s" : "n"
39
- params["nCdFormato"] = shipping.format ? shipping.format.code : 0
40
- params["nVlDiametro"] = shipping.diameter || 0
41
- params["StrRetorno"] = "xml"
30
+ params["nCdEmpresa"] = shipping.user || ""
31
+ params["sDsSenha"] = shipping.password || ""
32
+ params["sCepOrigem"] = shipping.from || ""
33
+ params["sCepDestino"] = shipping.to || ""
34
+ params["nVlPeso"] = shipping.weight || 0
35
+ params["nVlComprimento"] = shipping.length || 0
36
+ params["nVlAltura"] = shipping.height || 0
37
+ params["nVlLargura"] = shipping.width || 0
38
+ params["nVlValorDeclarado"] = shipping.value ? shipping.value.round : 0
39
+ params["sCdMaoPropria"] = shipping.in_hands ? "s" : "n"
40
+ params["sCdAvisoRecebimento"] = shipping.return_receipt ? "s" : "n"
41
+ params["nCdFormato"] = shipping.format ? shipping.format.code : 0
42
+ params["nVlDiametro"] = shipping.diameter || 0
43
+ params["StrRetorno"] = "xml"
42
44
 
43
45
  if shipping.service.kind_of?(Array)
44
46
  params["nCdServico"] = shipping.service.map {|s| s.code}.join(",")
@@ -72,7 +74,10 @@ module Ship
72
74
  values.each {|k,v| values[k] = cServico.search("#{k}").first.text}
73
75
  rtn[values["Codigo"]] = values
74
76
  end
75
- return rtn
77
+ rtn
78
+ rescue => e
79
+ STDERR.puts "error: #{e}"
80
+ []
76
81
  end
77
82
 
78
83
  def where(shipping)
@@ -85,23 +90,26 @@ module Ship
85
90
  "z_action" => "",
86
91
  "p_cod_lis" => shipping.id
87
92
  }
88
- url = "http://websro.correios.com.br/sro_bin/txect01$.inexistente?"+parms.map {|k,v| "#{k}=#{v}"}.join("&")
89
- data = Net::HTTP.get(URI.parse(url))
93
+ url = "http://websro.correios.com.br/sro_bin/txect01$.inexistente?"+parms.map {|k,v| "#{k}=#{v}"}.join("&")
94
+ data = open(URI.parse(url).to_s).read
90
95
  raise Exception.new("Could not get an answer from URL") if data.size<1
91
- rst = []
92
- rows = data.gsub("\n","").split(/<[\/]?tr>/).select {|r| r =~ /^<td/}
93
- rows.shift # discard
94
- ldate, ldesc, lstatus = nil, nil, nil
95
- rows.each do |row|
96
- date, desc, status = row.scan(/(>)([^<]+)(<)/).flatten.reject {|i| i =~ />|</}
97
- if !desc.nil?
98
- ldate, ldesc, lstatus = date, desc, status
99
- else
100
- desc, date, status = date, ldate, lstatus
101
- end
102
- rst << {:date=>date.strip,:desc=>desc.strip,:status=>status.strip}
103
- end
104
- rst.reverse
96
+ doc = Nokogiri::HTML(data)
97
+ rst = []
98
+ ldate = nil
99
+ doc.search("tr").each do |tr|
100
+ tds = tr.search("td")
101
+ matches = /^(?<date>\d{2}\/\d{2}\/\d{4} \d{2}:\d{2})|(?<address>Endereço)/.match(tds[0])
102
+ next if !matches
103
+ date = matches[:date] || ldate
104
+ ldate = date
105
+ desc = matches[:date] ? tds[1].text : tds[0].text
106
+ status = matches[:date] ? tds[2].text : "Postado"
107
+ rst << {date: date.strip, desc: desc.strip, status: status.strip}
108
+ end
109
+ rst.reverse
110
+ rescue => e
111
+ STDOUT.puts "error: #{e}"
112
+ []
105
113
  end
106
114
 
107
115
  def self.countries
@@ -110,16 +118,16 @@ module Ship
110
118
 
111
119
  def self.services
112
120
  [
113
- Service.new("PAC sem contrato" ,41106,false),
114
- Service.new("SEDEX sem contrato" ,40010,false),
121
+ Service.new("PAC sem contrato" ,41106,false),
122
+ Service.new("SEDEX sem contrato" ,40010,false),
115
123
  Service.new("SEDEX a cobrar, sem contrato",40045,false),
116
- Service.new("SEDEX 10, sem contrato" ,40215,false),
117
- Service.new("SEDEX Hoje, sem contrato" ,40290,false),
118
- Service.new("SEDEX com contrato" ,40096,true ),
119
- Service.new("SEDEX com contrato" ,40436,true ),
120
- Service.new("SEDEX com contrato" ,40444,true ),
121
- Service.new("e-SEDEX, com contrato" ,81019,true ),
122
- Service.new("PAC com contrato" ,41068,true)
124
+ Service.new("SEDEX 10, sem contrato" ,40215,false),
125
+ Service.new("SEDEX Hoje, sem contrato" ,40290,false),
126
+ Service.new("SEDEX com contrato" ,40096,true ),
127
+ Service.new("SEDEX com contrato" ,40436,true ),
128
+ Service.new("SEDEX com contrato" ,40444,true ),
129
+ Service.new("e-SEDEX, com contrato" ,81019,true ),
130
+ Service.new("PAC com contrato" ,41068,true)
123
131
  ]
124
132
  end
125
133
 
@@ -5,11 +5,12 @@ require "country.rb"
5
5
  module Ship
6
6
  class Provider
7
7
  attr_reader :name, :countries, :services, :formats
8
+
8
9
  def initialize(name,countries,services,formats)
9
- @name = name
10
- @countries = countries
11
- @services = services
12
- @formats = formats
10
+ @name = name
11
+ @countries = countries
12
+ @services = services
13
+ @formats = formats
13
14
  end
14
15
 
15
16
  def to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ship
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-07-27 00:00:00.000000000 Z
12
+ date: 2013-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -34,13 +34,13 @@ extensions: []
34
34
  extra_rdoc_files:
35
35
  - LICENSE
36
36
  files:
37
+ - lib/provider.rb
37
38
  - lib/country.rb
38
- - lib/service.rb
39
- - lib/correios.rb
39
+ - lib/format.rb
40
40
  - lib/shipping.rb
41
+ - lib/correios.rb
41
42
  - lib/ship.rb
42
- - lib/provider.rb
43
- - lib/format.rb
43
+ - lib/service.rb
44
44
  - LICENSE
45
45
  homepage: http://github.com/taq/ship
46
46
  licenses: