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 => 'user', :password => 'password')
resource.delete

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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options = {}, backwards_compatibility = nil) ⇒ Resource

Returns a new instance of Resource.



31
32
33
34
35
36
37
38
# File 'lib/resource.rb', line 31

def initialize(url, options={}, backwards_compatibility=nil)
	@url = url
	if options.class == Hash
		@options = options
	else # compatibility with previous versions
		@options = { :user => options, :password => backwards_compatibility }
	end
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



29
30
31
# File 'lib/resource.rb', line 29

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



29
30
31
# File 'lib/resource.rb', line 29

def url
  @url
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'


116
117
118
# File 'lib/resource.rb', line 116

def [](suburl)
	self.class.new(concat_urls(url, suburl), options)
end

#concat_urls(url, suburl) ⇒ Object

:nodoc:



120
121
122
123
124
125
126
127
128
# File 'lib/resource.rb', line 120

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(additional_headers = {}) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/resource.rb', line 66

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

#get(additional_headers = {}) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/resource.rb', line 40

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

#headersObject



86
87
88
# File 'lib/resource.rb', line 86

def headers
	options[:headers] || {}
end

#passwordObject



82
83
84
# File 'lib/resource.rb', line 82

def password
	options[:password]
end

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



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

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

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



57
58
59
60
61
62
63
64
# File 'lib/resource.rb', line 57

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

#to_sObject



74
75
76
# File 'lib/resource.rb', line 74

def to_s
	url
end

#userObject



78
79
80
# File 'lib/resource.rb', line 78

def user
	options[:user]
end