correios-sro-xml 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5304bbd81d83f760e5828196f54a4e7bbb5e2fd5
4
+ data.tar.gz: 0492be9b896f68b266b72c979b80ed6dff68d238
5
+ SHA512:
6
+ metadata.gz: 23cc2288024f0dd02ef64425c55b0cfcf5f0902f3befdc1b5662cb1c90e903b2c29b2d6391dfe62659d46d239db9b37138e5d90497f106f05b0ac11991712ac4
7
+ data.tar.gz: 1ffc569c2d430c092d616534a070584a71b7beb0256f049cd8a31288374286612f2524472706a32e95729c13ff91419d5d68f2600013dd7b18e87132efa839e8
data/CHANGELOG.rdoc CHANGED
@@ -1,3 +1,8 @@
1
+ == Version 0.2.0
2
+ - Warning: Minimal required Ruby version from now is 1.9.3.
3
+ - Improvement: Update LogMe gem to version 0.0.6 with support to log request and response messages.
4
+ - Maintenance: Refactory all classes.
5
+
1
6
  == Version 0.1.1
2
7
  - Maintenance: Update .gemspec info.
3
8
 
data/README.rdoc CHANGED
@@ -23,7 +23,7 @@ For development environment you can use follow access data:
23
23
 
24
24
  require 'correios-sro-xml'
25
25
 
26
- sro = Correios::SRO::Tracker.new(:user => "ECT", :password => "SRO")
26
+ sro = Correios::SRO::Tracker.new(user: "ECT", password: "SRO")
27
27
 
28
28
  Tracking a single object:
29
29
  object = sro.get("SI047624825BR")
@@ -89,7 +89,7 @@ For default, the timeout for a request to SRO XML Web Service is <b>5 seconds</b
89
89
  You can configure this timeout using <b>Correios::SRO</b> module.
90
90
 
91
91
  Correios::SRO.configure do |config|
92
- config.request_timeout = 3 # It configures timeout to 3 seconds
92
+ config.request_timeout = 3 # It configures timeout to 3 seconds
93
93
  end
94
94
 
95
95
  === Log
