rest-client-next 1.1.0 → 1.3.0

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.
@@ -1,146 +1,147 @@
1
1
  module RestClient
2
- # A class that can be instantiated for access to a RESTful resource,
3
- # including authentication.
4
- #
5
- # Example:
6
- #
7
- # resource = RestClient::Resource.new('http://some/resource')
8
- # jpg = resource.get(:accept => 'image/jpg')
9
- #
10
- # With HTTP basic authentication:
11
- #
12
- # resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
13
- # resource.delete
14
- #
15
- # With a timeout (seconds):
16
- #
17
- # RestClient::Resource.new('http://slow', :timeout => 10)
18
- #
19
- # With an open timeout (seconds):
20
- #
21
- # RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
22
- #
23
- # You can also use resources to share common headers. For headers keys,
24
- # symbols are converted to strings. Example:
25
- #
26
- # resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
27
- #
28
- # This header will be transported as X-Client-Version (notice the X prefix,
29
- # capitalization and hyphens)
30
- #
31
- # Use the [] syntax to allocate subresources:
32
- #
33
- # site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
34
- # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
35
- #
36
- class Resource
37
- attr_reader :url, :options
2
+ # A class that can be instantiated for access to a RESTful resource,
3
+ # including authentication.
4
+ #
5
+ # Example:
6
+ #
7
+ # resource = RestClient::Resource.new('http://some/resource')
8
+ # jpg = resource.get(:accept => 'image/jpg')
9
+ #
10
+ # With HTTP basic authentication:
11
+ #
12
+ # resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
13
+ # resource.delete
14
+ #
15
+ # With a timeout (seconds):
16
+ #
17
+ # RestClient::Resource.new('http://slow', :timeout => 10)
18
+ #
19
+ # With an open timeout (seconds):
20
+ #
21
+ # RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
22
+ #
23
+ # You can also use resources to share common headers. For headers keys,
24
+ # symbols are converted to strings. Example:
25
+ #
26
+ # resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
27
+ #
28
+ # This header will be transported as X-Client-Version (notice the X prefix,
29
+ # capitalization and hyphens)
30
+ #
31
+ # Use the [] syntax to allocate subresources:
32
+ #
33
+ # site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
34
+ # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
35
+ #
36
+ class Resource
37
+ attr_reader :url, :options, :block
38
38
 
39
- def initialize(url, options={}, backwards_compatibility=nil)
40
- @url = url
41
- if options.class == Hash
42
- @options = options
43
- else # compatibility with previous versions
44
- @options = { :user => options, :password => backwards_compatibility }
45
- end
46
- end
39
+ def initialize(url, options={}, backwards_compatibility=nil, &block)
40
+ @url = url
41
+ @block = block
42
+ if options.class == Hash
43
+ @options = options
44
+ else # compatibility with previous versions
45
+ @options = { :user => options, :password => backwards_compatibility }
46
+ end
47
+ end
47
48
 
48
- def get(additional_headers={}, &b)
49
- headers = (options[:headers] || {}).merge(additional_headers)
50
- Request.execute(options.merge(
51
- :method => :get,
52
- :url => url,
53
- :headers => headers), &b)
54
- end
49
+ def get(additional_headers={}, &block)
50
+ headers = (options[:headers] || {}).merge(additional_headers)
51
+ Request.execute(options.merge(
52
+ :method => :get,
53
+ :url => url,
54
+ :headers => headers), &(block || @block))
55
+ end
55
56
 
56
- def post(payload, additional_headers={}, &b)
57
- headers = (options[:headers] || {}).merge(additional_headers)
58
- Request.execute(options.merge(
59
- :method => :post,
60
- :url => url,
61
- :payload => payload,
62
- :headers => headers), &b)
63
- end
57
+ def post(payload, additional_headers={}, &block)
58
+ headers = (options[:headers] || {}).merge(additional_headers)
59
+ Request.execute(options.merge(
60
+ :method => :post,
61
+ :url => url,
62
+ :payload => payload,
63
+ :headers => headers), &(block || @block))
64
+ end
64
65
 
65
- def put(payload, additional_headers={}, &b)
66
- headers = (options[:headers] || {}).merge(additional_headers)
67
- Request.execute(options.merge(
68
- :method => :put,
69
- :url => url,
70
- :payload => payload,
71
- :headers => headers), &b)
72
- end
66
+ def put(payload, additional_headers={}, &block)
67
+ headers = (options[:headers] || {}).merge(additional_headers)
68
+ Request.execute(options.merge(
69
+ :method => :put,
70
+ :url => url,
71
+ :payload => payload,
72
+ :headers => headers), &(block || @block))
73
+ end
73
74
 
74
- def delete(additional_headers={}, &b)
75
- headers = (options[:headers] || {}).merge(additional_headers)
76
- Request.execute(options.merge(
77
- :method => :delete,
78
- :url => url,
79
- :headers => headers), &b)
80
- end
75
+ def delete(additional_headers={}, &block)
76
+ headers = (options[:headers] || {}).merge(additional_headers)
77
+ Request.execute(options.merge(
78
+ :method => :delete,
79
+ :url => url,
80
+ :headers => headers), &(block || @block))
81
+ end
81
82
 
