| Class | RestClient::Resource |
| In: |
lib/resource.rb
|
| Parent: | Object |
A class that can be instantiated for access to a RESTful resource, including authentication.
Example:
resource = RestClient::Resource.new('http://some/resource')
jpg = resource.get(:accept => 'image/jpg')
With HTTP basic authentication:
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'
| password | [R] | |
| url | [R] | |
| user | [R] |
# File lib/resource.rb, line 23
23: def initialize(url, user=nil, password=nil)
24: @url = url
25: @user = user
26: @password = password
27: 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 orders
RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
end
orders.get # GET http://example.com/orders
orders['1'].get # GET http://example.com/orders/1
orders['1/items'].delete # DELETE http://example.com/orders/1/items
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'
# File lib/resource.rb, line 89
89: def [](suburl)
90: self.class.new(concat_urls(url, suburl), user, password)
91: end
# File lib/resource.rb, line 55
55: def delete(headers={})
56: Request.execute(:method => :delete,
57: :url => url,
58: :user => user,
59: :password => password,
60: :headers => headers)
61: end
# File lib/resource.rb, line 29
29: def get(headers={})
30: Request.execute(:method => :get,
31: :url => url,
32: :user => user,
33: :password => password,
34: :headers => headers)
35: end
# File lib/resource.rb, line 37
37: def post(payload, headers={})
38: Request.execute(:method => :post,
39: :url => url,
40: :payload => payload,
41: :user => user,
42: :password => password,
43: :headers => headers)
44: end