rc-rest 3.0.1 → 4.0

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig CHANGED
Binary file
@@ -0,0 +1,8 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ Autotest.add_hook :initialize do |at|
6
+ at.testlib = 'minitest/unit'
7
+ end
8
+
@@ -1,3 +1,9 @@
1
+ == 4.0
2
+
3
+ * rc-rest now depends on Net::HTTP::Persistent.
4
+ * URI::HTTP stubs no longer work. Use rc_rest/net_http_persistent_stub
5
+ instead. See Net::HTTP::Persistent for details.
6
+
1
7
  == 3.0.1
2
8
 
3
9
  * Fix minitest dependency
@@ -1,9 +1,10 @@
1
+ .autotest
1
2
  History.txt
2
3
  LICENSE.txt
3
4
  Manifest.txt
4
5
  README.txt
5
6
  Rakefile
6
7
  lib/rc_rest.rb
8
+ lib/rc_rest/net_http_persistent_stub.rb
7
9
  lib/rc_rest/net_http_stub.rb
8
- lib/rc_rest/uri_stub.rb
9
10
  test/test_rc_rest.rb
data/Rakefile CHANGED
@@ -1,11 +1,13 @@
1
1
  require 'hoe'
2
2
 
3
3
  Hoe.plugin :seattlerb
4
+
4
5
  Hoe.spec 'rc-rest' do |p|
5
6
  self.rubyforge_name = 'seattlerb'
6
7
  developer 'Eric Hodel', 'drbrain@segment7.net'
7
8
 
8
- extra_deps << ['nokogiri', '>= 1.3.1']
9
+ extra_deps << ['nokogiri', '>= 1.3.1']
10
+ extra_deps << ['net-http-persistent', '~> 1.0']
9
11
  end
10
12
 
11
13
  # vim: syntax=Ruby
@@ -1,5 +1,4 @@
1
- require 'net/http'
2
- require 'open-uri'
1
+ require 'net/http/persistent'
3
2
  require 'nokogiri'
4
3
 
5
4
  ##
@@ -49,7 +48,7 @@ class RCRest
49
48
  ##
50
49
  # You are using this version of RCRest
51
50
 
52
- VERSION = '3.0.1'
51
+ VERSION = '4.0'
53
52
 
54
53
  ##
55
54
  # Abstract Error class.
@@ -71,6 +70,7 @@ class RCRest
71
70
 
72
71
  def initialize(original_exception)
73
72
  @original_exception = original_exception
73
+
74
74
  message = "Communication error: #{original_exception.message}(#{original_exception.class})"
75
75
  super message
76
76
  end
@@ -115,27 +115,39 @@ class RCRest
115
115
  # instance and returns its result.
116
116
 
117
117
  def get(method, params = {})
118
+ @http ||= Net::HTTP::Persistent.new
119
+
118
120
  url = make_url method, params
119
121
 
120
- url.open do |xml|
121
- res = Nokogiri::XML(xml, nil, nil, 0)
122
+ http_response = @http.request url
123
+
124
+ case http_response
125
+ when Net::HTTPSuccess
126
+ res = Nokogiri::XML http_response.body, nil, nil, 0
122
127
 
123
128
  check_error res
124
129
 
125
- return parse_response(res)
130
+ parse_response res
131
+ when Net::HTTPMovedPermanently,
132
+ Net::HTTPFound,
133
+ Net::HTTPSeeOther,
134
+ Net::HTTPTemporaryRedirect then
135
+ # TODO
136
+ else
137
+ begin
138
+ xml = Nokogiri::XML http_response.body, nil, nil, 0
139
+ check_error xml
140
+ rescue Nokogiri::XML::SyntaxError => e
141
+ end
142
+
143
+ e = CommunicationError.new http_response
144
+ e.message << "\n\nunhandled error:\n#{xml.to_s}"
145
+
146
+ raise e
126
147
  end
127
- rescue IOError, SystemCallError, SocketError, Timeout::Error,
148
+ rescue Net::HTTP::Persistent::Error, SocketError, Timeout::Error,
128
149
  Nokogiri::XML::SyntaxError => e
129
150
  raise CommunicationError.new(e)
130
- rescue OpenURI::HTTPError => e
131
- begin
132
- xml = Nokogiri::XML(e.io, nil, nil, 0)
133
- check_error xml
134
- rescue Nokogiri::XML::SyntaxError => e
135
- end
136
- new_e = CommunicationError.new e
137
- new_e.message << "\n\nunhandled error:\n#{xml.to_s}"
138
- raise new_e
139
151
  end
140
152
 
141
153
  ##
