Class: RestClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/restclient/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

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'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Resource.



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

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



37
38
39
# File 'lib/restclient/resource.rb', line 37

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



37
38
39
# File 'lib/restclient/resource.rb', line 37

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



37
38
39
# File 'lib/restclient/resource.rb', line 37

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'


133
134
135
# File 'lib/restclient/resource.rb', line 133

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

#concat_urls(url, suburl) ⇒ Object

:nodoc:



137
138
139
140
141
142
143
144
145
# File 'lib/restclient/resource.rb', line 137

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 = {}, &block) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/restclient/resource.rb', line 75

def delete(additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :delete,
          :url => url,
          :headers => headers), &(block || @block))
end

#get(additional_headers = {}, &block) ⇒ Object



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

def get(additional_headers={}, &block)
  headers = (options[:headers] || {}).merge(additional_headers)
  Request.execute(options.merge(
          :method => :get,
          :url => url,
          :headers => headers), &(block || @block))
end

#headersObject



95
96
97
# File 'lib/restclient/resource.rb', line 95

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

#open_timeoutObject



103
104
105
# File 'lib/restclient/resource.rb', line 103

def open_timeout
  options[:open_timeout]
end

#passwordObject



91
92
93
# File 'lib/restclient/resource.rb', line 91

def password
  options[:password]
end

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



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

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

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



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

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

#timeoutObject



99
100
101
# File 'lib/restclient/resource.rb', line 99

def timeout
  options[:timeout]
end

#to_sObject



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

def to_s
  url
end

#userObject



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

def user
  options[:user]
end