class RestClient::Resource
.post ‘Good article.’, :content_type => ‘text/plain’
site = RestClient::Resource.new(‘example.com’, :user => ‘adam’, :password => ‘mypasswd’)
Use the [] syntax to allocate subresources:
capitalization and hyphens)
This header will be transported as X-Client-Version (notice the X prefix,
resource = RestClient::Resource.new(‘some/resource’, :headers => { :client_version => 1 })
symbols are converted to strings. Example:
You can also use resources to share common headers. For headers keys,
RestClient::Resource.new(‘behindfirewall’, :open_timeout => 10)
With an open timeout (seconds):
RestClient::Resource.new(‘slow’, :timeout => 10)
With a timeout (seconds):
resource.delete
resource = RestClient::Resource.new(‘protected/resource’, :user => ‘user’, :password => ‘password’)
With HTTP basic authentication:
jpg = resource.get(:accept => ‘image/jpg’)
resource = RestClient::Resource.new(‘some/resource’)
Example:
including authentication.
A class that can be instantiated for access to a RESTful resource,
def [](suburl)
comments.post 'Hello', :content_type => 'text/plain'
comments = first_post['comments']
first_post = posts['1']
posts = site['posts']
site = RestClient::Resource.new('http://example.com')
Nest resources as far as you want:
orders['1/items'].delete # DELETE http://example.com/orders/1/items
orders['1'].get # GET http://example.com/orders/1
orders.get # GET http://example.com/orders
end
RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
def orders
call it in multiple locations:
This is especially useful if you wish to define your site in one place and
site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
Example:
Construct a subresource, preserving authentication.
def [](suburl) self.class.new(concat_urls(url, suburl), options) end
def concat_urls(url, suburl) # :nodoc:
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
def delete(additional_headers={}, &block)
def delete(additional_headers={}, &block) headers = (options[:headers] || {}).merge(additional_headers) Request.execute(options.merge( :method => :delete, :url => url, :headers => headers), &(block || @block)) end
def get(additional_headers={}, &block)
def get(additional_headers={}, &block) headers = (options[:headers] || {}).merge(additional_headers) Request.execute(options.merge( :method => :get, :url => url, :headers => headers), &(block || @block)) end
def headers
def headers options[:headers] || {} end
def initialize(url, options={}, backwards_compatibility=nil, &block)
def initialize(url, options={}, backwards_compatibility=nil, &block) @url = url @block = block if options.class == Hash @options = options else # compatibility with previous versions @options = { :user => options, :password => backwards_compatibility } end end
def open_timeout
def open_timeout options[:open_timeout] end
def password
def password options[:password] end
def post(payload, additional_headers={}, &block)
def post(payload, additional_headers={}, &block) headers = (options[:headers] || {}).merge(additional_headers) Request.execute(options.merge( :method => :post, :url => url, :payload => payload, :headers => headers), &(block || @block)) end
def put(payload, additional_headers={}, &block)
def put(payload, additional_headers={}, &block) headers = (options[:headers] || {}).merge(additional_headers) Request.execute(options.merge( :method => :put, :url => url, :payload => payload, :headers => headers), &(block || @block)) end
def timeout
def timeout options[:timeout] end
def to_s
def to_s url end
def user
def user options[:user] end