82
- def to_s
83
- url
84
- end
83
+ def to_s
84
+ url
85
+ end
85
86
 
86
- def user
87
- options[:user]
88
- end
87
+ def user
88
+ options[:user]
89
+ end
89
90
 
90
- def password
91
- options[:password]
92
- end
91
+ def password
92
+ options[:password]
93
+ end
93
94
 
94
- def headers
95
- options[:headers] || {}
96
- end
95
+ def headers
96
+ options[:headers] || {}
97
+ end
97
98
 
98
- def timeout
99
- options[:timeout]
100
- end
101
-
102
- def open_timeout
103
- options[:open_timeout]
104
- end
99
+ def timeout
100
+ options[:timeout]
101
+ end
105
102
 
106
- # Construct a subresource, preserving authentication.
107
- #
108
- # Example:
109
- #
110
- # site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
111
- # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
112
- #
113
- # This is especially useful if you wish to define your site in one place and
114
- # call it in multiple locations:
115
- #
116
- # def orders
117
- # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
118
- # end
119
- #
120
- # orders.get # GET http://example.com/orders
121
- # orders['1'].get # GET http://example.com/orders/1
122
- # orders['1/items'].delete # DELETE http://example.com/orders/1/items
123
- #
124
- # Nest resources as far as you want:
125
- #
126
- # site = RestClient::Resource.new('http://example.com')
127
- # posts = site['posts']
128
- # first_post = posts['1']
129
- # comments = first_post['comments']
130
- # comments.post 'Hello', :content_type => 'text/plain'
131
- #
132
- def [](suburl)
133
- self.class.new(concat_urls(url, suburl), options)
134
- end
103
+ def open_timeout
104
+ options[:open_timeout]
105
+ end
135
106
 
136
- def concat_urls(url, suburl) # :nodoc:
137
- url = url.to_s
138
- suburl = suburl.to_s
139
- if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
140
- url + suburl
141
- else
142
- "#{url}/#{suburl}"
143
- end
144
- end
145
- end
107
+ # Construct a subresource, preserving authentication.
108
+ #
109
+ # Example:
110
+ #
111
+ # site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
112
+ # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
113
+ #
114
+ # This is especially useful if you wish to define your site in one place and
115
+ # call it in multiple locations:
116
+ #
117
+ # def orders
118
+ # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
119
+ # end
120
+ #
121
+ # orders.get # GET http://example.com/orders
122
+ # orders['1'].get # GET http://example.com/orders/1
123
+ # orders['1/items'].delete # DELETE http://example.com/orders/1/items
124
+ #
125
+ # Nest resources as far as you want:
126
+ #
127
+ # site = RestClient::Resource.new('http://example.com')
128
+ # posts = site['posts']
129
+ # first_post = posts['1']
130
+ # comments = first_post['comments']
131
+ # comments.post 'Hello', :content_type => 'text/plain'
132
+ #
133
+ def [](suburl)
134
+ self.class.new(concat_urls(url, suburl), options)
135
+ end
136
+
137
+ def concat_urls(url, suburl) # :nodoc:
138
+ url = url.to_s
139
+ suburl = suburl.to_s
140
+ if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
141
+ url + suburl
142
+ else
143
+ "#{url}/#{suburl}"
144
+ end
145
+ end
146
+ end
146
147
  end
@@ -1,20 +1,20 @@
1
1
  require File.dirname(__FILE__) + '/mixin/response'
2
2
 
3
3
  module RestClient
4
- # The response from RestClient looks like a string, but is actually one of
5
- # these. 99% of the time you're making a rest call all you care about is
6
- # the body, but on the occassion you want to fetch the headers you can:
7
- #
8
- # RestClient.get('http://example.com').headers[:content_type]
9
- #
10
- class Response < String
4
+ # The response from RestClient looks like a string, but is actually one of
5
+ # these. 99% of the time you're making a rest call all you care about is
6
+ # the body, but on the occassion you want to fetch the headers you can:
7
+ #
8
+ # RestClient.get('http://example.com').headers[:content_type]
9
+ #
10
+ class Response < String
11
11
 
12
- include RestClient::Mixin::Response
12
+ include RestClient::Mixin::Response
13
13
 
14
- def initialize(string, net_http_res)
15
- @net_http_res = net_http_res
16
- super(string || "")
17
- end
14
+ def initialize(string, net_http_res)
15
+ @net_http_res = net_http_res
16
+ super(string || "")
17
+ end
18
18
 
19
- end
19
+ end
20
20
  end
@@ -1,65 +1,63 @@
1
1
  require File.dirname(__FILE__) + '/base'
2
2
 
3
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
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
12
  end
13
13
 
14
14
  describe RestClient::RequestFailed do
