rest-client 0.5 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -31,7 +31,7 @@ require 'rake/gempackagetask'
31
31
  require 'rake/rdoctask'
32
32
  require 'fileutils'
33
33
 
34
- version = "0.5"
34
+ version = "0.5.1"
35
35
  name = "rest-client"
36
36
 
37
37
  spec = Gem::Specification.new do |s|
@@ -1,17 +1,43 @@
1
1
  require 'rexml/document'
2
2
 
3
3
  module RestClient
4
+ # This is the base RestClient exception class. Rescue it if you want to
5
+ # catch any exception that your request might raise
6
+ class Exception < RuntimeError
7
+ def message(default=nil)
8
+ self.class::ErrorMessage
9
+ end
10
+ end
11
+
4
12
  # A redirect was encountered; caught by execute to retry with the new url.
5
- class Redirect < RuntimeError; end
13
+ class Redirect < Exception
14
+ ErrorMessage = "Redirect"
15
+
16
+ attr_accessor :url
17
+ def initialize(url)
18
+ @url = url
19
+ end
20
+ end
6
21
 
7
22
  # Authorization is required to access the resource specified.
8
- class Unauthorized < RuntimeError; end
23
+ class Unauthorized < Exception
24
+ ErrorMessage = 'Unauthorized'
25
+ end
26
+
27
+ # No resource was found at the given URL.
28
+ class ResourceNotFound < Exception
29
+ ErrorMessage = 'Resource not found'
30
+ end
9
31
 
10
32
  # The server broke the connection prior to the request completing.
11
- class ServerBrokeConnection < RuntimeError; end
33
+ class ServerBrokeConnection < Exception
34
+ ErrorMessage = 'Server broke connection'
35
+ end
12
36
 
13
37
  # The server took too long to respond.
14
- class RequestTimeout < RuntimeError; end
38
+ class RequestTimeout < Exception
39
+ ErrorMessage = 'Request timed out'
40
+ end
15
41
 
16
42
  # The request failed, meaning the remote HTTP server returned a code other
17
43
  # than success, unauthorized, or redirect.
@@ -22,19 +48,19 @@ module RestClient
22
48
  # You can get the status code by e.http_code, or see anything about the
23
49
  # response via e.response. For example, the entire result body (which is
24
50
  # probably an HTML error page) is e.response.body.
25
- class RequestFailed < RuntimeError
51
+ class RequestFailed < Exception
26
52
  attr_accessor :response
27
53
 
28
- def initialize(response)
54
+ def initialize(response=nil)
29
55
  @response = response
30
56
  end
31
57
 
32
58
  def http_code
33
- @response.code.to_i
59
+ @response.code.to_i if @response
34
60
  end
35
61
 
36
62
  def message(default="Unknown error, HTTP status code #{http_code}")
37
- return "Resource not found" if http_code == 404
63
+ return default unless @response
38
64
  parse_error_xml rescue default
39
65
  end
40
66
 
@@ -50,6 +76,8 @@ module RestClient
50
76
  end
51
77
 
52
78
  # backwards compatibility
53
- RestClient::Resource::Redirect = RestClient::Redirect
54
- RestClient::Resource::Unauthorized = RestClient::Unauthorized
55
- RestClient::Resource::RequestFailed = RestClient::RequestFailed
79
+ class RestClient::Request
80
+ Redirect = RestClient::Redirect
81
+ Unauthorized = RestClient::Unauthorized
82
+ RequestFailed = RestClient::RequestFailed
83
+ end
data/lib/resource.rb CHANGED
@@ -70,13 +70,13 @@ module RestClient
70
70
  # This is especially useful if you wish to define your site in one place and
71
71
  # call it in multiple locations:
72
72
  #
73
- # def product(id)
74
- # RestClient::Resource.new('http://example.com/products/#{id}', 'adam', 'mypasswd')
73
+ # def orders
74
+ # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
75
75
  # end
76
76
  #
77
- # product(123).get
78
- # product(123).put params.to_xml
79
- # product(123).delete
77
+ # orders.get # GET http://example.com/orders
78
+ # orders['1'].get # GET http://example.com/orders/1
79
+ # orders['1/items'].delete # DELETE http://example.com/orders/1/items
80
80
  #
81
81
  # Nest resources as far as you want:
82
82
  #
@@ -91,8 +91,10 @@ module RestClient
91
91
  end
92
92
 
93
93
  def concat_urls(url, suburl) # :nodoc:
94
+ url = url.to_s
95
+ suburl = suburl.to_s
94
96
  if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
95
- "#{url}#{suburl}"
97
+ url + suburl
96
98
  else
97
99
  "#{url}/#{suburl}"
98
100
  end
