mirror of
https://github.com/rest-client/rest-client.git
synced 2022-11-09 13:49:40 -05:00
subresources with []
This commit is contained in:
parent
7c7e62515c
commit
76b910b061
2 changed files with 60 additions and 0 deletions
|
@ -12,6 +12,11 @@ module RestClient
|
|||
# resource = RestClient::Resource.new('http://protected/resource', 'user', 'pass')
|
||||
# resource.delete
|
||||
#
|
||||
# Use the [] syntax to allocate subresources:
|
||||
#
|
||||
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
||||
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
||||
#
|
||||
class Resource
|
||||
attr_reader :url, :user, :password
|
||||
|
||||
|
@ -54,5 +59,43 @@ module RestClient
|
|||
:password => password,
|
||||
:headers => headers)
|
||||
end
|
||||
|
||||
# Construct a subresource, preserving authentication.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
|
||||
# site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
|
||||
#
|
||||
# This is especially useful if you wish to define your site in one place and
|
||||
# call it in multiple locations:
|
||||
#
|
||||
# def product(id)
|
||||
# RestClient::Resource.new('http://example.com/products/#{id}', 'adam', 'mypasswd')
|
||||
# end
|
||||
#
|
||||
# product(123).get
|
||||
# product(123).put params.to_xml
|
||||
# product(123).delete
|
||||
#
|
||||
# Nest resources as far as you want:
|
||||
#
|
||||
# site = RestClient::Resource.new('http://example.com')
|
||||
# posts = site['posts']
|
||||
# first_post = posts['1']
|
||||
# comments = first_post['comments']
|
||||
# comments.post 'Hello', :content_type => 'text/plain'
|
||||
#
|
||||
def [](suburl)
|
||||
self.class.new(concat_urls(url, suburl), user, password)
|
||||
end
|
||||
|
||||
def concat_urls(url, suburl) # :nodoc:
|
||||
if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
|
||||
url + suburl
|
||||
else
|
||||
"#{url}/#{suburl}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -28,4 +28,21 @@ describe RestClient::Resource do
|
|||
it "can instantiate with no user/password" do
|
||||
@resource = RestClient::Resource.new('http://some/resource')
|
||||
end
|
||||
|
||||
it "concatinates urls, inserting a slash when it needs one" do
|
||||
@resource.concat_urls('http://example.com', 'resource').should == 'http://example.com/resource'
|
||||
end
|
||||
|
||||
it "concatinates urls, using no slash if the first url ends with a slash" do
|
||||
@resource.concat_urls('http://example.com/', 'resource').should == 'http://example.com/resource'
|
||||
end
|
||||
|
||||
it "concatinates urls, using no slash if the second url starts with a slash" do
|
||||
@resource.concat_urls('http://example.com', '/resource').should == 'http://example.com/resource'
|
||||
end
|
||||
|
||||
it "offers subresources via []" do
|
||||
parent = RestClient::Resource.new('http://example.com')
|
||||
parent['posts'].url.should == 'http://example.com/posts'
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue