Class: RestClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/resource.rb

Overview

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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, user = nil, password = nil) ⇒ Resource

Returns a new instance of Resource.



23
24
25
26
27
# File 'lib/resource.rb', line 23

def initialize(url, user=nil, password=nil)
	@url = url
	@user = user
	@password = password
end

Instance Attribute Details

#passwordObject (readonly)

Returns the value of attribute password.



21
22
23
# File 'lib/resource.rb', line 21

def password
  @password
end

#urlObject (readonly)

Returns the value of attribute url.



21
22
23
# File 'lib/resource.rb', line 21

def url
  @url
end

#userObject (readonly)

Returns the value of attribute user.



21
22
23
# File 'lib/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/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/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 = {}) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/resource.rb', line 55

def delete(headers={})
	Request.execute(:method => :delete,
		:url => url,
		:user => user,
		:password => password,
		:headers => headers)
end

#get(headers = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/resource.rb', line 29

def get(headers={})
	Request.execute(:method => :get,
		:url => url,
		:user => user,
		:password => password,
		:headers => headers)
end

#post(payload, headers = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/resource.rb', line 37

def post(payload, headers={})
	Request.execute(:method => :post,
		:url => url,
		:payload => payload,
		:user => user,
		:password => password,
		:headers => headers)
end

#put(payload, headers = {}) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/resource.rb', line 46

def put(payload, headers={})
	Request.execute(:method => :put,
		:url => url,
		:payload => payload,
		:user => user,
		:password => password,
		:headers => headers)
end