rc-rest 2.1.0 → 2.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +12 -2
- data/Rakefile +4 -11
- data/lib/rc_rest.rb +45 -10
- data/lib/rc_rest/net_http_stub.rb +8 -3
- data/lib/rc_rest/uri_stub.rb +6 -1
- data/test/test_rc_rest.rb +65 -12
- metadata +15 -6
data/History.txt
CHANGED
@@ -1,3 +1,13 @@
|
|
1
|
+
= 2.2.0
|
2
|
+
|
3
|
+
* Moved to seattlerb rubyforge project
|
4
|
+
* Moved to p4
|
5
|
+
* Wrap communication errors in RCRest::CommunicationError
|
6
|
+
|
7
|
+
= 2.1.1
|
8
|
+
|
9
|
+
* Fix dependency on ZenTest for testing
|
10
|
+
|
1
11
|
= 2.1.0
|
2
12
|
|
3
13
|
* Don't split strings into extra params pairs on newlines
|
@@ -7,8 +17,8 @@
|
|
7
17
|
= 2.0.0
|
8
18
|
|
9
19
|
* Added +method+ parameter to RCRest#make_url to add path components
|
10
|
-
to a generic endpoint
|
11
|
-
* Added RCRest#post
|
20
|
+
to a generic endpoint
|
21
|
+
* Added RCRest#post
|
12
22
|
|
13
23
|
= 1.0.0
|
14
24
|
|
data/Rakefile
CHANGED
@@ -1,24 +1,17 @@
|
|
1
1
|
require 'hoe'
|
2
2
|
require './lib/rc_rest'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
hoe = Hoe.new 'rc-rest', RCRest::VERSION do |p|
|
4
|
+
Hoe.new 'rc-rest', RCRest::VERSION do |p|
|
7
5
|
p.summary = 'Robot Co-op REST web services base class'
|
8
6
|
p.description = 'This library makes it easy to implement REST-like web services APIs.'
|
9
7
|
p.author = 'Eric Hodel'
|
10
8
|
p.email = 'drbrain@segment7.net'
|
11
|
-
p.url = "http://
|
12
|
-
p.rubyforge_name = '
|
9
|
+
p.url = "http://seattlerb.rubyforge.org/rc-rest"
|
10
|
+
p.rubyforge_name = 'seattlerb'
|
13
11
|
|
14
12
|
p.changes = File.read('History.txt').scan(/\A(=.*?)^=/m).first.first
|
15
|
-
end
|
16
|
-
|
17
|
-
SPEC = hoe.spec
|
18
13
|
|
19
|
-
|
20
|
-
require '../tasks'
|
21
|
-
rescue LoadError
|
14
|
+
p.extra_deps << ['ZenTest', '>= 3.4.2']
|
22
15
|
end
|
23
16
|
|
24
17
|
# vim: syntax=Ruby
|
data/lib/rc_rest.rb
CHANGED
@@ -49,13 +49,34 @@ class RCRest
|
|
49
49
|
##
|
50
50
|
# You are using this version of RCRest
|
51
51
|
|
52
|
-
VERSION = '2.
|
52
|
+
VERSION = '2.2.0'
|
53
53
|
|
54
54
|
##
|
55
55
|
# Abstract Error class.
|
56
56
|
|
57
57
|
class Error < RuntimeError; end
|
58
58
|
|
59
|
+
##
|
60
|
+
# Error raised when communicating with the server
|
61
|
+
|
62
|
+
class CommunicationError < Error
|
63
|
+
|
64
|
+
##
|
65
|
+
# The original exception
|
66
|
+
|
67
|
+
attr_accessor :original_exception
|
68
|
+
|
69
|
+
##
|
70
|
+
# Creates a new CommunicationError with +message+ and +original_exception+
|
71
|
+
|
72
|
+
def initialize(original_exception)
|
73
|
+
@original_exception = original_exception
|
74
|
+
message = "Communication error: #{original_exception.message}(#{original_exception.class})"
|
75
|
+
super message
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
59
80
|
##
|
60
81
|
# Web services initializer.
|
61
82
|
#
|
@@ -103,10 +124,18 @@ class RCRest
|
|
103
124
|
|
104
125
|
return parse_response(res)
|
105
126
|
end
|
127
|
+
rescue IOError, SystemCallError, SocketError, Timeout::Error,
|
128
|
+
REXML::ParseException => e
|
129
|
+
raise CommunicationError.new(e)
|
106
130
|
rescue OpenURI::HTTPError => e
|
107
|
-
|
108
|
-
|
109
|
-
|
131
|
+
begin
|
132
|
+
xml = REXML::Document.new e.io.read
|
133
|
+
check_error xml
|
134
|
+
rescue REXML::ParseException => e
|
135
|
+
end
|
136
|
+
new_e = CommunicationError.new e
|
137
|
+
new_e.message << "\n\nunhandled error:\n#{xml.to_s}"
|
138
|
+
raise new_e
|
110
139
|
end
|
111
140
|
|
112
141
|
##
|
@@ -202,12 +231,15 @@ class RCRest
|
|
202
231
|
xml = REXML::Document.new res.body
|
203
232
|
|
204
233
|
check_error xml
|
205
|
-
|
206
|
-
|
234
|
+
|
235
|
+
parse_response xml
|
236
|
+
rescue SystemCallError, SocketError, Timeout::Error,
|
237
|
+
REXML::ParseException => e
|
238
|
+
raise CommunicationError.new(e)
|
207
239
|
rescue Net::HTTPError => e
|
208
240
|
xml = REXML::Document.new e.res.body
|
209
241
|
check_error xml
|
210
|
-
raise
|
242
|
+
raise CommunicationError.new(e)
|
211
243
|
end
|
212
244
|
|
213
245
|
##
|
@@ -232,12 +264,15 @@ class RCRest
|
|
232
264
|
xml = REXML::Document.new res.body
|
233
265
|
|
234
266
|
check_error xml
|
235
|
-
|
236
|
-
|
267
|
+
|
268
|
+
parse_response xml
|
269
|
+
rescue SystemCallError, SocketError, Timeout::Error,
|
270
|
+
REXML::ParseException => e
|
271
|
+
raise CommunicationError.new(e)
|
237
272
|
rescue Net::HTTPError => e
|
238
273
|
xml = REXML::Document.new e.res.body
|
239
274
|
check_error xml
|
240
|
-
raise
|
275
|
+
raise CommunicationError.new(e)
|
241
276
|
end
|
242
277
|
|
243
278
|
end
|
@@ -51,9 +51,14 @@ class Net::HTTP
|
|
51
51
|
def request(req)
|
52
52
|
self.class.paths << req.path
|
53
53
|
self.class.params << req.body
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
response = self.class.responses.shift
|
55
|
+
if response.respond_to? :call then
|
56
|
+
response.call req
|
57
|
+
else
|
58
|
+
res = Net::HTTPResponse.new '1.0', 200, 'OK'
|
59
|
+
res.body = response
|
60
|
+
res
|
61
|
+
end
|
57
62
|
end
|
58
63
|
|
59
64
|
end
|
data/lib/rc_rest/uri_stub.rb
CHANGED
@@ -44,7 +44,12 @@ class URI::HTTP # :nodoc:
|
|
44
44
|
|
45
45
|
def open
|
46
46
|
self.class.uris << self.to_s
|
47
|
-
|
47
|
+
response = self.class.responses.shift
|
48
|
+
if response.respond_to? :call then
|
49
|
+
response.call
|
50
|
+
else
|
51
|
+
yield StringIO.new(response)
|
52
|
+
end
|
48
53
|
end
|
49
54
|
|
50
55
|
end
|
data/test/test_rc_rest.rb
CHANGED
@@ -71,19 +71,58 @@ class TestFakeService < Test::Unit::TestCase
|
|
71
71
|
assert_equal 'http://example.com/method?', URI::HTTP.uris.first
|
72
72
|
end
|
73
73
|
|
74
|
-
def
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
74
|
+
def test_do_get_bad_xml
|
75
|
+
xml = '<result>stuff</result><extra/>'
|
76
|
+
URI::HTTP.responses << xml
|
77
|
+
|
78
|
+
assert_raise RCRest::CommunicationError do @fs.do_get end
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_do_get_error_400
|
82
|
+
URI::HTTP.responses << proc do
|
83
|
+
xml = '<error>you did the bad thing</error>'
|
84
|
+
raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
|
82
85
|
end
|
83
86
|
|
84
87
|
assert_raise FakeService::Error do @fs.do_get end
|
85
88
|
end
|
86
89
|
|
90
|
+
def test_do_get_error_400_bad_xml
|
91
|
+
URI::HTTP.responses << proc do
|
92
|
+
xml = '<error>you did the bad thing</error><extra/>'
|
93
|
+
raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
|
94
|
+
end
|
95
|
+
|
96
|
+
assert_raise RCRest::CommunicationError do @fs.do_get end
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_do_get_error_unhandled
|
100
|
+
URI::HTTP.responses << proc do
|
101
|
+
xml = '<other_error>you did the bad thing</other_error>'
|
102
|
+
raise OpenURI::HTTPError.new('500 Internal Server Error', StringIO.new(xml))
|
103
|
+
end
|
104
|
+
|
105
|
+
e = assert_raise RCRest::CommunicationError do @fs.do_get end
|
106
|
+
|
107
|
+
expected = <<-EOF.strip
|
108
|
+
Communication error: 500 Internal Server Error(OpenURI::HTTPError)
|
109
|
+
|
110
|
+
unhandled error:
|
111
|
+
<other_error>you did the bad thing</other_error>
|
112
|
+
EOF
|
113
|
+
|
114
|
+
assert_equal expected, e.message
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_do_get_eof_error
|
118
|
+
URI::HTTP.responses << proc do
|
119
|
+
xml = '<error>you did the bad thing</error>'
|
120
|
+
raise EOFError, 'end of file reached'
|
121
|
+
end
|
122
|
+
|
123
|
+
assert_raise RCRest::CommunicationError do @fs.do_get end
|
124
|
+
end
|
125
|
+
|
87
126
|
def test_do_post
|
88
127
|
xml = '<result>stuff</result>'
|
89
128
|
Net::HTTP.responses << xml
|
@@ -100,6 +139,13 @@ class TestFakeService < Test::Unit::TestCase
|
|
100
139
|
assert_equal '/method', Net::HTTP.paths.first
|
101
140
|
end
|
102
141
|
|
142
|
+
def test_do_post_bad_xml
|
143
|
+
xml = '<result>stuff</result><extra/>'
|
144
|
+
Net::HTTP.responses << xml
|
145
|
+
|
146
|
+
assert_raise RCRest::CommunicationError do @fs.do_post end
|
147
|
+
end
|
148
|
+
|
103
149
|
def test_do_post_multipart
|
104
150
|
xml = '<result>stuff</result>'
|
105
151
|
Net::HTTP.responses << xml
|
@@ -124,16 +170,23 @@ value\r
|
|
124
170
|
assert_equal '/method', Net::HTTP.paths.first
|
125
171
|
end
|
126
172
|
|
173
|
+
def test_do_post_multipart_bad_xml
|
174
|
+
xml = '<result>stuff</result><extra/>'
|
175
|
+
Net::HTTP.responses << xml
|
176
|
+
|
177
|
+
assert_raise RCRest::CommunicationError do @fs.do_post_multipart end
|
178
|
+
end
|
179
|
+
|
127
180
|
end
|
128
181
|
|
129
182
|
class TestRCRest < Test::Unit::TestCase
|
130
183
|
|
131
184
|
def test_initialize
|
132
|
-
|
133
|
-
|
185
|
+
e = assert_raise NotImplementedError do
|
186
|
+
RCRest.new
|
187
|
+
end
|
188
|
+
|
134
189
|
assert_equal 'need to implement #intialize and set @url', e.message
|
135
|
-
else
|
136
|
-
flunk 'expected NotImplementedError'
|
137
190
|
end
|
138
191
|
|
139
192
|
def test_check_error
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
|
-
rubygems_version: 0.9.
|
2
|
+
rubygems_version: 0.9.1
|
3
3
|
specification_version: 1
|
4
4
|
name: rc-rest
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 2.
|
7
|
-
date:
|
6
|
+
version: 2.2.0
|
7
|
+
date: 2007-01-29 00:00:00 -08:00
|
8
8
|
summary: Robot Co-op REST web services base class
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: drbrain@segment7.net
|
12
|
-
homepage: http://
|
13
|
-
rubyforge_project:
|
12
|
+
homepage: http://seattlerb.rubyforge.org/rc-rest
|
13
|
+
rubyforge_project: seattlerb
|
14
14
|
description: This library makes it easy to implement REST-like web services APIs.
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
@@ -51,6 +51,15 @@ extensions: []
|
|
51
51
|
requirements: []
|
52
52
|
|
53
53
|
dependencies:
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: ZenTest
|
56
|
+
version_requirement:
|
57
|
+
version_requirements: !ruby/object:Gem::Version::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 3.4.2
|
62
|
+
version:
|
54
63
|
- !ruby/object:Gem::Dependency
|
55
64
|
name: hoe
|
56
65
|
version_requirement:
|
@@ -58,5 +67,5 @@ dependencies:
|
|
58
67
|
requirements:
|
59
68
|
- - ">="
|
60
69
|
- !ruby/object:Gem::Version
|
61
|
-
version: 1.1.
|
70
|
+
version: 1.1.7
|
62
71
|
version:
|