15
- before do
16
- @response = mock('HTTP Response', :code => '502')
17
- end
18
-
19
- it "stores the http response on the exception" do
20
- begin
21
- raise RestClient::RequestFailed, :response
22
- rescue RestClient::RequestFailed => e
23
- e.response.should == :response
24
- end
25
- end
26
-
27
- it "http_code convenience method for fetching the code as an integer" do
28
- RestClient::RequestFailed.new(@response).http_code.should == 502
29
- end
30
-
31
- it "http_body convenience method for fetching the body (decoding when necessary)" do
32
- @response.stub!(:[]).with('content-encoding').and_return('gzip')
33
- @response.stub!(:body).and_return('compressed body')
34
- RestClient::Request.should_receive(:decode).with('gzip', 'compressed body').and_return('plain body')
35
- RestClient::RequestFailed.new(@response).http_body.should == 'plain body'
36
- end
37
-
38
- it "shows the status code in the message" do
39
- RestClient::RequestFailed.new(@response).to_s.should match(/502/)
40
- end
15
+ before do
16
+ @response = mock('HTTP Response', :code => '502')
17
+ end
18
+
19
+ it "stores the http response on the exception" do
20
+ begin
21
+ raise RestClient::RequestFailed, :response
22
+ rescue RestClient::RequestFailed => e
23
+ e.response.should == :response
24
+ end
25
+ end
26
+
27
+ it "http_code convenience method for fetching the code as an integer" do
28
+ RestClient::RequestFailed.new(@response).http_code.should == 502
29
+ end
30
+
31
+ it "http_body convenience method for fetching the body (decoding when necessary)" do
32
+ RestClient::RequestFailed.new(@response).http_code.should == 502
33
+ RestClient::RequestFailed.new(@response).message.should == 'HTTP status code 502'
34
+ end
35
+
36
+ it "shows the status code in the message" do
37
+ RestClient::RequestFailed.new(@response).to_s.should match(/502/)
38
+ end
41
39
  end
42
40
 
43
41
  describe RestClient::ResourceNotFound do
44
- it "also has the http response attached" do
45
- begin
46
- raise RestClient::ResourceNotFound, :response
47
- rescue RestClient::ResourceNotFound => e
48
- e.response.should == :response
49
- end
50
- end
42
+ it "also has the http response attached" do
43
+ begin
44
+ raise RestClient::ResourceNotFound, :response
45
+ rescue RestClient::ResourceNotFound => e
46
+ e.response.should == :response
47
+ end
48
+ end
51
49
  end
52
50
 
53
51
  describe "backwards compatibility" do
54
- it "alias RestClient::Request::Redirect to RestClient::Redirect" do
55
- RestClient::Request::Redirect.should == RestClient::Redirect
56
- end
52
+ it "alias RestClient::Request::Redirect to RestClient::Redirect" do
53
+ RestClient::Request::Redirect.should == RestClient::Redirect
54
+ end
57
55
 
58
- it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
59
- RestClient::Request::Unauthorized.should == RestClient::Unauthorized
60
- end
56
+ it "alias RestClient::Request::Unauthorized to RestClient::Unauthorized" do
57
+ RestClient::Request::Unauthorized.should == RestClient::Unauthorized
58
+ end
61
59
 
62
- it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
63
- RestClient::Request::RequestFailed.should == RestClient::RequestFailed
64
- end
60
+ it "alias RestClient::Request::RequestFailed to RestClient::RequestFailed" do
61
+ RestClient::Request::RequestFailed.should == RestClient::RequestFailed
62
+ end
65
63
  end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ require 'webmock/rspec'
4
+ include WebMock
5
+
6
+ describe RestClient do
7
+
8
+ it "a simple request" do
9
+ body = 'abc'
10
+ stub_request(:get, "www.example.com").to_return(:body => body, :status => 200)
11
+ response = RestClient.get "www.example.com"
12
+ response.code.should == 200
13
+ response.should == body
14
+ end
15
+
16
+ it "a simple request with gzipped content" do
17
+ stub_request(:get, "www.example.com").with(:headers => { 'Accept-Encoding' => 'gzip, deflate' }).to_return(:body => "\037\213\b\b\006'\252H\000\003t\000\313T\317UH\257\312,HM\341\002\000G\242(\r\v\000\000\000", :status => 200, :headers => { 'Content-Encoding' => 'gzip' } )
18
+ response = RestClient.get "www.example.com"
19
+ response.code.should == 200
20
+ response.should == "i'm gziped\n"
21
+ end
22
+
23
+ it "a 404" do
24
+ body = "Ho hai ! I'm not here !"
25
+ stub_request(:get, "www.example.com").to_return(:body => body, :status => 404)
26
+ begin
27
+ RestClient.get "www.example.com"
28
+ raise
29
+ rescue RestClient::ResourceNotFound => e
30
+ e.http_code.should == 404
31
+ e.response.code.should == 404
32
+ e.response.should == body
33
+ e.http_body.should == body
34
+ end
35
+ end
36
+
37
+
38
+ end