@@ -0,0 +1,74 @@
1
+ require 'net/http/persistent'
2
+
3
+ ##
4
+ # This stub overrides Net::HTTP::Persistent's request method to allow programs
5
+ # that use Net::HTTP::Persistent to be easily tested.
6
+ #
7
+ # == Usage
8
+ #
9
+ # require 'rc_rest/net_http_persistent_stub'
10
+ #
11
+ # class TestMyClass < MiniTest::Unit::TestCase
12
+ #
13
+ # def setup
14
+ # Net::HTTP::Persistent.responses = []
15
+ # Net::HTTP::Persistent.uris = []
16
+ #
17
+ # @obj = MyClass.new
18
+ # end
19
+ #
20
+ # def test_my_method
21
+ # Net::HTTP::Persistent.responses << 'some text request would return'
22
+ #
23
+ # result = @obj.my_method
24
+ #
25
+ # assert_equal :something_meaninfgul, result
26
+ #
27
+ # assert_equal true, Net::HTTP::Persistent.responses.empty?
28
+ # assert_equal 1, Net::HTTP::Persistent.uris.length
29
+ # assert_equal 'http://example.com/path', Net::HTTP::Persistent.uris.first
30
+ # end
31
+ #
32
+ # end
33
+
34
+ class Net::HTTP::Persistent
35
+
36
+ class << self
37
+
38
+ ##
39
+ # List of responses #request should return.
40
+ #
41
+ # If a String is given a Net::HTTPOK is created
42
+ #
43
+ # If a proc is given, it is called. The proc should raise an exception or
44
+ # return a Net::HTTPResponse subclass.
45
+ #
46
+ # Unlike URI::HTTP from rc-rest 3.x and earlier you must return a
47
+ # Net::HTTPResponse subclass.
48
+
49
+ attr_accessor :responses
50
+
51
+ ##
52
+ # URIs recorded
53
+
54
+ attr_accessor :uris
55
+
56
+ end
57
+
58
+ alias original_request request
59
+
60
+ def request uri
61
+ self.class.uris << uri.to_s
62
+ response = self.class.responses.shift
63
+
64
+ response = response.call if response.respond_to? :call
65
+
66
+ return response if Net::HTTPResponse === response
67
+
68
+ r = Net::HTTPOK.new '1.0', 200, 'OK'
69
+ r.body = response
70
+ r
71
+ end
72
+
73
+ end
74
+
@@ -1,6 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'minitest/autorun'
3
- require 'rc_rest/uri_stub'
3
+ require 'rc_rest/net_http_persistent_stub'
4
4
  require 'rc_rest/net_http_stub'
5
5
  require 'rc_rest'
6
6
 
@@ -37,8 +37,8 @@ end
37
37
  class TestFakeService < MiniTest::Unit::TestCase
38
38
 
39
39
  def setup
40
- URI::HTTP.responses = []
41
- URI::HTTP.uris = []
40
+ Net::HTTP::Persistent.responses = []
41
+ Net::HTTP::Persistent.uris = []
42
42
 
43
43
  Net::HTTP.params = []
44
44
  Net::HTTP.paths = []
@@ -62,49 +62,53 @@ class TestFakeService < MiniTest::Unit::TestCase
62
62
 
63
63
  def test_do_get
64
64
  xml = "<?xml version=\"1.0\"?>\n<result>stuff</result>\n"
65
- URI::HTTP.responses << xml
65
+ Net::HTTP::Persistent.responses << xml
66
66
 
67
67
  result = @fs.do_get
68
68
 
69
69
  assert_equal xml, result.to_s
70
- assert_equal 'http://example.com/method?', URI::HTTP.uris.first
70
+ assert_equal 'http://example.com/method?', Net::HTTP::Persistent.uris.first
71
71
  end
72
72
 
73
73
  def test_do_get_bad_xml
74
74
  xml = '<result>stuff</result><extra/>'
75
- URI::HTTP.responses << xml
75
+ Net::HTTP::Persistent.responses << xml
76
76
 
77
77
  assert_raises RCRest::CommunicationError do @fs.do_get end
78
78
  end
79
79
 
80
80
  def test_do_get_error_400
81
- URI::HTTP.responses << proc do
82
- xml = '<error>you did the bad thing</error>'
83
- raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
81
+ Net::HTTP::Persistent.responses << proc do
82
+ r = Net::HTTPBadRequest.new '1.0', 400, 'Bad Request'
83
+ r.body = '<error>you did the bad thing</error>'
84
+ r
84
85
  end
85
86
 
86
87
  assert_raises FakeService::Error do @fs.do_get end
87
88
  end
