ship 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/correios.rb +22 -17
  2. metadata +58 -44
data/lib/correios.rb CHANGED
@@ -1,6 +1,8 @@
1
+ require "cgi"
1
2
  require "net/http"
2
3
  require "uri"
3
- require "rexml/document"
4
+ require "nokogiri"
5
+ require "open-uri"
4
6
  require "provider.rb"
5
7
 
6
8
  module Ship
@@ -44,28 +46,31 @@ module Ship
44
46
  params["nCdServico"] = shipping.service ? shipping.service.code : ""
45
47
  end
46
48
 
47
- resp, data = Net::HTTP.post_form(URI.parse("http://shopping.correios.com.br/wbm/shopping/script/CalcPrecoPrazo.aspx"),params)
48
- raise Exception.new("Could not get an answer from URL") if resp.class!=Net::HTTPOK
49
+ params_str = params.map {|key,val| "#{CGI.escape(key.to_s)}=#{CGI.escape(val.to_s)}"}.join("&")
50
+ url = URI.parse("http://ws.correios.com.br/calculador/CalcPrecoPrazo.asmx/CalcPrecoPrazo?#{params_str}")
51
+ data = open(url.to_s)
52
+ raise Exception.new("Could not get an answer from URL: #{full_path}") if data.status[0]!="200"
49
53
 
54
+ data = data.read
50
55
  error_msg = data.scan(/(<Erro>)([0-9-]{1,5})(<\/Erro>)(<MsgErro>)([a-z0-9 ]+)(<\/MsgErro>)(.*)/i)
51
56
  raise Exception.new(error_msg[0][4]) if error_msg.size>0 && error_msg[0][1]!="0"
52
57
 
53
- xml = REXML::Document.new(data)
58
+ xml = Nokogiri::XML(data)
54
59
  rtn = {}
55
60
 
56
- xml.elements.each("Servicos/cServico") do |cServico|
57
- values = {
58
- "Codigo"=>0,
59
- "Valor"=>0,
60
- "PrazoEntrega"=>0,
61
- "ValorMaoPropria"=>0,
62
- "ValorAvisoRecebimento"=>0,
63
- "ValorValorDeclarado"=>0,
64
- "EntregaDomiciliar"=>0,
65
- "EntregaSabado"=>0
66
- }
67
- values.each {|k,v| values[k] = REXML::XPath.first(cServico,"#{k}").text}
68
- rtn[values["Codigo"]] = values
61
+ xml.search("Servicos/cServico").each do |cServico|
62
+ values = {
63
+ "Codigo"=>0,
64
+ "Valor"=>0,
65
+ "PrazoEntrega"=>0,
66
+ "ValorMaoPropria"=>0,
67
+ "ValorAvisoRecebimento"=>0,
68
+ "ValorValorDeclarado"=>0,
69
+ "EntregaDomiciliar"=>0,
70
+ "EntregaSabado"=>0
71
+ }
72
+ values.each {|k,v| values[k] = cServico.search("#{k}").first.text}
73
+ rtn[values["Codigo"]] = values
69
74
  end
70
75
  return rtn
71
76
  end
metadata CHANGED
@@ -1,71 +1,85 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: ship
3
- version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 0
8
- - 3
9
- version: 0.0.3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ prerelease:
10
6
  platform: ruby
11
- authors:
7
+ authors:
12
8
  - Eustaquio Rangel
13
9
  autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
-
17
- date: 2010-05-18 00:00:00 -03:00
18
- default_executable:
19
- dependencies: []
20
-
12
+ date: 2013-07-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: open-uri
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
21
46
  description: A class to get values and tracking info from shipping providers
22
47
  email: eustaquiorangel@gmail.com
23
48
  executables: []
24
-
25
49
  extensions: []
26
-
27
- extra_rdoc_files:
50
+ extra_rdoc_files:
28
51
  - LICENSE
29
- files:
52
+ files:
53
+ - lib/provider.rb
30
54
  - lib/country.rb
31
- - lib/service.rb
32
- - lib/correios.rb
55
+ - lib/format.rb
33
56
  - lib/shipping.rb
57
+ - lib/correios.rb
34
58
  - lib/ship.rb
35
- - lib/provider.rb
36
- - lib/format.rb
59
+ - lib/service.rb
37
60
  - LICENSE
38
- has_rdoc: true
39
61
  homepage: http://github.com/taq/ship
40
62
  licenses: []
41
-
42
63
  post_install_message:
43
64
  rdoc_options: []
44
-
45
- require_paths:
65
+ require_paths:
46
66
  - lib
47
- required_ruby_version: !ruby/object:Gem::Requirement
48
- requirements:
49
- - - ">="
50
- - !ruby/object:Gem::Version
51
- segments:
52
- - 0
53
- version: "0"
54
- required_rubygems_version: !ruby/object:Gem::Requirement
55
- requirements:
56
- - - ">="
57
- - !ruby/object:Gem::Version
58
- segments:
59
- - 1
60
- - 3
61
- - 6
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
62
78
  version: 1.3.6
63
79
  requirements: []
64
-
65
80
  rubyforge_project:
66
- rubygems_version: 1.3.6
81
+ rubygems_version: 1.8.25
67
82
  signing_key:
68
83
  specification_version: 3
69
84
  summary: A class to get values and tracking info from shipping providers
70
85
  test_files: []
71
-