corrails 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ require 'net/http'
2
+ require File.expand_path('../parser/correios_parser', __FILE__)
3
+ module Corrails
4
+
5
+ class Correios
6
+ def rastrear(id_objeto)
7
+ uri = URI.parse("http://websro.correios.com.br/sro_bin/txect01$.QueryList");
8
+
9
+ params = {
10
+ 'P_LINGUA' => '001',
11
+ 'P_TIPO' => '001',
12
+ 'P_COD_UNI' => id_objeto
13
+ }
14
+
15
+ http = Net::HTTP.new(uri.host, uri.port)
16
+ request = Net::HTTP::Get.new(uri.path)
17
+ request.set_form_data( params )
18
+ request = Net::HTTP::Get.new( uri.path+ '?' + request.body )
19
+ response = http.request(request)
20
+ Parser::CorreiosParser.parse(response.body)
21
+ end
22
+ end
23
+
24
+ end
@@ -0,0 +1,30 @@
1
+ require 'rubygems'
2
+ require 'savon'
3
+ require File.expand_path('../parser/directlog_parser', __FILE__)
4
+ require File.expand_path('../model/response', __FILE__)
5
+
6
+ module Corrails
7
+
8
+ class Directlog
9
+ attr_accessor :wsdl
10
+ def initialize(wsdl)
11
+ @client = Savon.client(wsdl);
12
+ end
13
+
14
+ def rastrear(login, password, pedido)
15
+ resp = Response.new
16
+ begin
17
+ xml = "<?xml version="1.0" encoding="ISO-8859-1" ?><directlog><parametros><parametro name="pedido">#{pedido}</parametro></parametros></directlog>"
18
+ response = @client.request :consulta_status_entrega, :xmlns => 'http://www.directlog.com.br/' do
19
+ soap.namespaces["xmlns:soap"] = "http://schemas.xmlsoap.org/soap/envelope/"
20
+ soap.body = "<login>#{login}</login><password>#{password}</password><xml>#{xml}</xml><type>DEFAULT</type>"
21
+ end
22
+ resp = Parser::DirectlogParser.parse(response)
23
+ rescue => e
24
+ resp.error_msg=e.message
25
+ end
26
+ resp
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,13 @@
1
+ module Corrails
2
+ class Response
3
+ attr_accessor :result, :error_msg, :item, :status
4
+
5
+ def initialize
6
+ end
7
+
8
+ def ok?
9
+ result == true
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path('../../model/response', __FILE__)
2
+
3
+ module Corrails
4
+ module Parser
5
+ class CorreiosParser
6
+
7
+ def self.parse(html)
8
+ response = Corrails::Response.new
9
+ regex = /^<tr><td rowspan=.*>(.*)<\/td><td>(.*)<\/td><td><FONT COLOR=".*">(.*)<\/font><\/td><\/tr>$/
10
+ historicos = []
11
+ html.scan(regex) do |data, local, status|
12
+ historicos.push({
13
+ :data => data,
14
+ :local => local,
15
+ :status => status
16
+ })
17
+ end
18
+
19
+ if not (historicos.empty?)
20
+ response.result = true
21
+ response.item = Hash.new
22
+ response.item[:historico] = historicos
23
+ else
24
+ response.error_msg = 'O nosso sistema não possui dados sobre o objeto informado. Verifique se o código digitado está correto'
25
+ end
26
+ response
27
+ end
28
+
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,67 @@
1
+ require 'nokogiri'
2
+ require File.expand_path('../../model/response', __FILE__)
3
+
4
+ module Corrails
5
+ module Parser
6
+ class DirectlogParser
7
+ def self.parse(returnXml)
8
+ resp = returnXml.to_hash[:consulta_status_entrega_response]
9
+ response = Corrails::Response.new
10
+ response.result = resp[:consulta_status_entrega_result]
11
+ if(response.ok?)
12
+ response.item = build_remessa(resp[:xml])
13
+ else
14
+ response.error_msg = get_error(resp[:xml])
15
+ end
16
+ response
17
+ end
18
+
19
+ def self.get_error xml
20
+ xml_doc = Nokogiri::XML(xml)
21
+ xml_doc.xpath('//mensagem').first.content
22
+ end
23
+
24
+ def self.build_remessa(xml)
25
+ xml_doc = Nokogiri::XML(xml)
26
+ numero = xml_doc.xpath('//nrremessa').first.content
27
+ pedido = xml_doc.xpath('//nrpedido').first.content
28
+ notafiscal = xml_doc.xpath('//nrnotafiscal').first.content
29
+ serianota = xml_doc.xpath('//serianota').first.content
30
+
31
+ remessa = Hash.new
32
+ remessa[:numero] = numero
33
+ remessa[:pedido] = pedido
34
+ remessa[:notafiscal] = notafiscal
35
+ remessa[:serianota] = serianota
36
+ remessa[:historico] = get_status_list(xml_doc)
37
+ remessa
38
+ end
39
+
40
+ def self.get_status_list(xml_doc)
41
+ status_list = Array.new
42
+ historicos = xml_doc.xpath("//historicos//status")
43
+ historicos.each do |historico|
44
+ status = Hash.new
45
+ status[:codigo] = historico.xpath('codstatus').first.content
46
+ status[:status] = historico.xpath('descricaostatus').first.content
47
+ status[:data] = historico.xpath('dtstatus').first.content
48
+ status[:ocorrencias] = get_ocorrencias(historico)
49
+ status_list << status
50
+ end
51
+ status_list
52
+ end
53
+
54
+ def self.get_ocorrencias(historico)
55
+ list = Array.new
56
+ ocorrencias = historico.xpath('ocorrencia')
57
+ ocorrencias.each do |ocorrencia|
58
+ ocorren = Hash.new
59
+ ocorren[:codigo] = ocorrencia.xpath('codocorrencia').first.content
60
+ ocorren[:descricao] = ocorrencia.xpath('descricaoocorrencia').first.content
61
+ list << ocorren
62
+ end
63
+ list
64
+ end
65
+ end
66
+ end
67
+ end
data/lib/corrails.rb ADDED
@@ -0,0 +1,6 @@
1
+ require 'corrails/correios'
2
+ require 'corrails/directlog'
3
+
4
+ module Corrails
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: corrails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
11
+ platform: ruby
12
+ authors:
13
+ - Vizir
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-12-02 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: savon
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 31
29
+ segments:
30
+ - 1
31
+ - 2
32
+ - 0
33
+ version: 1.2.0
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: nokogiri
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 9
45
+ segments:
46
+ - 1
47
+ - 5
48
+ - 5
49
+ version: 1.5.5
50
+ type: :runtime
51
+ version_requirements: *id002
52
+ description: Gem utilizada para rastrear pedidos da Directlog ou dos Correios
53
+ email: contato@vizir.com.br
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files: []
59
+
60
+ files:
61
+ - lib/corrails.rb
62
+ - lib/corrails/directlog.rb
63
+ - lib/corrails/correios.rb
64
+ - lib/corrails/model/response.rb
65
+ - lib/corrails/parser/directlog_parser.rb
66
+ - lib/corrails/parser/correios_parser.rb
67
+ homepage: http://rubygems.org/gems/corrails
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.24
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: Gem utilizada para rastrear pedidos da Directlog ou dos Correios
100
+ test_files: []
101
+