88
89
 
89
90
  def test_do_get_error_400_bad_xml
90
- URI::HTTP.responses << proc do
91
- xml = '<error>you did the bad thing</error><extra/>'
92
- raise OpenURI::HTTPError.new('400 Bad Request', StringIO.new(xml))
91
+ Net::HTTP::Persistent.responses << proc do
92
+ r = Net::HTTPBadRequest.new '1.0', 400, 'Bad Request'
93
+ r.body = '<error>you did the bad thing</error><extra/>'
94
+ r
93
95
  end
94
96
 
95
97
  assert_raises RCRest::CommunicationError do @fs.do_get end
96
98
  end
97
99
 
98
100
  def test_do_get_error_unhandled
99
- URI::HTTP.responses << proc do
101
+ Net::HTTP::Persistent.responses << proc do
100
102
  xml = "<?xml version=\"1.0\"?>\n<other_error>you did the bad thing</other_error>\n"
101
- raise OpenURI::HTTPError.new('500 Internal Server Error', StringIO.new(xml))
103
+ r = Net::HTTPInternalServerError.new '1.0', 500, 'Internal Server Error'
104
+ r.body = xml
105
+ r
102
106
  end
103
107
 
104
108
  e = assert_raises RCRest::CommunicationError do @fs.do_get end
105
109
 
106
110
  expected = <<-EOF.strip
107
- Communication error: 500 Internal Server Error(OpenURI::HTTPError)
111
+ Communication error: Internal Server Error(Net::HTTPInternalServerError)
108
112
 
109
113
  unhandled error:
110
114
  <?xml version=\"1.0\"?>
@@ -114,10 +118,9 @@ unhandled error:
114
118
  assert_equal expected, e.message.strip
115
119
  end
116
120
 
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
+ def test_do_get_net_http_persistent_error
122
+ Net::HTTP::Persistent.responses << proc do
123
+ raise Net::HTTP::Persistent::Error, 'end of file reached'
121
124
  end
122
125
 
123
126
  assert_raises RCRest::CommunicationError do @fs.do_get end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rc-rest
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 27
4
5
  prerelease: false
5
6
  segments:
6
- - 3
7
+ - 4
7
8
  - 0
8
- - 1
9
- version: 3.0.1
9
+ version: "4.0"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Eric Hodel
@@ -35,16 +35,18 @@ cert_chain:
35
35
  x52qPcexcYZR7w==
36
36
  -----END CERTIFICATE-----
37
37
 
38
- date: 2010-02-11 00:00:00 -08:00
38
+ date: 2010-05-11 00:00:00 -07:00
39
39
  default_executable:
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: nokogiri
43
43
  prerelease: false
44
44
  requirement: &id001 !ruby/object:Gem::Requirement
45
+ none: false
45
46
  requirements:
46
47
  - - ">="
47
48
  - !ruby/object:Gem::Version
49
+ hash: 25
48
50
  segments:
49
51
  - 1
50
52
  - 3
@@ -53,61 +55,84 @@ dependencies:
53
55
  type: :runtime
54
56
  version_requirements: *id001
55
57
  - !ruby/object:Gem::Dependency
56
- name: rubyforge
58
+ name: net-http-persistent
57
59
  prerelease: false
58
60
  requirement: &id002 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ hash: 15
66
+ segments:
67
+ - 1
68
+ - 0
69
+ version: "1.0"
70
+ type: :runtime
71
+ version_requirements: *id002
72
+ - !ruby/object:Gem::Dependency
73
+ name: rubyforge
74
+ prerelease: false
75
+ requirement: &id003 !ruby/object:Gem::Requirement
76
+ none: false
59
77
  requirements:
60
78
  - - ">="
61
79
  - !ruby/object:Gem::Version
80
+ hash: 9
62
81
  segments:
63
82
  - 2
64
83
  - 0
65
84
  - 3
66
85
  version: 2.0.3
67
86
  type: :development
68
- version_requirements: *id002
87
+ version_requirements: *id003
69
88
  - !ruby/object:Gem::Dependency
70
89
  name: gemcutter
71
90
  prerelease: false
72
- requirement: &id003 !ruby/object:Gem::Requirement
91
+ requirement: &id004 !ruby/object:Gem::Requirement
92
+ none: false
73
93
  requirements:
74
94
  - - ">="
75
95
  - !ruby/object:Gem::Version
96
+ hash: 11
76
97
  segments:
77
98
  - 0
78
- - 3
99
+ - 5
79
100
  - 0
80
- version: 0.3.0
101
+ version: 0.5.0
81
102
  type: :development
