Class: Rest::InternalClient::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/rest/wrappers/internal_client/internal/resource.rb

Overview

A class that can be instantiated for access to a RESTful resource, including authentication.

Example:

resource = InternalClient::Resource.new('http://some/resource')
jpg = resource.get(:accept => 'image/jpg')

With HTTP basic authentication:

resource = InternalClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
resource.delete

With a timeout (seconds):

InternalClient::Resource.new('http://slow', :timeout => 10)

With an open timeout (seconds):

InternalClient::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 = InternalClient::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 = InternalClient::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.



40
41
42
43
44
45
46
47
48
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 40

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.



38
39
40
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 38

def block
  @block
end

#optionsObject (readonly)

Returns the value of attribute options.



38
39
40
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 38

def options
  @options
end

#urlObject (readonly)

Returns the value of attribute url.



38
39
40
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 38

def url
  @url
end

Instance Method Details

#[](suburl, &new_block) ⇒ Object

Construct a subresource, preserving authentication.

Example:

site = InternalClient::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
  InternalClient::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 = InternalClient::Resource.new('http://example.com')
posts = site['posts']
first_post = posts['1']
comments = first_post['comments']
comments.post 'Hello', :content_type => 'text/plain'


151
152
153
154
155
156
157
158
159
160
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 151

def [](suburl, &new_block)
  case
    when block_given? then
      self.class.new(concat_urls(url, suburl), options, &new_block)
    when block then
      self.class.new(concat_urls(url, suburl), options, &block)
    else
      self.class.new(concat_urls(url, suburl), options)
  end
end

#concat_urls(url, suburl) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
169
170
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 162

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



93
94
95
96
97
98
99
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 93

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



50
51
52
53
54
55
56
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 50

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

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



58
59
60
61
62
63
64
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 58

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

#headersObject



113
114
115
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 113

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

#open_timeoutObject



121
122
123
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 121

def open_timeout
  options[:open_timeout]
end

#passwordObject



109
110
111
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 109

def password
  options[:password]
end

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



84
85
86
87
88
89
90
91
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 84

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

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



66
67
68
69
70
71
72
73
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 66

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



75
76
77
78
79
80
81
82
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 75

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



117
118
119
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 117

def timeout
  options[:timeout]
end

#to_sObject



101
102
103
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 101

def to_s
  url
end

#userObject



105
106
107
# File 'lib/rest/wrappers/internal_client/internal/resource.rb', line 105

def user
  options[:user]
end