Parent

RestClient::Resource

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'

Attributes

url[R]
options[R]

Public Class Methods

new(url, options={}, backwards_compatibility=nil) click to toggle source
    # File lib/restclient/resource.rb, line 39
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

Public Instance Methods

[](suburl) click to toggle source

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/restclient/resource.rb, line 132
132:                 def [](suburl)
133:                         self.class.new(concat_urls(url, suburl), options)
134:                 end
delete(additional_headers={}) click to toggle source
    # File lib/restclient/resource.rb, line 74
74:                 def delete(additional_headers={})
75:                         Request.execute(options.merge(
76:                                 :method => :delete,
77:                                 :url => url,
78:                                 :headers => headers.merge(additional_headers)
79:                         ))
80:                 end
get(additional_headers={}) click to toggle source
    # File lib/restclient/resource.rb, line 48
48:                 def get(additional_headers={})
49:                         Request.execute(options.merge(
50:                                 :method => :get,
51:                                 :url => url,
52:                                 :headers => headers.merge(additional_headers)
53:                         ))
54:                 end
headers() click to toggle source
    # File lib/restclient/resource.rb, line 94
94:                 def headers
95:                         options[:headers] || {}
96:                 end
open_timeout() click to toggle source
     # File lib/restclient/resource.rb, line 102
102:                 def open_timeout
103:                   options[:open_timeout]
104:           end
password() click to toggle source
    # File lib/restclient/resource.rb, line 90
90:                 def password
91:                         options[:password]
92:                 end
post(payload, additional_headers={}) click to toggle source
    # File lib/restclient/resource.rb, line 56
56:                 def post(payload, additional_headers={})
57:                         Request.execute(options.merge(
58:                                 :method => :post,
59:                                 :url => url,
60:                                 :payload => payload,
61:                                 :headers => headers.merge(additional_headers)
62:                         ))
63:                 end
put(payload, additional_headers={}) click to toggle source
    # File lib/restclient/resource.rb, line 65
65:                 def put(payload, additional_headers={})
66:                         Request.execute(options.merge(
67:                                 :method => :put,
68:                                 :url => url,
69:                                 :payload => payload,
70:                                 :headers => headers.merge(additional_headers)
71:                         ))
72:                 end
timeout() click to toggle source
     # File lib/restclient/resource.rb, line 98
 98:                 def timeout
 99:                         options[:timeout]
100:                 end
to_s() click to toggle source
    # File lib/restclient/resource.rb, line 82
82:                 def to_s
83:                         url
84:                 end
user() click to toggle source
    # File lib/restclient/resource.rb, line 86
86:                 def user
87:                         options[:user]
88:                 end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.