82
- version_requirements: *id003
103
+ version_requirements: *id004
83
104
  - !ruby/object:Gem::Dependency
84
105
  name: minitest
85
106
  prerelease: false
86
- requirement: &id004 !ruby/object:Gem::Requirement
107
+ requirement: &id005 !ruby/object:Gem::Requirement
108
+ none: false
87
109
  requirements:
88
110
  - - ">="
89
111
  - !ruby/object:Gem::Version
112
+ hash: 3
90
113
  segments:
91
114
  - 1
92
115
  - 5
93
116
  - 0
94
117
  version: 1.5.0
95
118
  type: :development
96
- version_requirements: *id004
119
+ version_requirements: *id005
97
120
  - !ruby/object:Gem::Dependency
98
121
  name: hoe
99
122
  prerelease: false
100
- requirement: &id005 !ruby/object:Gem::Requirement
123
+ requirement: &id006 !ruby/object:Gem::Requirement
124
+ none: false
101
125
  requirements:
102
126
  - - ">="
103
127
  - !ruby/object:Gem::Version
128
+ hash: 27
104
129
  segments:
105
130
  - 2
106
131
  - 5
107
132
  - 0
108
133
  version: 2.5.0
109
134
  type: :development
110
- version_requirements: *id005
135
+ version_requirements: *id006
111
136
  description: |-
112
137
  Robot Co-op REST web services base class. This library makes it easy to
113
138
  implement REST-like web services APIs.
@@ -123,14 +148,15 @@ extra_rdoc_files:
123
148
  - Manifest.txt
124
149
  - README.txt
125
150
  files:
151
+ - .autotest
126
152
  - History.txt
127
153
  - LICENSE.txt
128
154
  - Manifest.txt
129
155
  - README.txt
130
156
  - Rakefile
131
157
  - lib/rc_rest.rb
158
+ - lib/rc_rest/net_http_persistent_stub.rb
132
159
  - lib/rc_rest/net_http_stub.rb
133
- - lib/rc_rest/uri_stub.rb
134
160
  - test/test_rc_rest.rb
135
161
  has_rdoc: true
136
162
  homepage: http://seattlerb.rubyforge.org/rc-rest
@@ -143,23 +169,27 @@ rdoc_options:
143
169
  require_paths:
144
170
  - lib
145
171
  required_ruby_version: !ruby/object:Gem::Requirement
172
+ none: false
146
173
  requirements:
147
174
  - - ">="
148
175
  - !ruby/object:Gem::Version
176
+ hash: 3
149
177
  segments:
150
178
  - 0
151
179
  version: "0"
152
180
  required_rubygems_version: !ruby/object:Gem::Requirement
181
+ none: false
153
182
  requirements:
154
183
  - - ">="
155
184
  - !ruby/object:Gem::Version
185
+ hash: 3
156
186
  segments:
157
187
  - 0
158
188
  version: "0"
159
189
  requirements: []
160
190
 
161
191
  rubyforge_project: seattlerb
162
- rubygems_version: 1.3.6.pre.2
192
+ rubygems_version: 1.3.7.pre.1
163
193
  signing_key:
164
194
  specification_version: 3
165
195
  summary: Robot Co-op REST web services base class
metadata.gz.sig CHANGED
Binary file
@@ -1,56 +0,0 @@
1
- require 'open-uri'
2
-
3
- module URI # :nodoc:
4
- end
5
-
6
- ##
7
- # This stub overrides OpenURI's open method to allow programs that use OpenURI
8
- # to be easily tested.
9
- #
10
- # == Usage
11
- #
12
- # require 'rc_rest/uri_stub'
13
- #
14
- # class TestMyClass < Test::Unit::TestCase
15
- #
16
- # def setup
17
- # URI::HTTP.responses = []
18
- # URI::HTTP.uris = []
19
- #
20
- # @obj = MyClass.new
21
- # end
22
- #
23
- # def test_my_method
24
- # URI::HTTP.responses << 'some text open would ordinarily return'
25
- #
26
- # result = @obj.my_method
27
- #
28
- # assert_equal :something_meaninfgul, result
29
- #
30
- # assert_equal true, URI::HTTP.responses.empty?
31
- # assert_equal 1, URI::HTTP.uris.length
32
- # assert_equal 'http://example.com/path', URI::HTTP.uris.first
33
- # end
34
- #
35
- # end
36
-
37
- class URI::HTTP # :nodoc:
38
-
39
- class << self
40
- attr_accessor :responses, :uris
41
- end
42
-
43
- alias original_open open
44
-
45
- def open
46
- self.class.uris << self.to_s
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
53
- end
54
-
55
- end
56
-