Class: RestClient::Resource
- Inherits:
-
Object
- Object
- RestClient::Resource
- Defined in:
- lib/vendor/rest-client/lib/rest_client/resource.rb
Overview
site.post ‘Good article.’, :content_type => ‘text/plain’
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#[](suburl) ⇒ Object
Construct a subresource, preserving authentication.
-
#concat_urls(url, suburl) ⇒ Object
:nodoc:.
- #delete(headers = {}, &b) ⇒ Object
- #get(headers = {}, &b) ⇒ Object
-
#initialize(url, user = nil, password = nil) ⇒ Resource
constructor
A new instance of Resource.
- #post(payload, headers = {}, &b) ⇒ Object
- #put(payload, headers = {}, &b) ⇒ Object
Constructor Details
#initialize(url, user = nil, password = nil) ⇒ Resource
Returns a new instance of Resource.
23 24 25 26 27 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 23 def initialize(url, user=nil, password=nil) @url = url @user = user @password = password end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
21 22 23 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 21 def password @password end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
21 22 23 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 21 def url @url end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
21 22 23 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 21 def user @user end |
Instance Method Details
#[](suburl) ⇒ Object
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'
89 90 91 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 89 def [](suburl) self.class.new(concat_urls(url, suburl), user, password) end |
#concat_urls(url, suburl) ⇒ Object
:nodoc:
93 94 95 96 97 98 99 100 101 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 93 def concat_urls(url, suburl) # :nodoc: url = url.to_s suburl = suburl.to_s if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/' url + suburl else "#{url}/#{suburl}" end end |
#delete(headers = {}, &b) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 55 def delete(headers={}, &b) Request.execute(:method => :delete, :url => url, :user => user, :password => password, :headers => headers, &b) end |
#get(headers = {}, &b) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 29 def get(headers={}, &b) Request.execute(:method => :get, :url => url, :user => user, :password => password, :headers => headers, &b) end |
#post(payload, headers = {}, &b) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 37 def post(payload, headers={}, &b) Request.execute(:method => :post, :url => url, :payload => payload, :user => user, :password => password, :headers => headers, &b) end |
#put(payload, headers = {}, &b) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/vendor/rest-client/lib/rest_client/resource.rb', line 46 def put(payload, headers={}, &b) Request.execute(:method => :put, :url => url, :payload => payload, :user => user, :password => password, :headers => headers, &b) end |