@@ -97,11 +97,11 @@ You can configure this timeout using <b>Correios::SRO</b> module.
97
97
  For default, each request to SRO XML Web service is logged to STDOUT, with <b>:info</b> log level, using the gem {LogMe}[http://github.com/prodis/log-me].
98
98
 
99
99
  Log example:
100
- I, [2012-08-17T00:55:10.531780 #22692] INFO -- : Correios-SRO-XML Request:
100
+ I, [2012-08-17T00:55:10.531780 #22692] INFO -- : [Correios::SRO] Request:
101
101
  POST http://websro.correios.com.br/sro_bin/sroii_xml.eventos
102
102
  Usuario=ECT&Senha=SRO&Tipo=L&Resultado=U&Objetos=PB996681660BR
103
103
 
104
- I, [2012-08-17T00:55:10.750308 #22692] INFO -- : Correios-SRO-XML Response:
104
+ I, [2012-08-17T00:55:10.750308 #22692] INFO -- : [Correios::SRO] Response:
105
105
  HTTP/1.1 200 OK
106
106
  <?xml version="1.0" encoding="iso-8859-1" ?>
107
107
  <sroxml>
@@ -129,66 +129,22 @@ Log example:
129
129
  </objeto>
130
130
  </sroxml>
131
131
 
132
- If you configure log level to <b>:debug</b>, request and response HTTP headers will be logged too.
133
- D, [2012-08-17T00:58:56.226742 #22692] DEBUG -- : Correios-SRO-XML Request:
134
- POST http://websro.correios.com.br/sro_bin/sroii_xml.eventos
135
- accept: */*
136
- user-agent: Ruby
137
- content-type: application/x-www-form-urlencoded
138
- Usuario=ECT&Senha=SRO&Tipo=L&Resultado=U&Objetos=PB996681660BR
139
-
140
- D, [2012-08-17T00:58:56.495131 #22692] DEBUG -- : Correios-SRO-XML Response:
141
- HTTP/1.1 200 OK
142
- date: Fri, 17 Aug 2012 03:58:56 GMT
143
- server: Oracle-Application-Server-10g/10.1.2.0.2 Oracle-HTTP-Server
144
- content-length: 855
145
- content-type: text/html; charset=ISO-8859-1
146
- x-cache: MISS from 127.0.0.1
147
- connection: close
148
- <?xml version="1.0" encoding="iso-8859-1" ?>
149
- <sroxml>
150
- <versao>1.0</versao>
151
- <qtd>1</qtd>
152
- <TipoPesquisa>Lista de Objetos</TipoPesquisa>
153
- <TipoResultado>?ltimo evento</TipoResultado>
154
- <objeto>
155
- <numero>PB996681660BR</numero>
156
- <evento>
157
- <tipo>BDE</tipo>
158
- <status>01</status>
159
- <data>05/07/2012</data>
160
- <hora>21:11</hora>
161
- <descricao>Entregue</descricao>
162
- <recebedor> </recebedor>
163
- <documento> </documento>
164
- <comentario> </comentario>
165
- <local>CDD VILA ANDRADE</local>
166
- <codigo>05724970</codigo>
167
- <cidade>SAO PAULO</cidade>
168
- <uf>SP</uf>
169
- <sto>72824000</sto>
170
- </evento>
171
- </objeto>
172
- </sroxml>
173
-
174
- To disable the log, change log level and configure other log output, use <b>Correios::SRO</b> module:
132
+ To disable the log and configure other log output, use <b>Correios::SRO</b> module:
175
133
 
176
134
  Correios::SRO.configure do |config|
177
135
  config.log_enabled = false # It disables the log
178
- config.log_level = :debug # It changes log level
179
136
  config.logger = Rails.logger # It uses Rails logger
180
137
  end
181
138
 
182
139
  === Configuration example
183
140
 
184
141
  Correios::SRO.configure do |config|
185
- config.log_level = :debug
186
142
  config.logger = Rails.logger
187
143
  config.request_timeout = 3
188
144
  end
189
145
 
190
146
  == Author
191
- - {Fernando Hamasaki (prodis)}[http://prodis.blog.br]
147
+ - {Fernando Hamasaki de Amorim (prodis)}[http://prodis.blog.br]
192
148
 
193
149
  == Collaborators
194
150
  - {Thiago Ganzarolli (tganzarolli)}[https://github.com/tganzarolli]
@@ -213,7 +169,7 @@ To disable the log, change log level and configure other log output, use <b>Corr
213
169
 
214
170
  http://prodis.net.br/images/prodis_150.gif
215
171
 
216
- Copyright (c) 2012 Prodis
172
+ Copyright (c) 2012-2014 Prodis
217
173
 
218
174
  Permission is hereby granted, free of charge, to any person obtaining
219
175
  a copy of this software and associated documentation files (the
@@ -1,30 +1,33 @@
1
- # -*- encoding: utf-8 -*-
2
- $:.push File.expand_path('../lib', __FILE__)
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require 'correios/sro/version'
4
4
 
5
- Gem::Specification.new do |gem|
6
- gem.name = "correios-sro-xml"
7
- gem.version = Correios::SRO::VERSION
8
- gem.authors = ["Prodis a.k.a. Fernando Hamasaki"]
9
- gem.email = ["prodis@gmail.com"]
10
- gem.summary = "Tracking Objects System from Correios - SRO (Sistema de Rastreamento de Objetos dos Correios)."
11
- gem.description = "Tracking Objects System from Correios - SRO (Sistema de Rastreamento de Objetos dos Correios), using SRO XML Web Service, that allows to query up to 50 orders simultaneously."
12
- gem.homepage = "http://github.com/prodis/correios-sro-xml"
13
- gem.licenses = ["MIT"]
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "correios-sro-xml"
7
+ spec.version = Correios::SRO::VERSION
8
+ spec.authors = ["Prodis a.k.a. Fernando Hamasaki de Amorim"]
9
+ spec.email = ["prodis@gmail.com"]
10
+ spec.summary = "Tracking Objects System from Correios - SRO (Sistema de Rastreamento de Objetos dos Correios)."
11
+ spec.description = "Tracking Objects System from Correios - SRO (Sistema de Rastreamento de Objetos dos Correios), using SRO XML Web Service, that allows to query up to 50 orders simultaneously."
12
+ spec.homepage = "http://prodis.blog.br/2012/08/17/correios-sro-xml-gem-para-rastreamento-de-objetos-dos-correios"
13
+ spec.licenses = ["MIT"]
14
14
 
15
- gem.files = `git ls-files`.split($\)
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
15
+ spec.files = `git ls-files`.split($\)
16
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
19
 
20
- gem.platform = Gem::Platform::RUBY
21
- gem.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
20
+ spec.platform = Gem::Platform::RUBY
21
+ spec.required_ruby_version = Gem::Requirement.new(">= 1.9.3")
22
22
 
23
- gem.add_dependency "log-me", "~> 0.0.4"
24
- gem.add_dependency "nokogiri", "~> 1.5"
25
- gem.add_dependency "sax-machine", "~> 0.1"
23
+ spec.add_dependency "log-me", "= 0.0.6"
24
+ spec.add_dependency "nokogiri", "~> 1.6"
25
+ spec.add_dependency "sax-machine", "~> 0.2.1"
26
26
 
27
- gem.add_development_dependency "rake"
28
- gem.add_development_dependency "rspec", "~> 2.11"
29
- gem.add_development_dependency "webmock", "~> 1.8"
27
+ spec.add_development_dependency "coveralls"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec", "~> 2.14"
31
+ spec.add_development_dependency "vcr", "~> 2.8"
32
+ spec.add_development_dependency "webmock", "~> 1.15.2"
30
33
  end
@@ -1,10 +1,8 @@
1
- # encoding: UTF-8
2
- require 'rubygems'
1
+ require 'correios/sro/timeout'
3
2
  require 'correios/sro'
4
3
  require 'correios/sro/destination'
5
4
  require 'correios/sro/event'
6
5
  require 'correios/sro/object'
7
6
  require 'correios/sro/parser'
8
7
  require 'correios/sro/tracker'
9
- require 'correios/sro/string'
10
8
  require 'correios/sro/web_service'
data/lib/correios/sro.rb CHANGED
@@ -1,19 +1,8 @@
1
- # encoding: UTF-8
2
1
  require 'log-me'
3
2
 
4
3
  module Correios
5
4
  module SRO
6
5
  extend LogMe
7
-
8
- module Timeout
9
- DEFAULT_REQUEST_TIMEOUT = 5 #seconds
10
- attr_writer :request_timeout
11
-
12
- def request_timeout
13
- (@request_timeout ||= DEFAULT_REQUEST_TIMEOUT).to_i
14
- end
15
- end
16
-
17
- extend Timeout
6
+ extend Correios::SRO::Timeout
18
7
  end
19
8
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'sax-machine'
3
2
 
4
3
  module Correios
@@ -6,7 +5,7 @@ module Correios
6
5
  class Destination
7
6
  include SAXMachine
8
7
 
9
- element :local, :as => :place
8
+ element :local, :as => :place
10
9
  element :codigo, :as => :code
11
10
  element :cidade, :as => :city
12
11
  element :bairro, :as => :neighborhood
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'sax-machine'
3
2
 
4
3
  module Correios
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'sax-machine'
3
2
 
4
3
  module Correios
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'nokogiri'
3
2
 
4
3
  module Correios
@@ -6,7 +5,7 @@ module Correios
6
5
  class Parser
7
6
  def objects(xml)
8
7
  objects = {}
9
- xml = xml.backward_encode("UTF-8", "ISO-8859-1")
8
+ xml.encode!("UTF-8", "ISO-8859-1")
10
9
 
11
10
  doc = Nokogiri::XML(xml)
12
11
  doc.xpath("//objeto").each do |element|
@@ -0,0 +1,12 @@
1
+ module Correios
2
+ module SRO
3
+ module Timeout
4
+ DEFAULT_REQUEST_TIMEOUT = 5 #seconds
5
+ attr_writer :request_timeout
6
+
7
+ def request_timeout
8
+ (@request_timeout ||= DEFAULT_REQUEST_TIMEOUT).to_i
9
+ end
10
+ end
11
+ end
12
+ end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  module Correios
3
2
  module SRO
4
3
  class Tracker
@@ -6,7 +5,7 @@ module Correios
6
5
  attr_accessor :query_type, :result_mode
7
6
  attr_reader :object_numbers
8
7
 
9
- DEFAULT_OPTIONS = { :query_type => :list, :result_mode => :last }
8
+ DEFAULT_OPTIONS = { query_type: :list, result_mode: :last }.freeze
10
9
 
11
10
  def initialize(options = {})
12
11
  DEFAULT_OPTIONS.merge(options).each do |attr, value|
@@ -1,7 +1,5 @@
1
- # encoding: UTF-8
2
1
  module Correios
3
2
  module SRO
4
- VERSION = "0.1.1"
3
+ VERSION = "0.2.0"
5
4
  end
6
5
  end
7
-
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'net/http'
3
2
  require 'uri'
4
3
 
@@ -6,8 +5,8 @@ module Correios
6
5
  module SRO
7
6
  class WebService
8
7
  URL = "http://websro.correios.com.br/sro_bin/sroii_xml.eventos"
9
- QUERY_TYPES = { :list => "L", :range => "F" }
10
- RESULT_MODES = { :all => "T", :last => "U" }
8
+ QUERY_TYPES = { list: "L", range: "F" }
9
+ RESULT_MODES = { all: "T", last: "U" }
11
10
 
12
11
  def initialize(tracker)
13
12
  @uri = URI.parse(URL)
@@ -18,10 +17,10 @@ module Correios
18
17
  http = build_http
19
18
 
20
19
  request = build_request
21
- log_request(request)
20
+ Correios::SRO.log_request request, @uri.to_s
22
21
 
23
22
  response = http.request(request)
24
- log_response(response)
23
+ Correios::SRO.log_response response
25
24
 
26
25
  response.body
27
26
  end
@@ -42,46 +41,13 @@ module Correios
42
41
 
43
42
  def request_params
44
43
  {
45
- :Usuario => @tracker.user,
46
- :Senha => @tracker.password,
47
- :Tipo => QUERY_TYPES[@tracker.query_type],
48
- :Resultado => RESULT_MODES[@tracker.result_mode],
49
- :Objetos => @tracker.object_numbers.join
44
+ Usuario: @tracker.user,
45
+ Senha: @tracker.password,
46
+ Tipo: QUERY_TYPES[@tracker.query_type],
47
+ Resultado: RESULT_MODES[@tracker.result_mode],
48
+ Objetos: @tracker.object_numbers.join
50
49
  }
51
50
  end
52
-
53
- def log_request(request)
54
- message = format_message(request) do
55
- message = with_line_break { "Correios-SRO-XML Request:" }
56
- message << with_line_break { "POST #{URL}" }
57
- end
58
-
59
- Correios::SRO.log(message)
60
- end
61
-
62
- def log_response(response)
63
- message = format_message(response) do
64
- message = with_line_break { "Correios-SRO-XML Response:" }
65
- message << with_line_break { "HTTP/#{response.http_version} #{response.code} #{response.message}" }
66
- end
67
-
68
- Correios::SRO.log(message)
69
- end
70
-
71
- def format_message(http)
72
- message = yield
73
- message << with_line_break { format_headers_for(http) } if Correios::SRO.log_level == :debug
74
- message << with_line_break { http.body }
75
- end
76
-
77
- def format_headers_for(http)
78
- # I'm using an empty block in each_header method for Ruby 1.8.7 compatibility.
79
- http.each_header{}.map { |name, values| "#{name}: #{values.first}" }.join("\n")
80
- end
81
-
82
- def with_line_break
83
- "#{yield}\n"
84
- end
85
51
  end
86
52
  end
87
53
  end
@@ -1,14 +1,11 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Correios::SRO::Event do
5
- let(:event) { Correios::SRO::Event.new }
6
-
7
4
  [:receiver, :document, :comment].each do |method|
8
5
  describe "##{method}=" do
9
6
  it "strips string" do
10
- event.send("#{method}=", " Texto. ")
11
- event.send(method).should == "Texto."
7
+ subject.send("#{method}=", " Texto. ")
8
+ expect(subject.send(method)).to eql "Texto."
12
9
  end
13
10
  end
14
11
  end
@@ -1,55 +1,53 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Correios::SRO::Parser do
5
4
  describe "#objects" do
6
- let(:xml) { body_for :success_response_many_objects }
7
- let(:parser) { Correios::SRO::Parser.new }
5
+ let(:xml) { Fixture.load :sro_many_objects }
8
6
 
9
7
  it "encodes from ISO-8859-1 to UTF-8" do
10
- xml.should_receive(:backward_encode).with("UTF-8", "ISO-8859-1").and_return(xml)
11
- parser.objects(xml)
8
+ expect(xml).to receive(:encode!).with("UTF-8", "ISO-8859-1")
9
+ subject.objects(xml)
12
10
  end
13
11
 
14
12
  ["SI047624825BR", "SX104110463BR"].each do |number|
15
13
  it "returns object number" do
16
- objects = parser.objects(xml)
17
- objects[number].number.should == number
14
+ objects = subject.objects(xml)
15
+ expect(objects[number].number).to eql number
18
16
  end
19
17
  end
20
18
 
21
19
  context "returns event" do
22
- before(:each) { @objects = parser.objects(xml) }
20
+ before { @objects = subject.objects(xml) }
23
21
 
24
22
  { "SI047624825BR" => {
25
- :type => "BDI",
26
- :status => "01",
27
- :date => "26/12/2011",
28
- :hour => "15:22",
29
- :description => "Entregue",
30
- :receiver => "",
31
- :document => "",
32
- :comment => "?",
33
- :place => "AC CENTRAL DE SAO PAULO",
34
- :code => "01009972",
35
- :city => "SAO PAULO",
36
- :state => "SP",
37
- :sto => "00024419"
23
+ type: "BDI",
24
+ status: "01",
25
+ date: "26/12/2011",
26
+ hour: "15:22",
27
+ description: "Entregue",
28
+ receiver: "",
29
+ document: "",
30
+ comment: "?",
31
+ place: "AC CENTRAL DE SAO PAULO",
32
+ code: "01009972",
33
+ city: "SAO PAULO",
34
+ state: "SP",
35
+ sto: "00024419"
38
36
  },
39
37
  "SX104110463BR" => {
40
- :type => "BDE",
41
- :status => "01",
42
- :date => "08/12/2011",
43
- :hour => "09:30",
44
- :description => "Entregue",
45
- :receiver => "",
46
- :document => "",
47
- :comment => "",
48
- :place => "CEE JUNDIAI",
49
- :code => "13211970",
50
- :city => "JUNDIAI",
51
- :state => "SP",
52
- :sto => "74654209"
38
+ type: "BDE",
39
+ status: "01",
40
+ date: "08/12/2011",
41
+ hour: "09:30",
42
+ description: "Entregue",
43
+ receiver: "",
44
+ document: "",
45
+ comment: "",
46
+ place: "CEE JUNDIAI",
47
+ code: "13211970",
48
+ city: "JUNDIAI",
49
+ state: "SP",
50
+ sto: "74654209"
53
51
  },
54
52
  }.each do |number, first_event|
55
53
  first_event.each do |attr, value|
@@ -62,27 +60,27 @@ describe Correios::SRO::Parser do
62
60
  end
63
61
 
64
62
  context "returns destination" do
65
- before(:each) { @objects = parser.objects(xml) }
63
+ before { @objects = subject.objects(xml) }
66
64
 
67
65
  { "SI047624825BR" => {
68
- :place => "CTE VILA MARIA",
69
- :code => "02170975",
70
- :city => "SAO PAULO",
71
- :neighborhood => "PQ NOVO MUNDO",
72
- :state => "SP"
66
+ place: "CTE VILA MARIA",
67
+ code: "02170975",
68
+ city: "SAO PAULO",
69
+ neighborhood: "PQ NOVO MUNDO",
70
+ state: "SP"
73
71
  },
74
72
  "SX104110463BR" => {
75
- :place => "CTE CAMPINAS",
76
- :code => "13050971",
77
- :city => "VALINHOS",
78
- :neighborhood => "MACUCO",
79
- :state => "SP"
73
+ place: "CTE CAMPINAS",
74
+ code: "13050971",
75
+ city: "VALINHOS",
76
+ neighborhood: "MACUCO",
77
+ state: "SP"
80
78
  },
81
79
  }.each do |number, destinations|
82
80
  destinations.each do |attr, value|
83
81
  it attr do
84
82
  destination = @objects[number].events[3].destination
85
- destination.send(attr).should == value
83
+ expect(destination.send(attr)).to eql value
86
84
  end
87
85
  end
88
86
  end
@@ -1,31 +1,29 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Correios::SRO::Tracker do
5
4
  describe ".new" do
6
5
  it "creates with default values" do
7
- sro = Correios::SRO::Tracker.new
8
- sro.query_type.should == :list
9
- sro.result_mode.should == :last
10
- sro.object_numbers.should == []
6
+ expect(subject.query_type).to eql :list
7
+ expect(subject.result_mode).to eql :last
8
+ expect(subject.object_numbers).to eql []
11
9
  end
12
10
 
13
- { :user => "PRODIS",
14
- :password => "pim321",
15
- :query_type => :range,
16
- :result_mode => :all
11
+ { user: "PRODIS",
12
+ password: "pim321",
13
+ query_type: :range,
14
+ result_mode: :all
17
15
  }.each do |attr, value|
18
16
  context "when #{attr} is supplied" do
19
17
  it "sets #{attr}" do
20
18
  sro = Correios::SRO::Tracker.new(attr => value)
21
- sro.send(attr).should == value
19
+ expect(sro.send(attr)).to eql value
22
20
  end
23
21
  end
24
22
 
25
23
  context "when #{attr} is supplied in a block" do
26
24
  it "sets #{attr}" do
27
25
  sro = Correios::SRO::Tracker.new { |t| t.send("#{attr}=", value) }
28
- sro.send(attr).should == value
26
+ expect(sro.send(attr)).to eql value
29
27
  end
30
28
  end
31
29
  end
@@ -38,77 +36,76 @@ describe Correios::SRO::Tracker do
38
36
  Correios::SRO.configure { |config| config.log_enabled = true }
39
37
  end
40
38
 
41
- let(:sro) { Correios::SRO::Tracker.new(:user => "PRODIS", :password => "pim321") }
39
+ let(:subject) { Correios::SRO::Tracker.new(user: "PRODIS", password: "pim321") }
40
+ let(:response) { "" }
41
+ before { Correios::SRO::WebService.any_instance.stub(:request!).and_return(response) }
42
42
 
43
43
  context "to many objects" do
44
- before(:each) { mock_request_for(:success_response_many_objects) }
44
+ let(:response) { Fixture.load :sro_many_objects }
45
45
 
46
46
  it "sets objects numbers" do
47
- sro.get("SI047624825BR", "SX104110463BR")
48
- sro.object_numbers.size.should == 2
49
- sro.object_numbers.first.should == "SI047624825BR"
50
- sro.object_numbers.last.should == "SX104110463BR"
51
- end
47
+ subject.get("SI047624825BR", "SX104110463BR")
52
48
 
53
- it "creates a WebService with correct params" do
54
- web_service = Correios::SRO::WebService.new(sro)
55
- Correios::SRO::WebService.should_receive(:new).with(sro).and_return(web_service)
56
- sro.get("SI047624825BR", "SX104110463BR")
49
+ expect(subject.object_numbers.size).to eql 2
50
+ expect(subject.object_numbers.first).to eql "SI047624825BR"
51
+ expect(subject.object_numbers.last).to eql "SX104110463BR"
57
52
  end
58
53
 
59
54
  it "returns all objects" do
60
- objects = sro.get("SI047624825BR", "SX104110463BR")
61
- objects.size.should == 2
62
- objects["SI047624825BR"].number.should == "SI047624825BR"
63
- objects["SX104110463BR"].number.should == "SX104110463BR"
55
+ objects = subject.get("SI047624825BR", "SX104110463BR")
56
+
57
+ expect(objects.size).to eql 2
58
+
59
+ expect(objects["SI047624825BR"].number).to eql "SI047624825BR"
60
+ expect(objects["SI047624825BR"].events.first.description).to eql "Entregue"
61
+
62
+ expect(objects["SX104110463BR"].number).to eql "SX104110463BR"
63
+ expect(objects["SX104110463BR"].events.first.description).to eql "Entregue"
64
64
  end
65
65
 
66
66
  context "when only one object found" do
67
- before(:each) { mock_request_for(:success_response_one_object) }
67
+ let(:response) { Fixture.load :sro_one_object }
68
68
 
69
69
  it "returns a Hash" do
70
- objects = sro.get("SI047624825BR", "SX104110463BR")
71
- objects.should be_an_instance_of Hash
70
+ objects = subject.get("SI047624825BR", "SX104110463BR")
71
+ expect(objects).to be_an_instance_of Hash
72
72
  end
73
73
 
74
74
  it "returns the object found" do
75
- objects = sro.get("SI047624825BR", "SX104110463BR")
76
- objects.size.should == 1
77
- objects["SI047624825BR"].number.should == "SI047624825BR"
75
+ objects = subject.get("SI047624825BR", "SX104110463BR")
76
+
77
+ expect(objects.size).to eql 1
78
+ expect(objects["SI047624825BR"].number).to eql "SI047624825BR"
79
+ expect(objects["SI047624825BR"].events.first.description).to eql "Entregue"
78
80
  end
79
81
 
80
82
  it "returns nil in object not found" do
81
- objects = sro.get("SI047624825BR", "SX104110463BR")
82
- objects["SX104110463BR"].should be_nil
83
+ objects = subject.get("SI047624825BR", "SX104110463BR")
84
+ expect(objects["SX104110463BR"]).to be_nil
83
85
  end
84
86
  end
85
87
  end
86
88
 
87
89
  context "to one object" do
88
- before(:each) { mock_request_for(:success_response_one_object) }
90
+ let(:response) { Fixture.load :sro_one_object }
89
91
 
90
92
  it "sets object number" do
91
- sro.get("SI047624825BR")
92
- sro.object_numbers.size.should == 1
93
- sro.object_numbers.first.should == "SI047624825BR"
94
- end
95
-
96
- it "creates a WebService with correct params" do
97
- web_service = Correios::SRO::WebService.new(sro)
98
- Correios::SRO::WebService.should_receive(:new).with(sro).and_return(web_service)
99
- sro.get("SI047624825BR")
93
+ subject.get("SI047624825BR")
94
+ expect(subject.object_numbers.size).to eql 1
95
+ expect(subject.object_numbers.first).to eql "SI047624825BR"
100
96
  end
101
97
 
102
98
  it "returns only one object" do
103
- object = sro.get("SI047624825BR")
104
- object.number.should == "SI047624825BR"
99
+ object = subject.get("SI047624825BR")
100
+ expect(object.number).to eql "SI047624825BR"
101
+ expect(object.events.first.description).to eql "Entregue"
105
102
  end
106
103
 
107
104
  context "when object not found" do
105
+ let(:response) { Fixture.load :sro_not_found }
106
+
108
107
  it "returns nil" do
109
- mock_request_for(:failure_response_not_found)
110
- object = sro.get("SI047624825BR")
111
- object.should be_nil
108
+ expect(subject.get("SI047624825BR")).to be_nil
112
109
  end
113
110
  end
114
111
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Correios::SRO::WebService do
@@ -9,12 +8,15 @@ describe Correios::SRO::WebService do
9
8
  Correios::SRO.configure { |config| config.log_enabled = true }
10
9
  end
11
10
 
12
- let(:tracker) { Correios::SRO::Tracker.new }
13
- let(:web_service) { Correios::SRO::WebService.new(tracker) }
11
+ let(:tracker) do
12
+ sro = Correios::SRO::Tracker.new(user: "ECT", password: "SRO")
13
+ sro.instance_variable_set :@object_numbers, ["SS123456789BR"]
14
+ sro
15
+ end
16
+ let(:subject) { Correios::SRO::WebService.new(tracker) }
14
17
 
15
- it "returns XML response" do
16
- mock_request_for("<xml><fake></fake>")
17
- web_service.request!.should eql "<xml><fake></fake>"
18
+ it "returns XML response", vcr: { cassette_name: "sro_found_last" } do
19
+ expect(subject.request!).to include("<numero>SS123456789BR</numero>")
18
20
  end
19
21
  end
20
22
  end
@@ -1,4 +1,3 @@
1
- # encoding: UTF-8
2
1
  require 'spec_helper'
3
2
 
4
3
  describe Correios::SRO do
@@ -0,0 +1,57 @@
1
+ ---
2
+ http_interactions:
3
+ - request:
4
+ method: post
5
+ uri: http://websro.correios.com.br/sro_bin/sroii_xml.eventos
6
+ body:
7
+ encoding: US-ASCII
8
+ string: Usuario=ECT&Senha=SRO&Tipo=L&Resultado=U&Objetos=SS123456789BR
9
+ headers:
10
+ Accept-Encoding:
11
+ - gzip;q=1.0,deflate;q=0.6,identity;q=0.3
12
+ Accept:
13
+ - "*/*"
14
+ User-Agent:
15
+ - Ruby
16
+ Content-Type:
17
+ - application/x-www-form-urlencoded
18
+ response:
19
+ status:
20
+ code: 200
21
+ message: OK
22
+ headers:
23
+ Date:
24
+ - Mon, 24 Feb 2014 01:12:34 GMT
25
+ Server:
26
+ - Oracle-Application-Server-10g/10.1.2.0.2 Oracle-HTTP-Server
27
+ Content-Length:
28
+ - '850'
29
+ Content-Type:
30
+ - text/html; charset=ISO-8859-1
31
+ X-Cache:
32
+ - MISS from 127.0.0.1
33
+ body:
34
+ encoding: ASCII-8BIT
35
+ string: !binary |-
36
+ PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSIgPz4K
37
+ PHNyb3htbD4KICAgPHZlcnNhbz4xLjA8L3ZlcnNhbz4KICAgPHF0ZD4xPC9x
38
+ dGQ+CiAgIDxUaXBvUGVzcXVpc2E+TGlzdGEgZGUgT2JqZXRvczwvVGlwb1Bl
39
+ c3F1aXNhPgogICA8VGlwb1Jlc3VsdGFkbz7abHRpbW8gZXZlbnRvPC9UaXBv
40
+ UmVzdWx0YWRvPgogICAgIDxvYmpldG8+CiAgICAgICA8bnVtZXJvPlNTMTIz
41
+ NDU2Nzg5QlI8L251bWVybz4KICAgICAgIDxldmVudG8+CiAgICAgICAgICA8
42
+ dGlwbz5STzwvdGlwbz4KICAgICAgICAgIDxzdGF0dXM+MDE8L3N0YXR1cz4K
43
+ ICAgICAgICAgIDxkYXRhPjE3LzA5LzIwMTM8L2RhdGE+CiAgICAgICAgICA8
44
+ aG9yYT4xMzo0ODwvaG9yYT4KICAgICAgICAgIDxkZXNjcmljYW8+RW5jYW1p
45
+ bmhhZG88L2Rlc2NyaWNhbz4KICAgICAgICAgIDxsb2NhbD5MQUJPUkFUT1JJ
46
+ TzAxLVNST0lJL0NFPC9sb2NhbD4KICAgICAgICAgIDxjb2RpZ28+NjAwMDA5
47
+ MDE8L2NvZGlnbz4KICAgICAgICAgIDxjaWRhZGU+Rk9SVEFMRVpBPC9jaWRh
48
+ ZGU+CiAgICAgICAgICA8dWY+Q0U8L3VmPgogICAgICAgICAgPHN0bz45OTk5
49
+ OTk5OTwvc3RvPgogICAgICAgPGRlc3Rpbm8+CiAgICAgICAgICA8bG9jYWw+
50
+ TEFCT1JBVE9SSU8wMi1TUk9JSS9DRTwvbG9jYWw+CiAgICAgICAgICA8Y29k
51
+ aWdvPjYwMDAwOTAyPC9jb2RpZ28+CiAgICAgICAgICA8Y2lkYWRlPkZPUlRB
52
+ TEVaQTwvY2lkYWRlPgogICAgICAgICAgPGJhaXJybz5BTERFT1RBPC9iYWly
53
+ cm8+CiAgICAgICAgICA8dWY+Q0U8L3VmPgogICAgICAgPC9kZXN0aW5vPgog
54
+ ICAgICA8L2V2ZW50bz4KICAgICA8L29iamV0bz4KPC9zcm94bWw+Cg==
55
+ http_version:
56
+ recorded_at: Mon, 24 Feb 2014 01:12:40 GMT
57
+ recorded_with: VCR 2.8.0
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,21 @@
1
1
  require 'correios-sro-xml'
2
- require 'webmock/rspec'
2
+ require 'coveralls'
3
+ require 'vcr'
4
+ require 'support/fixture'
3
5
 
4
- # Requires supporting files with custom matchers and macros, etc,
5
- # in ./support/ and its subdirectories.
6
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
6
+ Coveralls.wear!
7
7
 
8
8
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
9
9
  RSpec.configure do |config|
10
+ config.treat_symbols_as_metadata_keys_with_true_values = true
11
+ config.run_all_when_everything_filtered = true
12
+ config.filter_run :focus
10
13
  config.order = "random"
11
14
  end
12
15
 
13
- WebMock.disable_net_connect!
16
+ VCR.configure do |config|
17
+ config.default_cassette_options = { :match_requests_on => [:uri, :method, :body] }
18
+ config.cassette_library_dir = 'spec/fixtures/cassettes'
19
+ config.hook_into :webmock
20
+ config.configure_rspec_metadata!
21
+ end
@@ -0,0 +1,21 @@
1
+ class Fixture
2
+ class << self
3
+ def load(name)
4
+ case name
5
+ when :sro_one_object,
6
+ :sro_many_objects,
7
+ :sro_many_objects_international,
8
+ :sro_not_found
9
+ read_file_for name
10
+ else
11
+ raise ArgumentError, "Fixture '#{name}' not found."
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def read_file_for(filename)
18
+ File.open("#{File.expand_path("../../", __FILE__)}/fixtures/#{filename}.xml").read
19
+ end
20
+ end
21
+ end
metadata CHANGED
@@ -1,82 +1,141 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: correios-sro-xml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
5
- prerelease:
4
+ version: 0.2.0
6
5
  platform: ruby
7
6
  authors:
8
- - Prodis a.k.a. Fernando Hamasaki
7
+ - Prodis a.k.a. Fernando Hamasaki de Amorim
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-08-17 00:00:00.000000000 Z
11
+ date: 2014-02-25 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: log-me
16
- requirement: &14781180 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ~>
17
+ - - '='
20
18
  - !ruby/object:Gem::Version
21
- version: 0.0.4
19
+ version: 0.0.6
22
20
  type: :runtime
23
21
  prerelease: false
24
- version_requirements: *14781180
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.6
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: nokogiri
27
- requirement: &14780950 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
- - - ~>
31
+ - - "~>"
31
32
  - !ruby/object:Gem::Version
32
- version: '1.5'
33
+ version: '1.6'
33
34
  type: :runtime
34
35
  prerelease: false
35
- version_requirements: *14780950
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
36
41
  - !ruby/object:Gem::Dependency
37
42
  name: sax-machine
38
- requirement: &14780720 !ruby/object:Gem::Requirement
39
- none: false
43
+ requirement: !ruby/object:Gem::Requirement
40
44
  requirements:
41
- - - ~>
45
+ - - "~>"
42
46
  - !ruby/object:Gem::Version
43
- version: '0.1'
47
+ version: 0.2.1
44
48
  type: :runtime
45
49
  prerelease: false
46
- version_requirements: *14780720
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.2.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: coveralls
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
47
83
  - !ruby/object:Gem::Dependency
48
84
  name: rake
49
- requirement: &14780530 !ruby/object:Gem::Requirement
50
- none: false
85
+ requirement: !ruby/object:Gem::Requirement
51
86
  requirements:
52
- - - ! '>='
87
+ - - ">="
53
88
  - !ruby/object:Gem::Version
54
89
  version: '0'
55
90
  type: :development
56
91
  prerelease: false
57
- version_requirements: *14780530
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
58
97
  - !ruby/object:Gem::Dependency
59
98
  name: rspec
60
- requirement: &14780260 !ruby/object:Gem::Requirement
61
- none: false
99
+ requirement: !ruby/object:Gem::Requirement
62
100
  requirements:
63
- - - ~>
101
+ - - "~>"
64
102
  - !ruby/object:Gem::Version
65
- version: '2.11'
103
+ version: '2.14'
66
104
  type: :development
67
105
  prerelease: false
68
- version_requirements: *14780260
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '2.14'
111
+ - !ruby/object:Gem::Dependency
112
+ name: vcr
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.8'
69
125
  - !ruby/object:Gem::Dependency
70
126
  name: webmock
71
- requirement: &14780010 !ruby/object:Gem::Requirement
72
- none: false
127
+ requirement: !ruby/object:Gem::Requirement
73
128
  requirements:
74
- - - ~>
129
+ - - "~>"
75
130
  - !ruby/object:Gem::Version
76
- version: '1.8'
131
+ version: 1.15.2
77
132
  type: :development
78
133
  prerelease: false
79
- version_requirements: *14780010
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.15.2
80
139
  description: Tracking Objects System from Correios - SRO (Sistema de Rastreamento
81
140
  de Objetos dos Correios), using SRO XML Web Service, that allows to query up to
82
141
  50 orders simultaneously.
@@ -86,8 +145,8 @@ executables: []
86
145
  extensions: []
87
146
  extra_rdoc_files: []
88
147
  files:
89
- - .gitignore
90
- - .rspec
148
+ - ".gitignore"
149
+ - ".rspec"
91
150
  - CHANGELOG.rdoc
92
151
  - Gemfile
93
152
  - README.rdoc
@@ -99,7 +158,7 @@ files:
99
158
  - lib/correios/sro/event.rb
100
159
  - lib/correios/sro/object.rb
101
160
  - lib/correios/sro/parser.rb
102
- - lib/correios/sro/string.rb
161
+ - lib/correios/sro/timeout.rb
103
162
  - lib/correios/sro/tracker.rb
104
163
  - lib/correios/sro/version.rb
105
164
  - lib/correios/sro/web_service.rb
@@ -109,39 +168,36 @@ files:
109
168
  - spec/correios/sro/tracker_spec.rb
110
169
  - spec/correios/sro/web_service_spec.rb
111
170
  - spec/correios/sro_spec.rb
171
+ - spec/fixtures/cassettes/sro_found_last.yml
172
+ - spec/fixtures/sro_many_objects.xml
173
+ - spec/fixtures/sro_many_objects_international.xml
174
+ - spec/fixtures/sro_not_found.xml
175
+ - spec/fixtures/sro_one_object.xml
112
176
  - spec/spec_helper.rb
113
- - spec/support/mock_request.rb
114
- - spec/support/responses/failure_response_not_found.xml
115
- - spec/support/responses/success_response_many_objects.xml
116
- - spec/support/responses/success_response_many_objects_international.xml
117
- - spec/support/responses/success_response_one_object.xml
118
- homepage: http://github.com/prodis/correios-sro-xml
177
+ - spec/support/fixture.rb
178
+ homepage: http://prodis.blog.br/2012/08/17/correios-sro-xml-gem-para-rastreamento-de-objetos-dos-correios
119
179
  licenses:
120
180
  - MIT
181
+ metadata: {}
121
182
  post_install_message:
122
183
  rdoc_options: []
123
184
  require_paths:
124
185
  - lib
125
186
  required_ruby_version: !ruby/object:Gem::Requirement
126
- none: false
127
187
  requirements:
128
- - - ! '>='
188
+ - - ">="
129
189
  - !ruby/object:Gem::Version
130
- version: 1.8.7
190
+ version: 1.9.3
131
191
  required_rubygems_version: !ruby/object:Gem::Requirement
132
- none: false
133
192
  requirements:
134
- - - ! '>='
193
+ - - ">="
135
194
  - !ruby/object:Gem::Version
136
195
  version: '0'
137
- segments:
138
- - 0
139
- hash: -695408557
140
196
  requirements: []
141
197
  rubyforge_project:
142
- rubygems_version: 1.8.16
198
+ rubygems_version: 2.2.2
143
199
  signing_key:
144
- specification_version: 3
200
+ specification_version: 4
145
201
  summary: Tracking Objects System from Correios - SRO (Sistema de Rastreamento de Objetos
146
202
  dos Correios).
147
203
  test_files:
@@ -150,9 +206,10 @@ test_files:
150
206
  - spec/correios/sro/tracker_spec.rb
151
207
  - spec/correios/sro/web_service_spec.rb
152
208
  - spec/correios/sro_spec.rb
209
+ - spec/fixtures/cassettes/sro_found_last.yml
210
+ - spec/fixtures/sro_many_objects.xml
211
+ - spec/fixtures/sro_many_objects_international.xml
212
+ - spec/fixtures/sro_not_found.xml
213
+ - spec/fixtures/sro_one_object.xml
153
214
  - spec/spec_helper.rb
154
- - spec/support/mock_request.rb
155
- - spec/support/responses/failure_response_not_found.xml
156
- - spec/support/responses/success_response_many_objects.xml
157
- - spec/support/responses/success_response_many_objects_international.xml
158
- - spec/support/responses/success_response_one_object.xml
215
+ - spec/support/fixture.rb
@@ -1,10 +0,0 @@
1
- class String
2
- def backward_encode(to, from)
3
- if self.respond_to?(:encode)
4
- self.encode(to, from)
5
- else
6
- require 'iconv'
7
- Iconv.conv(to, from, self)
8
- end
9
- end
10
- end
@@ -1,22 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- def mock_request_for(response)
4
- url = Regexp.new("http://websro.correios.com.br/sro_bin/sroii_xml.eventos")
5
- WebMock::API.stub_request(:post, url).to_return(:status => 200, :body => body_for(response))
6
- end
7
-
8
- def body_for(response)
9
- case response
10
- when :success_response_one_object,
11
- :success_response_many_objects,
12
- :success_response_many_objects_international,
13
- :failure_response_not_found
14
- read_file_for(response)
15
- else
16
- response
17
- end
18
- end
19
-
20
- def read_file_for(filename)
21
- File.open("#{File.dirname(__FILE__)}/responses/#{filename}.xml").read
22
- end