dhl-get_quote 0.5.3 → 0.5.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +49 -0
- data/dhl-get_quote.gemspec +2 -2
- data/lib/dhl-get_quote.rb +48 -0
- data/lib/dhl/get_quote/errors.rb +4 -1
- data/lib/dhl/get_quote/request.rb +47 -4
- data/lib/dhl/get_quote/version.rb +6 -1
- data/spec/lib/dhl/dhl-get_quote_spec.rb +128 -24
- data/spec/lib/dhl/get_quote/request_spec.rb +113 -6
- metadata +127 -111
data/README.md
CHANGED
@@ -328,6 +328,55 @@ The #name() method will return the name for a given service. It will work on bot
|
|
328
328
|
|
329
329
|
---
|
330
330
|
|
331
|
+
### Setting a logger and log levels
|
332
|
+
|
333
|
+
By default the gem will only log output of fatal errors that occur when communicating with the DHL servers. If such an error is caught, the gem will log the exception name, the request XML (if generated) and the response xml (if received).
|
334
|
+
|
335
|
+
To change the log level:
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
Dhl::GetQuote::configure do |c|
|
339
|
+
c.set_log_level :verbose
|
340
|
+
end
|
341
|
+
|
342
|
+
# or
|
343
|
+
|
344
|
+
Dhl::GetQuote::set_log_level :verbose
|
345
|
+
```
|
346
|
+
|
347
|
+
Available log levels are:
|
348
|
+
|
349
|
+
:none Logs nothing
|
350
|
+
:critical Logs fatal exceptions (DEFAULT)
|
351
|
+
:verbose Log :critical, also logs internal validation errors
|
352
|
+
:debug Log everything
|
353
|
+
|
354
|
+
The default logger is STDERR. You can change this by passing a Proc object to set_logger(). For example, if you wanted to log to the Rails Logger instead:
|
355
|
+
|
356
|
+
```ruby
|
357
|
+
# with a block
|
358
|
+
Dhl::GetQuote::set_logger do
|
359
|
+
Proc.new do |message, log_level|
|
360
|
+
Rails.logger.info(message)
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
# as an argument
|
365
|
+
logger = Proc.new { |message, log_level| Rails.logger.info(message) }
|
366
|
+
Dhl::GetQuote::set_logger(logger)
|
367
|
+
|
368
|
+
# you can also do this is the configure block:
|
369
|
+
Dhl::GetQuote::configure do |c|
|
370
|
+
c.set_logger(
|
371
|
+
Proc.new { |message, log_level| Rails.logger.info(message) }
|
372
|
+
)
|
373
|
+
end
|
374
|
+
```
|
375
|
+
|
376
|
+
Log level CAN NOT be set via "Dhl::GetQuote::new()" options.
|
377
|
+
|
378
|
+
---
|
379
|
+
|
331
380
|
### Initializers with Dhl::GetQuote
|
332
381
|
|
333
382
|
If you don't want to have to pass email, password, weight setting, etc, every time you build a new request object you can set these defaults beforehand. This works well in cases where you want to put setting in something like a Rails initializer.
|
data/dhl-get_quote.gemspec
CHANGED
@@ -20,11 +20,11 @@ Gem::Specification.new do |gem|
|
|
20
20
|
gem.add_dependency 'httparty', '~>0.10.2'
|
21
21
|
gem.add_dependency 'multi_xml', '~>0.5.3'
|
22
22
|
|
23
|
-
gem.add_development_dependency 'rake', '~>10.0.4'
|
24
23
|
gem.add_development_dependency 'rake', '10.0.4'
|
24
|
+
# gem.add_development_dependency 'rspec', '2.14.1'
|
25
25
|
gem.add_development_dependency 'rspec', '2.13.0'
|
26
26
|
gem.add_development_dependency 'rspec-must', '0.0.1'
|
27
|
-
gem.add_development_dependency 'timecop', '
|
27
|
+
gem.add_development_dependency 'timecop', '0.6.1'
|
28
28
|
# gem.add_development_dependency 'debugger'
|
29
29
|
|
30
30
|
if Dhl::GetQuote::PostInstallMessage
|
data/lib/dhl-get_quote.rb
CHANGED
@@ -13,6 +13,8 @@ class Dhl
|
|
13
13
|
|
14
14
|
DIMENSIONS_UNIT_CODES = { :centimeters => "CM", :inches => "IN" }
|
15
15
|
WEIGHT_UNIT_CODES = { :kilograms => "KG", :pounds => "LB" }
|
16
|
+
LOG_LEVELS = [:debug, :verbose, :critical, :none]
|
17
|
+
DEFAULT_LOG_LEVEL = :critical
|
16
18
|
|
17
19
|
def self.configure(&block)
|
18
20
|
yield self if block_given?
|
@@ -93,10 +95,56 @@ class Dhl
|
|
93
95
|
@@dimensions_unit = DIMENSIONS_UNIT_CODES[:centimeters]
|
94
96
|
@@dutiable = false
|
95
97
|
@@test_mode = false
|
98
|
+
|
99
|
+
@@logger = self.default_logger
|
100
|
+
@@log_level = DEFAULT_LOG_LEVEL
|
101
|
+
end
|
102
|
+
|
103
|
+
def self.log(message, level = DEFAULT_LOG_LEVEL)
|
104
|
+
validate_log_level!(level)
|
105
|
+
return unless LOG_LEVELS.index(level.to_sym) >= LOG_LEVELS.index(log_level)
|
106
|
+
get_logger.call(message, level)
|
107
|
+
end
|
108
|
+
|
109
|
+
def self.set_logger(logger_proc=nil, &block)
|
110
|
+
@@logger = block || logger_proc || default_logger
|
111
|
+
end
|
112
|
+
|
113
|
+
def self.get_logger
|
114
|
+
@@logger
|
115
|
+
end
|
116
|
+
|
117
|
+
def self.set_log_level(log_level)
|
118
|
+
validate_log_level!(log_level)
|
119
|
+
@@log_level = log_level
|
120
|
+
end
|
121
|
+
|
122
|
+
def self.log_level
|
123
|
+
@@log_level
|
96
124
|
end
|
97
125
|
|
98
126
|
private
|
99
127
|
|
128
|
+
def self.validate_log_level!(level)
|
129
|
+
raise "Log level :#{level} is not valid" unless
|
130
|
+
valid_log_level?(level)
|
131
|
+
end
|
132
|
+
|
133
|
+
def self.valid_log_level?(level)
|
134
|
+
LOG_LEVELS.include?(level.to_sym)
|
135
|
+
end
|
136
|
+
|
137
|
+
def self.default_logger
|
138
|
+
@default_logger ||= Proc.new do |m, ll|
|
139
|
+
output = if (lines = m.split("\n")).size < 2
|
140
|
+
m
|
141
|
+
else
|
142
|
+
"\n" + lines.map{|l| "\t#{l}"}.join("\n")
|
143
|
+
end
|
144
|
+
STDERR.puts "#{ll.to_s.upcase}: Dhl-get_quote gem: #{output}"
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
100
148
|
def self.deprication_notice(meth, m)
|
101
149
|
messages = {
|
102
150
|
:metric => "Method replaced by Dhl::GetQuote#metic_measurements!(). I am now setting your measurements to metric",
|
data/lib/dhl/get_quote/errors.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
class Dhl::GetQuote
|
2
|
-
class InputError < StandardError
|
2
|
+
class InputError < StandardError
|
3
|
+
def log_level; :verbose; end # by default we won't log these
|
4
|
+
end
|
3
5
|
class OptionsError < InputError; end
|
4
6
|
class FromNotSetError < InputError; end
|
5
7
|
class ToNotSetError < InputError; end
|
@@ -7,6 +9,7 @@ class Dhl::GetQuote
|
|
7
9
|
class PieceError < InputError; end
|
8
10
|
|
9
11
|
class Upstream < StandardError
|
12
|
+
def log_level; :critical; end # by default we will log these
|
10
13
|
class UnknownError < Upstream; end
|
11
14
|
class ValidationFailureError < Upstream; end
|
12
15
|
class ConditionError < Upstream
|
@@ -149,7 +149,7 @@ class Dhl::GetQuote::Request
|
|
149
149
|
|
150
150
|
def to_xml
|
151
151
|
validate!
|
152
|
-
ERB.new(File.new(xml_template_path).read, nil,'%<>-').result(binding)
|
152
|
+
@to_xml = ERB.new(File.new(xml_template_path).read, nil,'%<>-').result(binding)
|
153
153
|
end
|
154
154
|
|
155
155
|
# ready times are only 8a-5p(17h)
|
@@ -164,7 +164,7 @@ class Dhl::GetQuote::Request
|
|
164
164
|
# ready dates are only mon-fri
|
165
165
|
def ready_date(t=Time.now)
|
166
166
|
date = Date.parse(t.to_s)
|
167
|
-
if (date.cwday >= 6) || (date.
|
167
|
+
if (date.cwday >= 6) || (date.cwday >= 5 && t.hour >= 17)
|
168
168
|
date.send(:next_day, 8-date.cwday)
|
169
169
|
else
|
170
170
|
date
|
@@ -172,13 +172,33 @@ class Dhl::GetQuote::Request
|
|
172
172
|
end
|
173
173
|
|
174
174
|
def post
|
175
|
-
validate!
|
176
175
|
response = HTTParty.post(servlet_url,
|
177
176
|
:body => to_xml,
|
178
177
|
:headers => { 'Content-Type' => 'application/xml' }
|
179
178
|
).response
|
180
179
|
|
181
|
-
Dhl::GetQuote::Response.new(response.body)
|
180
|
+
return Dhl::GetQuote::Response.new(response.body)
|
181
|
+
rescue Exception => e
|
182
|
+
request_xml = if @to_xml.to_s.size>0
|
183
|
+
@to_xml
|
184
|
+
else
|
185
|
+
'<not generated at time of error>'
|
186
|
+
end
|
187
|
+
|
188
|
+
response_body = if (response && response.body && response.body.to_s.size > 0)
|
189
|
+
response.body
|
190
|
+
else
|
191
|
+
'<not received at time of error>'
|
192
|
+
end
|
193
|
+
|
194
|
+
log_level = if e.respond_to?(:log_level)
|
195
|
+
e.log_level
|
196
|
+
else
|
197
|
+
:critical
|
198
|
+
end
|
199
|
+
|
200
|
+
log_request_and_response_xml(log_level, e, request_xml, response_body )
|
201
|
+
raise e
|
182
202
|
end
|
183
203
|
|
184
204
|
def special_services
|
@@ -237,4 +257,27 @@ private
|
|
237
257
|
}
|
238
258
|
puts "!!!! Method \"##{meth}()\" is depricated. #{messages[m.to_sym]}."
|
239
259
|
end
|
260
|
+
|
261
|
+
def log_request_and_response_xml(level, exception, request_xml, response_xml)
|
262
|
+
log_exception(exception, level)
|
263
|
+
log_request_xml(request_xml, level)
|
264
|
+
log_response_xml(response_xml, level)
|
265
|
+
end
|
266
|
+
|
267
|
+
def log_exception(exception, level)
|
268
|
+
log("Exception: #{exception}", level)
|
269
|
+
end
|
270
|
+
|
271
|
+
def log_request_xml(xml, level)
|
272
|
+
log("Request XML: #{xml}", level)
|
273
|
+
end
|
274
|
+
|
275
|
+
def log_response_xml(xml, level)
|
276
|
+
log("Response XML: #{xml}", level)
|
277
|
+
end
|
278
|
+
|
279
|
+
def log(msg, level)
|
280
|
+
Dhl::GetQuote.log(msg, level)
|
281
|
+
end
|
282
|
+
|
240
283
|
end
|
@@ -1,11 +1,16 @@
|
|
1
1
|
class Dhl
|
2
2
|
class GetQuote
|
3
|
-
VERSION = "0.5.
|
3
|
+
VERSION = "0.5.4"
|
4
4
|
|
5
5
|
PostInstallMessage = <<EOS
|
6
6
|
|
7
7
|
*** NOTE Dhl-GetQuote ***
|
8
8
|
|
9
|
+
This version introduces the following changes from 0.5.3:
|
10
|
+
|
11
|
+
* Logging of request and response in the event of an error
|
12
|
+
* Logging levels and logging method can be set
|
13
|
+
|
9
14
|
This version introduces the following changes from 0.4.x:
|
10
15
|
|
11
16
|
* #inches!, #pounds!, #kilograms! and #centimeters have been depricated.
|
@@ -22,11 +22,13 @@ describe Dhl::GetQuote do
|
|
22
22
|
describe ".configure" do
|
23
23
|
|
24
24
|
it "must accept and execute a block" do
|
25
|
-
|
26
|
-
|
27
|
-
|
25
|
+
expect(
|
26
|
+
lambda do
|
27
|
+
klass.configure do
|
28
|
+
raise RuntimeError, "Testing"
|
29
|
+
end
|
28
30
|
end
|
29
|
-
|
31
|
+
).to raise_exception(RuntimeError)
|
30
32
|
end
|
31
33
|
|
32
34
|
context "configure() block" do
|
@@ -37,12 +39,12 @@ describe Dhl::GetQuote do
|
|
37
39
|
end
|
38
40
|
|
39
41
|
it "must set the class test_mode? to true" do
|
40
|
-
klass.test_mode
|
42
|
+
expect(klass.test_mode?).to be_true
|
41
43
|
end
|
42
44
|
|
43
45
|
it "Dhl::GetQuote::Request must honor this test mode" do
|
44
46
|
request = Dhl::GetQuote::Request.new(valid_request_options)
|
45
|
-
request.test_mode
|
47
|
+
expect(request.test_mode?).to be_true
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
@@ -52,12 +54,12 @@ describe Dhl::GetQuote do
|
|
52
54
|
end
|
53
55
|
|
54
56
|
it "must set the classvar test_mode to false" do
|
55
|
-
klass.test_mode
|
57
|
+
expect(klass.test_mode?).to be_false
|
56
58
|
end
|
57
59
|
|
58
60
|
it "Dhl::GetQuote::Request must honor this test mode" do
|
59
61
|
request = Dhl::GetQuote::Request.new(valid_request_options)
|
60
|
-
request.test_mode
|
62
|
+
expect(request.test_mode?).to be_false
|
61
63
|
end
|
62
64
|
end
|
63
65
|
|
@@ -65,7 +67,9 @@ describe Dhl::GetQuote do
|
|
65
67
|
before(:each) { klass.configure { |c| c.site_id "SomethingHere" } }
|
66
68
|
|
67
69
|
it "must set class site_id to passed string" do
|
68
|
-
|
70
|
+
expect(
|
71
|
+
klass.site_id
|
72
|
+
).to eq("SomethingHere")
|
69
73
|
end
|
70
74
|
|
71
75
|
it "Dhl::GetQuote::Request must honor this" do
|
@@ -73,7 +77,9 @@ describe Dhl::GetQuote do
|
|
73
77
|
:password => 'xxx'
|
74
78
|
)
|
75
79
|
|
76
|
-
|
80
|
+
expect(
|
81
|
+
request.instance_variable_get(:@site_id)
|
82
|
+
).to eq("SomethingHere")
|
77
83
|
end
|
78
84
|
end
|
79
85
|
|
@@ -81,7 +87,7 @@ describe Dhl::GetQuote do
|
|
81
87
|
before(:each) { klass.configure { |c| c.password "ppaasswwoorrdd" } }
|
82
88
|
|
83
89
|
it "must set class password to passed string" do
|
84
|
-
klass.password.
|
90
|
+
expect(klass.password).to eq("ppaasswwoorrdd")
|
85
91
|
end
|
86
92
|
|
87
93
|
it "Dhl::GetQuote::Request must honor this" do
|
@@ -89,15 +95,16 @@ describe Dhl::GetQuote do
|
|
89
95
|
:site_id => 'ASiteId'
|
90
96
|
)
|
91
97
|
|
92
|
-
request.instance_variable_get(:@password).
|
98
|
+
expect(request.instance_variable_get(:@password)).to eq("ppaasswwoorrdd")
|
93
99
|
end
|
94
100
|
end
|
95
101
|
|
96
102
|
describe "kilograms!" do
|
97
103
|
# silence deprication notices in tests
|
98
|
-
before(:each) { klass.stub
|
104
|
+
before(:each) { klass.stub(:puts) }
|
99
105
|
|
100
106
|
it "must call #metric_measurements!" do
|
107
|
+
# expect(klass).to receive(:metric_measurements!)
|
101
108
|
klass.must_receive(:metric_measurements!)
|
102
109
|
klass.kilograms!
|
103
110
|
end
|
@@ -105,9 +112,10 @@ describe Dhl::GetQuote do
|
|
105
112
|
|
106
113
|
describe "pounds!" do
|
107
114
|
# silence deprication notices in tests
|
108
|
-
before(:each) { klass.stub
|
115
|
+
before(:each) { klass.stub(:puts) }
|
109
116
|
|
110
117
|
it "must call #us_measurements!" do
|
118
|
+
# expect(klass).to receive(:us_measurements!)
|
111
119
|
klass.must_receive(:us_measurements!)
|
112
120
|
klass.pounds!
|
113
121
|
end
|
@@ -115,9 +123,10 @@ describe Dhl::GetQuote do
|
|
115
123
|
|
116
124
|
describe "centimeters!" do
|
117
125
|
# silence deprication notices in tests
|
118
|
-
before(:each) { klass.stub
|
126
|
+
before(:each) { klass.stub(:puts) }
|
119
127
|
|
120
128
|
it "must call #metric_measurements!" do
|
129
|
+
# expect(klass).to receive(:metric_measurements!)
|
121
130
|
klass.must_receive(:metric_measurements!)
|
122
131
|
klass.centimeters!
|
123
132
|
end
|
@@ -125,9 +134,10 @@ describe Dhl::GetQuote do
|
|
125
134
|
|
126
135
|
describe "inches!" do
|
127
136
|
# silence deprication notices in tests
|
128
|
-
before(:each) { klass.stub
|
137
|
+
before(:each) { klass.stub(:puts) }
|
129
138
|
|
130
139
|
it "must call #us_measurements!" do
|
140
|
+
# expect(klass).to receive(:us_measurements!)
|
131
141
|
klass.must_receive(:us_measurements!)
|
132
142
|
klass.inches!
|
133
143
|
end
|
@@ -137,14 +147,14 @@ describe Dhl::GetQuote do
|
|
137
147
|
before(:each) { klass.metric_measurements! }
|
138
148
|
it "must set the weight and dimensions to LB and IN" do
|
139
149
|
klass.us_measurements!
|
140
|
-
klass.dimensions_unit.
|
141
|
-
klass.weight_unit.
|
150
|
+
expect(klass.dimensions_unit).to eq("IN")
|
151
|
+
expect(klass.weight_unit).to eq("LB")
|
142
152
|
end
|
143
153
|
|
144
154
|
it "Dhl::GetQuote::Request must honor this" do
|
145
155
|
klass.us_measurements!
|
146
|
-
valid_request.dimensions_unit.
|
147
|
-
valid_request.weight_unit.
|
156
|
+
expect(valid_request.dimensions_unit).to eq("IN")
|
157
|
+
expect(valid_request.weight_unit).to eq("LB")
|
148
158
|
end
|
149
159
|
end
|
150
160
|
|
@@ -152,14 +162,108 @@ describe Dhl::GetQuote do
|
|
152
162
|
before(:each) { klass.us_measurements! }
|
153
163
|
it "must set the weight and dimensions to KG and CM" do
|
154
164
|
klass.metric_measurements!
|
155
|
-
klass.dimensions_unit.
|
156
|
-
klass.weight_unit.
|
165
|
+
expect(klass.dimensions_unit).to eq("CM")
|
166
|
+
expect(klass.weight_unit).to eq("KG")
|
157
167
|
end
|
158
168
|
|
159
169
|
it "Dhl::GetQuote::Request must honor this" do
|
160
170
|
klass.metric_measurements!
|
161
|
-
valid_request.dimensions_unit.
|
162
|
-
valid_request.weight_unit.
|
171
|
+
expect(valid_request.dimensions_unit).to eq("CM")
|
172
|
+
expect(valid_request.weight_unit).to eq("KG")
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe "set_logger" do
|
177
|
+
# note: we test on what the logger proc DOES, not by what it IS
|
178
|
+
# because in 1.8.7, Proc objects can't be compared accurately with ==.
|
179
|
+
describe "it sets the logging method" do
|
180
|
+
let(:logger_proc) do
|
181
|
+
Proc.new { |msg,ll| puts msg }
|
182
|
+
end
|
183
|
+
|
184
|
+
it "must accept an argument" do
|
185
|
+
klass.set_logger(logger_proc)
|
186
|
+
expect(klass.get_logger).to eq(logger_proc)
|
187
|
+
end
|
188
|
+
|
189
|
+
it "must accept a block" do
|
190
|
+
klass.set_logger do
|
191
|
+
:foo
|
192
|
+
end
|
193
|
+
expect(klass.get_logger.call).to eq( :foo )
|
194
|
+
end
|
195
|
+
|
196
|
+
it "if both argument and block are given, it uses the block" do
|
197
|
+
klass.set_logger(logger_proc) do
|
198
|
+
:bar
|
199
|
+
end
|
200
|
+
expect(klass.get_logger.call).to eq( :bar )
|
201
|
+
end
|
202
|
+
|
203
|
+
it "if called without either it uses self.default_logger" do
|
204
|
+
# expect(klass).to receive(:default_logger).at_least(:once).and_return(logger_proc)
|
205
|
+
klass.must_receive(:default_logger).at_least(:once).and_return(logger_proc)
|
206
|
+
klass.set_logger
|
207
|
+
end
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe "set_log_level" do
|
212
|
+
it "allows log level to be set as :none, :critical, :verbose and :debug" do
|
213
|
+
[:none, :critical, :verbose, :debug].each do |level|
|
214
|
+
klass.set_log_level level
|
215
|
+
expect(klass.log_level).to eq(level)
|
216
|
+
end
|
217
|
+
end
|
218
|
+
|
219
|
+
it "throws an error if an incorrect log level is given" do
|
220
|
+
expect(
|
221
|
+
lambda { klass.set_log_level :wrong }
|
222
|
+
).to raise_exception(RuntimeError, "Log level :wrong is not valid")
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "log" do
|
227
|
+
let(:logger_proc) { Proc.new {|m,l|puts m} }
|
228
|
+
before(:each) do
|
229
|
+
klass.set_logger logger_proc
|
230
|
+
end
|
231
|
+
it "should not log message if log level of the message is lower than log_level()" do
|
232
|
+
klass.set_log_level(:critical)
|
233
|
+
|
234
|
+
logger_proc.should_not_receive(:call)
|
235
|
+
|
236
|
+
klass.log("foo", :verbose)
|
237
|
+
end
|
238
|
+
it "should log message if log level of the message is equal to log_level()" do
|
239
|
+
|
240
|
+
klass.set_log_level(:critical)
|
241
|
+
|
242
|
+
logger_proc.should_receive(:call).with("foo", :critical)
|
243
|
+
|
244
|
+
klass.log("foo", :critical)
|
245
|
+
end
|
246
|
+
it "should log message if log level of the message is greater than log_level()" do
|
247
|
+
klass.set_log_level(:verbose)
|
248
|
+
|
249
|
+
logger_proc.should_receive(:call).with("foo", :critical)
|
250
|
+
|
251
|
+
klass.log("foo", :critical)
|
252
|
+
end
|
253
|
+
|
254
|
+
it "should never log when log level is :none" do
|
255
|
+
klass.set_log_level(:none)
|
256
|
+
|
257
|
+
logger_proc.should_not_receive(:call)
|
258
|
+
|
259
|
+
klass.log("foo", :debug)
|
260
|
+
end
|
261
|
+
end
|
262
|
+
|
263
|
+
describe "default_logger" do
|
264
|
+
it "should print log messages to STDERR" do
|
265
|
+
STDERR.should_receive(:puts).with("Y: Dhl-get_quote gem: x")
|
266
|
+
klass.default_logger.call('x', 'y')
|
163
267
|
end
|
164
268
|
end
|
165
269
|
end
|
@@ -360,6 +360,12 @@ describe Dhl::GetQuote::Request do
|
|
360
360
|
# gsub here removes leading whitespace which may be variable.
|
361
361
|
let(:xml_output) { subject.to_xml.gsub(/^\s+/, '') }
|
362
362
|
|
363
|
+
it "must validate the object" do
|
364
|
+
subject.must_receive(:validate!)
|
365
|
+
|
366
|
+
subject.to_xml
|
367
|
+
end
|
368
|
+
|
363
369
|
it "must return an XML version of the object including Pieces" do
|
364
370
|
|
365
371
|
subject.pieces << mock_piece
|
@@ -455,12 +461,9 @@ eos
|
|
455
461
|
mock(:httparty, :response => mock_httparty_response)
|
456
462
|
)
|
457
463
|
Dhl::GetQuote::Response.stub!(:new).and_return(mock_response_object)
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
subject.must_receive(:validate!)
|
462
|
-
|
463
|
-
subject.post
|
464
|
+
|
465
|
+
# to be unstubbed later
|
466
|
+
subject.stub(:log_request_and_response_xml)
|
464
467
|
end
|
465
468
|
|
466
469
|
it "must post to server" do
|
@@ -479,6 +482,110 @@ eos
|
|
479
482
|
subject.post.must == mock_response_object
|
480
483
|
end
|
481
484
|
|
485
|
+
context "there is an exception" do
|
486
|
+
|
487
|
+
let(:exception) do
|
488
|
+
NoMethodError.new("undefined method `detect' for nil:NilClass")
|
489
|
+
end
|
490
|
+
|
491
|
+
before(:each) do
|
492
|
+
Dhl::GetQuote::Response.stub(:new).and_raise(exception)
|
493
|
+
Dhl::GetQuote.stub(:log) # silence log output for this test
|
494
|
+
end
|
495
|
+
|
496
|
+
it "must log the request and response if there is any exception" do
|
497
|
+
subject.unstub(:log_request_and_response_xml)
|
498
|
+
# expect(subject).to receive(
|
499
|
+
# :log_request_and_response_xml
|
500
|
+
# ).exactly(:once)
|
501
|
+
subject.should_receive(
|
502
|
+
:log_request_and_response_xml
|
503
|
+
).exactly(:once)
|
504
|
+
|
505
|
+
# unless wrapped in an expect, the exception is swallowed!
|
506
|
+
expect(
|
507
|
+
lambda { subject.post }
|
508
|
+
).to raise_exception
|
509
|
+
end
|
510
|
+
|
511
|
+
it "must re-raise any exceptions" do
|
512
|
+
expect(
|
513
|
+
lambda { subject.post }
|
514
|
+
).to raise_exception(
|
515
|
+
exception.class
|
516
|
+
)
|
517
|
+
end
|
518
|
+
|
519
|
+
context "validation error" do
|
520
|
+
let(:exception) do
|
521
|
+
Dhl::GetQuote::OptionsError.new(":password is a required option")
|
522
|
+
end
|
523
|
+
|
524
|
+
before(:each) do
|
525
|
+
Dhl::GetQuote::Response.stub(:new).and_raise(exception)
|
526
|
+
subject.unstub(:log_request_and_response_xml)
|
527
|
+
end
|
528
|
+
|
529
|
+
after(:each) do
|
530
|
+
expect( lambda { subject.post } ).to raise_exception
|
531
|
+
end
|
532
|
+
|
533
|
+
it "should log with the correct log level of :verbose" do
|
534
|
+
Dhl::GetQuote.set_log_level(Dhl::GetQuote::DEFAULT_LOG_LEVEL)
|
535
|
+
|
536
|
+
subject.should_receive(:log_exception).with(
|
537
|
+
exception, exception.log_level
|
538
|
+
)
|
539
|
+
end
|
540
|
+
end
|
541
|
+
|
542
|
+
context ":critical error" do
|
543
|
+
before(:each) do
|
544
|
+
subject.unstub(:log_request_and_response_xml)
|
545
|
+
end
|
546
|
+
|
547
|
+
after(:each) do
|
548
|
+
expect( lambda { subject.post } ).to raise_exception
|
549
|
+
end
|
550
|
+
|
551
|
+
it "must log exception name" do
|
552
|
+
subject.should_receive(:log_exception).with(
|
553
|
+
exception, :critical
|
554
|
+
)
|
555
|
+
end
|
556
|
+
|
557
|
+
it "must log the request xml" do
|
558
|
+
subject.instance_variable_set(:@to_xml, 'this is request')
|
559
|
+
|
560
|
+
subject.should_receive(:log_request_xml).with(
|
561
|
+
"this is request", :critical
|
562
|
+
)
|
563
|
+
end
|
564
|
+
|
565
|
+
it "must log the a note if no request xml has been generated yet" do
|
566
|
+
subject.should_receive(:log_request_xml).with(
|
567
|
+
"<not generated at time of error>", :critical
|
568
|
+
)
|
569
|
+
end
|
570
|
+
|
571
|
+
it "must log the response body" do
|
572
|
+
mock_httparty_response.stub(:body).and_return(
|
573
|
+
'this is the body'
|
574
|
+
)
|
575
|
+
|
576
|
+
subject.should_receive(:log_response_xml).with(
|
577
|
+
"this is the body", :critical
|
578
|
+
)
|
579
|
+
end
|
580
|
+
|
581
|
+
it "must log the a note if no response xml has been received yet" do
|
582
|
+
subject.should_receive(:log_response_xml).with(
|
583
|
+
"<not received at time of error>", :critical
|
584
|
+
)
|
585
|
+
end
|
586
|
+
end
|
587
|
+
end
|
588
|
+
|
482
589
|
end
|
483
590
|
|
484
591
|
describe "#add_special_service" do
|
metadata
CHANGED
@@ -1,136 +1,129 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: dhl-get_quote
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 5
|
9
|
+
- 4
|
10
|
+
version: 0.5.4
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Deseret Book
|
9
14
|
- Matthew Nielsen
|
10
15
|
autorequire:
|
11
16
|
bindir: bin
|
12
17
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
18
|
+
|
19
|
+
date: 2013-11-04 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
16
22
|
name: httparty
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ~>
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: 0.10.2
|
23
|
-
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
25
|
none: false
|
27
|
-
requirements:
|
26
|
+
requirements:
|
28
27
|
- - ~>
|
29
|
-
- !ruby/object:Gem::Version
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 51
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 10
|
33
|
+
- 2
|
30
34
|
version: 0.10.2
|
31
|
-
- !ruby/object:Gem::Dependency
|
32
|
-
name: multi_xml
|
33
|
-
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
|
-
requirements:
|
36
|
-
- - ~>
|
37
|
-
- !ruby/object:Gem::Version
|
38
|
-
version: 0.5.3
|
39
35
|
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: multi_xml
|
40
39
|
prerelease: false
|
41
|
-
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
41
|
none: false
|
43
|
-
requirements:
|
42
|
+
requirements:
|
44
43
|
- - ~>
|
45
|
-
- !ruby/object:Gem::Version
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 13
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 5
|
49
|
+
- 3
|
46
50
|
version: 0.5.3
|
47
|
-
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
48
54
|
name: rake
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - ~>
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: 10.0.4
|
55
|
-
type: :development
|
56
55
|
prerelease: false
|
57
|
-
|
58
|
-
none: false
|
59
|
-
requirements:
|
60
|
-
- - ~>
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
version: 10.0.4
|
63
|
-
- !ruby/object:Gem::Dependency
|
64
|
-
name: rake
|
65
|
-
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
66
57
|
none: false
|
67
|
-
requirements:
|
68
|
-
- -
|
69
|
-
- !ruby/object:Gem::Version
|
58
|
+
requirements:
|
59
|
+
- - "="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 71
|
62
|
+
segments:
|
63
|
+
- 10
|
64
|
+
- 0
|
65
|
+
- 4
|
70
66
|
version: 10.0.4
|
71
67
|
type: :development
|
72
|
-
|
73
|
-
|
74
|
-
none: false
|
75
|
-
requirements:
|
76
|
-
- - '='
|
77
|
-
- !ruby/object:Gem::Version
|
78
|
-
version: 10.0.4
|
79
|
-
- !ruby/object:Gem::Dependency
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
80
70
|
name: rspec
|
81
|
-
requirement: !ruby/object:Gem::Requirement
|
82
|
-
none: false
|
83
|
-
requirements:
|
84
|
-
- - '='
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
version: 2.13.0
|
87
|
-
type: :development
|
88
71
|
prerelease: false
|
89
|
-
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
90
73
|
none: false
|
91
|
-
requirements:
|
92
|
-
- -
|
93
|
-
- !ruby/object:Gem::Version
|
74
|
+
requirements:
|
75
|
+
- - "="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 59
|
78
|
+
segments:
|
79
|
+
- 2
|
80
|
+
- 13
|
81
|
+
- 0
|
94
82
|
version: 2.13.0
|
95
|
-
- !ruby/object:Gem::Dependency
|
96
|
-
name: rspec-must
|
97
|
-
requirement: !ruby/object:Gem::Requirement
|
98
|
-
none: false
|
99
|
-
requirements:
|
100
|
-
- - '='
|
101
|
-
- !ruby/object:Gem::Version
|
102
|
-
version: 0.0.1
|
103
83
|
type: :development
|
84
|
+
version_requirements: *id004
|
85
|
+
- !ruby/object:Gem::Dependency
|
86
|
+
name: rspec-must
|
104
87
|
prerelease: false
|
105
|
-
|
88
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
106
89
|
none: false
|
107
|
-
requirements:
|
108
|
-
- -
|
109
|
-
- !ruby/object:Gem::Version
|
90
|
+
requirements:
|
91
|
+
- - "="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
hash: 29
|
94
|
+
segments:
|
95
|
+
- 0
|
96
|
+
- 0
|
97
|
+
- 1
|
110
98
|
version: 0.0.1
|
111
|
-
- !ruby/object:Gem::Dependency
|
112
|
-
name: timecop
|
113
|
-
requirement: !ruby/object:Gem::Requirement
|
114
|
-
none: false
|
115
|
-
requirements:
|
116
|
-
- - ~>
|
117
|
-
- !ruby/object:Gem::Version
|
118
|
-
version: 0.6.1
|
119
99
|
type: :development
|
100
|
+
version_requirements: *id005
|
101
|
+
- !ruby/object:Gem::Dependency
|
102
|
+
name: timecop
|
120
103
|
prerelease: false
|
121
|
-
|
104
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
122
105
|
none: false
|
123
|
-
requirements:
|
124
|
-
- -
|
125
|
-
- !ruby/object:Gem::Version
|
106
|
+
requirements:
|
107
|
+
- - "="
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
hash: 5
|
110
|
+
segments:
|
111
|
+
- 0
|
112
|
+
- 6
|
113
|
+
- 1
|
126
114
|
version: 0.6.1
|
115
|
+
type: :development
|
116
|
+
version_requirements: *id006
|
127
117
|
description: Get shipping quotes from DHL
|
128
|
-
email:
|
118
|
+
email:
|
129
119
|
- mnielsen@deseretbook.com
|
130
120
|
executables: []
|
121
|
+
|
131
122
|
extensions: []
|
123
|
+
|
132
124
|
extra_rdoc_files: []
|
133
|
-
|
125
|
+
|
126
|
+
files:
|
134
127
|
- .gitignore
|
135
128
|
- .rspec
|
136
129
|
- .rvmrc.example
|
@@ -157,34 +150,57 @@ files:
|
|
157
150
|
- tpl/request.xml.erb
|
158
151
|
homepage: https://github.com/deseretbook/dhl-get_quote
|
159
152
|
licenses: []
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
153
|
+
|
154
|
+
post_install_message: |+
|
155
|
+
|
156
|
+
*** NOTE Dhl-GetQuote ***
|
157
|
+
|
158
|
+
This version introduces the following changes from 0.5.3:
|
159
|
+
|
160
|
+
* Logging of request and response in the event of an error
|
161
|
+
* Logging levels and logging method can be set
|
162
|
+
|
163
|
+
This version introduces the following changes from 0.4.x:
|
164
|
+
|
165
|
+
* #inches!, #pounds!, #kilograms! and #centimeters have been depricated.
|
166
|
+
Please use #us_measurements! or #metric_measurements!.
|
167
|
+
|
168
|
+
* dutiable() replaces dutiable! now requires arguments (dutiable value and currency).
|
169
|
+
|
170
|
+
This version introduces the following changes from 0.5.1:
|
171
|
+
|
172
|
+
* Product.new() can accept weights and measures as float as well as by integer.
|
173
|
+
|
166
174
|
rdoc_options: []
|
167
|
-
|
175
|
+
|
176
|
+
require_paths:
|
168
177
|
- lib
|
169
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
178
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
170
179
|
none: false
|
171
|
-
requirements:
|
172
|
-
- -
|
173
|
-
- !ruby/object:Gem::Version
|
174
|
-
|
175
|
-
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
hash: 3
|
184
|
+
segments:
|
185
|
+
- 0
|
186
|
+
version: "0"
|
187
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
176
188
|
none: false
|
177
|
-
requirements:
|
178
|
-
- -
|
179
|
-
- !ruby/object:Gem::Version
|
180
|
-
|
189
|
+
requirements:
|
190
|
+
- - ">="
|
191
|
+
- !ruby/object:Gem::Version
|
192
|
+
hash: 3
|
193
|
+
segments:
|
194
|
+
- 0
|
195
|
+
version: "0"
|
181
196
|
requirements: []
|
197
|
+
|
182
198
|
rubyforge_project:
|
183
199
|
rubygems_version: 1.8.25
|
184
200
|
signing_key:
|
185
201
|
specification_version: 3
|
186
202
|
summary: Gem to interface with DHL's XML-PI shipping quote service.
|
187
|
-
test_files:
|
203
|
+
test_files:
|
188
204
|
- spec/lib/dhl/dhl-get_quote_spec.rb
|
189
205
|
- spec/lib/dhl/get_quote/market_service_spec.rb
|
190
206
|
- spec/lib/dhl/get_quote/piece_spec.rb
|