data/lib/rest_client.rb CHANGED
@@ -52,7 +52,7 @@ module RestClient
52
52
  def execute
53
53
  execute_inner
54
54
  rescue Redirect => e
55
- @url = e.message
55
+ @url = e.url
56
56
  execute
57
57
  end
58
58
 
@@ -124,6 +124,8 @@ module RestClient
124
124
  raise Redirect, res.header['Location']
125
125
  elsif res.code == "401"
126
126
  raise Unauthorized
127
+ elsif res.code == "404"
128
+ raise ResourceNotFound
127
129
  else
128
130
  raise RequestFailed, res
129
131
  end
@@ -1,8 +1,19 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
+ describe RestClient::Exception do
4
+ it "sets the exception message to ErrorMessage" do
5
+ RestClient::ResourceNotFound.new.message.should == 'Resource not found'
6
+ end
7
+
8
+ it "contains exceptions in RestClient" do
9
+ RestClient::Unauthorized.new.should be_a_kind_of(RestClient::Exception)
10
+ RestClient::ServerBrokeConnection.new.should be_a_kind_of(RestClient::Exception)
11
+ end
12
+ end
13
+
3
14
  describe RestClient::RequestFailed do
4
15
  before do
5
- @error = RestClient::RequestFailed.new(nil)
16
+ @error = RestClient::RequestFailed.new
6
17
  end
7
18
 
8
19
  it "extracts the error message from xml" do
@@ -27,15 +38,15 @@ describe RestClient::RequestFailed do
27
38
  end
28
39
 
29
40
  describe "backwards compatibility" do
30
- it "alias RestClient::Resource::Redirect to RestClient::Redirect" do
31
- RestClient::Resource::Redirect.should == RestClient::Redirect
41
+ it "alias RestClient::Request::Redirect to RestClient::Redirect" do
42
+ RestClient::Request::Redirect.should == RestClient::Redirect
32
43
  end
33
44
 
34
- it "alias RestClient::Resource::Unauthorized to RestClient::Unauthorized" do
35
- RestClient::Resource::Unauthorized.should == RestClient::Unauthorized
45
+ it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
46
+ RestClient::Request::Unauthorized.should == RestClient::Unauthorized
36
47
  end
37
48
 
38
- it "alias RestClient::Resource::RequestFailed to RestClient::RequestFailed" do
39
- RestClient::Resource::RequestFailed.should == RestClient::RequestFailed
49
+ it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
50
+ RestClient::Request::RequestFailed.should == RestClient::RequestFailed
40
51
  end
41
52
  end
@@ -41,8 +41,8 @@ describe RestClient::Resource do
41
41
  @resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
42
42
  end
43
43
 
44
- it "concatinates even non-string urls (i.e. 'posts' + 1)" do
45
- @resource.concat_urls('posts/', 1).should == 'posts/1'
44
+ it "concatinates even non-string urls, :posts + 1 => 'posts/1'" do
45
+ @resource.concat_urls(:posts, 1).should == 'posts/1'
46
46
  end
47
47
 
48
48
  it "offers subresources via []" do
@@ -181,7 +181,7 @@ describe RestClient do
181
181
 
182
182
  it "raises a Redirect with the new location when the response is in the 30x range" do
183
183
  res = mock('response', :code => '301', :header => { 'Location' => 'http://new/resource' })
184
- lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect, 'http://new/resource')
184
+ lambda { @request.process_result(res) }.should raise_error(RestClient::Redirect) { |e| e.url.should == 'http://new/resource'}
185
185
  end
186
186
 
187
187
  it "raises Unauthorized when the response is 401" do
@@ -189,6 +189,11 @@ describe RestClient do
189
189
  lambda { @request.process_result(res) }.should raise_error(RestClient::Unauthorized)
190
190
  end
191
191
 
192
+ it "raises ResourceNotFound when the response is 404" do
193
+ res = mock('response', :code => '404')
194
+ lambda { @request.process_result(res) }.should raise_error(RestClient::ResourceNotFound)
195
+ end
196
+
192
197
  it "raises RequestFailed otherwise" do
193
198
  res = mock('response', :code => '500')
194
199
  lambda { @request.process_result(res) }.should raise_error(RestClient::RequestFailed)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-client
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.5"
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Wiggins
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-06-20 00:00:00 -07:00
12
+ date: 2008-07-01 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  requirements: []
53
53
 
54
54
  rubyforge_project: rest-client
55
- rubygems_version: 1.1.1
55
+ rubygems_version: 1.2.0
56
56
  signing_key:
57
57
  specification_version: 2
58
58
  summary: Simple REST client for Ruby, inspired by microframework syntax for specifying actions.