Class: RestClient::Resource

Class | RestClient::Resource
In: | lib/restclient/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 => 'user', :password => 'password')
      resource.delete

With a timeout (seconds):

      RestClient::Resource.new('http://slow', :timeout => 10)

With an open timeout (seconds):

      RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)

You can also use resources to share common headers. For headers keys, symbols are converted to strings. Example:

      resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })

This header will be transported as X-Client-Version (notice the X prefix, capitalization and hyphens)

Use the [] syntax to allocate subresources:

      site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
      site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'

### Methods

[] delete get headers new open_timeout password post put timeout to_s user

### Attributes

options | [R] |
url | [R] |

### Public Class methods

new(url, options={}, backwards_compatibility=nil)

### Public Instance methods

[](suburl)

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'

delete(additional_headers={})
get(additional_headers={})
headers()
open_timeout()
password()
post(payload, additional_headers={})
put(payload, additional_headers={})
timeout()
to_s()
user()

[Validate]