auser-poolparty 1.2.10 → 1.2.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/VERSION.yml +1 -1
  2. data/bin/cloud +1 -1
  3. data/bin/cloud-list +19 -7
  4. data/bin/cloud-provision +3 -0
  5. data/bin/cloud-run +37 -0
  6. data/bin/server-list-active +1 -1
  7. data/config/jeweler.rb +2 -2
  8. data/examples/basic.rb +4 -3
  9. data/examples/fairchild.rb +2 -2
  10. data/examples/metavirt_cloud.rb +18 -8
  11. data/examples/simple.rb +4 -3
  12. data/examples/vmrun_cloud.rb +2 -5
  13. data/lib/poolparty.rb +1 -1
  14. data/lib/poolparty/core/object.rb +6 -4
  15. data/lib/poolparty/core/string.rb +2 -1
  16. data/lib/poolparty/helpers/optioner.rb +1 -1
  17. data/lib/poolparty/modules/callbacks.rb +2 -0
  18. data/lib/poolparty/modules/cloud_resourcer.rb +4 -5
  19. data/lib/poolparty/modules/daemonizable.rb +4 -5
  20. data/lib/poolparty/modules/definable_resource.rb +1 -0
  21. data/lib/poolparty/modules/pinger.rb +5 -1
  22. data/lib/poolparty/monitors/monitor_daemon.rb +159 -0
  23. data/lib/poolparty/monitors/monitor_rack.rb +21 -11
  24. data/lib/poolparty/monitors/monitors/clock_monitor.rb +5 -0
  25. data/lib/poolparty/monitors/monitors/elections_monitor.rb +49 -0
  26. data/lib/poolparty/monitors/monitors/neighborhood_monitor.rb +7 -7
  27. data/lib/poolparty/monitors/monitors/stats_monitor.rb +15 -3
  28. data/lib/poolparty/net/init.rb +1 -1
  29. data/lib/poolparty/net/remote_instance.rb +43 -39
  30. data/lib/poolparty/net/remoter/connections.rb +6 -3
  31. data/lib/poolparty/net/remoter/interactive.rb +18 -11
  32. data/lib/poolparty/net/remoter_base.rb +39 -12
  33. data/lib/poolparty/net/remoter_bases/ec2/ec2.rb +23 -19
  34. data/lib/poolparty/net/remoter_bases/ec2/ec2_remote_instance.rb +15 -33
  35. data/lib/poolparty/net/remoter_bases/ec2/ec2_response_object.rb +28 -20
  36. data/lib/poolparty/net/remoter_bases/libvirt/libvirt.rb +73 -0
  37. data/lib/poolparty/net/remoter_bases/libvirt/libvirt_instance.rb +64 -0
  38. data/lib/poolparty/net/remoter_bases/metavirt/metavirt.rb +27 -17
  39. data/lib/poolparty/net/remoter_bases/metavirt/metavirt_instance.rb +20 -35
  40. data/lib/poolparty/net/remoter_bases/vmrun/vmrun.rb +35 -17
  41. data/lib/poolparty/net/remoter_bases/vmrun/vmrun_instance.rb +32 -24
  42. data/lib/poolparty/plugins/apache2/apache.rb +1 -1
  43. data/lib/poolparty/plugins/chef/chef.rb +12 -26
  44. data/lib/poolparty/plugins/chef/chef_deploy.rb +5 -23
  45. data/lib/poolparty/plugins/chef/chef_deploy_definition.rb +32 -0
  46. data/lib/poolparty/plugins/chef/chef_library.rb +7 -0
  47. data/lib/poolparty/plugins/chef/chef_recipe.rb +7 -0
  48. data/lib/poolparty/plugins/chef/include_chef_recipe.rb +14 -0
  49. data/lib/poolparty/{resources → plugins}/host.rb +5 -4
  50. data/lib/poolparty/plugins/line_in_file.rb +1 -1
  51. data/lib/poolparty/{resources → plugins}/sshkey.rb +10 -8
  52. data/lib/poolparty/poolparty/cloud.rb +7 -0
  53. data/lib/poolparty/poolparty/default.rb +0 -2
  54. data/lib/poolparty/poolparty/pool.rb +13 -3
  55. data/lib/poolparty/provision/boot_strapper.rb +8 -8
  56. data/lib/poolparty/resources/exec.rb +7 -0
  57. data/lib/poolparty/resources/group.rb +3 -3
  58. data/lib/poolparty/resources/user.rb +6 -1
  59. data/lib/poolparty/templates/monitor.ru +12 -0
  60. data/spec/poolparty/net/remoter_bases/ec2_remote_instance_spec.rb +0 -2
  61. data/spec/poolparty/net/remoter_bases/ec2_spec.rb +7 -4
  62. data/spec/poolparty/poolparty/example_spec.rb +21 -21
  63. data/spec/poolparty/poolparty/pool_spec.rb +2 -1
  64. data/spec/poolparty/resources/sshkey_spec.rb +39 -40
  65. data/tasks/poolparty.rake +27 -1
  66. data/test/fixtures/fake_clouds.rb +11 -0
  67. data/test/poolparty/dependency_resolver/chef_resolver_test.rb +82 -0
  68. data/test/poolparty/monitors/test_base_monitor.rb +2 -2
  69. data/test/poolparty/monitors/test_monitor_rack.rb +11 -24
  70. data/test/poolparty/net/remoter_base_test.rb +4 -5
  71. data/test/poolparty/net/remoter_bases/libvirt/libvirt_test.rb +70 -0
  72. data/test/poolparty/net/remoter_bases/metavirt/metavirt_test.rb +32 -3
  73. data/test/poolparty/plugins/chef_plugin_test.rb +23 -0
  74. data/test/poolparty/poolparty/pool_test.rb +22 -0
  75. data/vendor/gems/dslify/lib/dslify.rb +3 -0
  76. data/vendor/gems/dslify/test/dslify_test.rb +4 -15
  77. data/vendor/gems/git-style-binaries/README.markdown +6 -0
  78. data/vendor/gems/git-style-binaries/Rakefile +1 -1
  79. data/vendor/gems/git-style-binaries/VERSION.yml +1 -1
  80. data/vendor/gems/git-style-binaries/doc/gsb-screencast.png +0 -0
  81. data/vendor/gems/git-style-binaries/doc/poolparty-binaries.screenplay +11 -13
  82. data/vendor/gems/git-style-binaries/git-style-binaries.gemspec +12 -3
  83. data/vendor/gems/git-style-binaries/lib/git-style-binary.rb +15 -1
  84. data/vendor/gems/git-style-binaries/test/running_binaries_test.rb +2 -2
  85. data/vendor/gems/suitcase/VERSION.yml +1 -1
  86. data/vendor/gems/suitcase/lib/suitcase/zipper.rb +73 -25
  87. data/vendor/gems/suitcase/suitcase.gemspec +5 -5
  88. data/vendor/gems/suitcase/test/suitcase_test.rb +12 -6
  89. data/vendor/gems/suitcase/test/test_dir/gems/famoseagle-carrot-0.6.0.gem +0 -0
  90. metadata +24 -52
  91. data/spec/poolparty/resources/host_spec.rb +0 -35
  92. data/vendor/gems/rest-client/README.rdoc +0 -151
  93. data/vendor/gems/rest-client/Rakefile +0 -85
  94. data/vendor/gems/rest-client/bin/restclient +0 -87
  95. data/vendor/gems/rest-client/lib/rest_client.rb +0 -2
  96. data/vendor/gems/rest-client/lib/restclient.rb +0 -93
  97. data/vendor/gems/rest-client/lib/restclient/exceptions.rb +0 -84
  98. data/vendor/gems/rest-client/lib/restclient/mixin/response.rb +0 -43
  99. data/vendor/gems/rest-client/lib/restclient/raw_response.rb +0 -30
  100. data/vendor/gems/rest-client/lib/restclient/request.rb +0 -232
  101. data/vendor/gems/rest-client/lib/restclient/resource.rb +0 -146
  102. data/vendor/gems/rest-client/lib/restclient/response.rb +0 -20
  103. data/vendor/gems/rest-client/rest-client.gemspec +0 -21
  104. data/vendor/gems/rest-client/spec/base.rb +0 -4
  105. data/vendor/gems/rest-client/spec/exceptions_spec.rb +0 -54
  106. data/vendor/gems/rest-client/spec/mixin/response_spec.rb +0 -46
  107. data/vendor/gems/rest-client/spec/raw_response_spec.rb +0 -17
  108. data/vendor/gems/rest-client/spec/request_spec.rb +0 -442
  109. data/vendor/gems/rest-client/spec/resource_spec.rb +0 -75
  110. data/vendor/gems/rest-client/spec/response_spec.rb +0 -16
  111. data/vendor/gems/rest-client/spec/restclient_spec.rb +0 -53
@@ -1,84 +0,0 @@
1
- module RestClient
2
- # This is the base RestClient exception class. Rescue it if you want to
3
- # catch any exception that your request might raise
4
- class Exception < RuntimeError
5
- def message(default=nil)
6
- self.class::ErrorMessage
7
- end
8
- end
9
-
10
- # Base RestClient exception when there's a response available
11
- class ExceptionWithResponse < Exception
12
- attr_accessor :response
13
-
14
- def initialize(response=nil)
15
- @response = response
16
- end
17
-
18
- def http_code
19
- @response.code.to_i if @response
20
- end
21
- end
22
-
23
- # A redirect was encountered; caught by execute to retry with the new url.
24
- class Redirect < Exception
25
- ErrorMessage = "Redirect"
26
-
27
- attr_accessor :url
28
- def initialize(url)
29
- @url = url
30
- end
31
- end
32
-
33
- class NotModified < ExceptionWithResponse
34
- ErrorMessage = 'NotModified'
35
- end
36
-
37
- # Authorization is required to access the resource specified.
38
- class Unauthorized < ExceptionWithResponse
39
- ErrorMessage = 'Unauthorized'
40
- end
41
-
42
- # No resource was found at the given URL.
43
- class ResourceNotFound < ExceptionWithResponse
44
- ErrorMessage = 'Resource not found'
45
- end
46
-
47
- # The server broke the connection prior to the request completing. Usually
48
- # this means it crashed, or sometimes that your network connection was
49
- # severed before it could complete.
50
- class ServerBrokeConnection < Exception
51
- ErrorMessage = 'Server broke connection'
52
- end
53
-
54
- # The server took too long to respond.
55
- class RequestTimeout < Exception
56
- ErrorMessage = 'Request timed out'
57
- end
58
-
59
- # The request failed, meaning the remote HTTP server returned a code other
60
- # than success, unauthorized, or redirect.
61
- #
62
- # The exception message attempts to extract the error from the XML, using
63
- # format returned by Rails: <errors><error>some message</error></errors>
64
- #
65
- # You can get the status code by e.http_code, or see anything about the
66
- # response via e.response. For example, the entire result body (which is
67
- # probably an HTML error page) is e.response.body.
68
- class RequestFailed < ExceptionWithResponse
69
- def message
70
- "HTTP status code #{http_code}"
71
- end
72
-
73
- def to_s
74
- message
75
- end
76
- end
77
- end
78
-
79
- # backwards compatibility
80
- class RestClient::Request
81
- Redirect = RestClient::Redirect
82
- Unauthorized = RestClient::Unauthorized
83
- RequestFailed = RestClient::RequestFailed
84
- end
@@ -1,43 +0,0 @@
1
- module RestClient
2
- module Mixin
3
- module Response
4
- attr_reader :net_http_res
5
-
6
- # HTTP status code, always 200 since RestClient throws exceptions for
7
- # other codes.
8
- def code
9
- @code ||= @net_http_res.code.to_i
10
- end
11
-
12
- # A hash of the headers, beautified with symbols and underscores.
13
- # e.g. "Content-type" will become :content_type.
14
- def headers
15
- @headers ||= self.class.beautify_headers(@net_http_res.to_hash)
16
- end
17
-
18
- # Hash of cookies extracted from response headers
19
- def cookies
20
- @cookies ||= (self.headers[:set_cookie] || "").split('; ').inject({}) do |out, raw_c|
21
- key, val = raw_c.split('=')
22
- unless %w(expires domain path secure).member?(key)
23
- out[key] = val
24
- end
25
- out
26
- end
27
- end
28
-
29
- def self.included(receiver)
30
- receiver.extend(RestClient::Mixin::Response::ClassMethods)
31
- end
32
-
33
- module ClassMethods
34
- def beautify_headers(headers)
35
- headers.inject({}) do |out, (key, value)|
36
- out[key.gsub(/-/, '_').to_sym] = value.first
37
- out
38
- end
39
- end
40
- end
41
- end
42
- end
43
- end
@@ -1,30 +0,0 @@
1
- require File.dirname(__FILE__) + '/mixin/response'
2
-
3
- module RestClient
4
- # The response from RestClient on a raw request looks like a string, but is
5
- # actually one of these. 99% of the time you're making a rest call all you
6
- # care about is the body, but on the occassion you want to fetch the
7
- # headers you can:
8
- #
9
- # RestClient.get('http://example.com').headers[:content_type]
10
- #
11
- # In addition, if you do not use the response as a string, you can access
12
- # a Tempfile object at res.file, which contains the path to the raw
13
- # downloaded request body.
14
- class RawResponse
15
- include RestClient::Mixin::Response
16
-
17
- attr_reader :file
18
-
19
- def initialize(tempfile, net_http_res)
20
- @net_http_res = net_http_res
21
- @file = tempfile
22
- end
23
-
24
- def to_s
25
- @file.open
26
- @file.read
27
- end
28
-
29
- end
30
- end
@@ -1,232 +0,0 @@
1
- require 'tempfile'
2
-
3
- module RestClient
4
- # This class is used internally by RestClient to send the request, but you can also
5
- # access it internally if you'd like to use a method not directly supported by the
6
- # main API. For example:
7
- #
8
- # RestClient::Request.execute(:method => :head, :url => 'http://example.com')
9
- #
10
- class Request
11
- attr_reader :method, :url, :payload, :headers,
12
- :cookies, :user, :password, :timeout, :open_timeout,
13
- :verify_ssl, :ssl_client_cert, :ssl_client_key, :ssl_ca_file,
14
- :raw_response
15
-
16
- def self.execute(args)
17
- new(args).execute
18
- end
19
-
20
- def initialize(args)
21
- @method = args[:method] or raise ArgumentError, "must pass :method"
22
- @url = args[:url] or raise ArgumentError, "must pass :url"
23
- @headers = args[:headers] || {}
24
- @cookies = @headers.delete(:cookies) || args[:cookies] || {}
25
- @payload = process_payload(args[:payload])
26
- @user = args[:user]
27
- @password = args[:password]
28
- @timeout = args[:timeout]
29
- @open_timeout = args[:open_timeout]
30
- @raw_response = args[:raw_response] || false
31
- @verify_ssl = args[:verify_ssl] || false
32
- @ssl_client_cert = args[:ssl_client_cert] || nil
33
- @ssl_client_key = args[:ssl_client_key] || nil
34
- @ssl_ca_file = args[:ssl_ca_file] || nil
35
- @tf = nil # If you are a raw request, this is your tempfile
36
- end
37
-
38
- def execute
39
- execute_inner
40
- rescue Redirect => e
41
- @url = e.url
42
- execute
43
- end
44
-
45
- def execute_inner
46
- uri = parse_url_with_auth(url)
47
- transmit uri, net_http_request_class(method).new(uri.request_uri, make_headers(headers)), payload
48
- end
49
-
50
- def make_headers(user_headers)
51
- unless @cookies.empty?
52
- user_headers[:cookie] = @cookies.map {|key, val| "#{key.to_s}=#{val}" }.join('; ')
53
- end
54
-
55
- default_headers.merge(user_headers).inject({}) do |final, (key, value)|
56
- final[key.to_s.gsub(/_/, '-').capitalize] = value.to_s
57
- final
58
- end
59
- end
60
-
61
- def net_http_class
62
- if RestClient.proxy
63
- proxy_uri = URI.parse(RestClient.proxy)
64
- Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
65
- else
66
- Net::HTTP
67
- end
68
- end
69
-
70
- def net_http_request_class(method)
71
- Net::HTTP.const_get(method.to_s.capitalize)
72
- end
73
-
74
- def parse_url(url)
75
- url = "http://#{url}" unless url.match(/^http/)
76
- URI.parse(url)
77
- end
78
-
79
- def parse_url_with_auth(url)
80
- uri = parse_url(url)
81
- @user = uri.user if uri.user
82
- @password = uri.password if uri.password
83
- uri
84
- end
85
-
86
- def process_payload(p=nil, parent_key=nil)
87
- unless p.is_a?(Hash)
88
- p
89
- else
90
- @headers[:content_type] ||= 'application/x-www-form-urlencoded'
91
- p.keys.map do |k|
92
- key = parent_key ? "#{parent_key}[#{k}]" : k
93
- if p[k].is_a? Hash
94
- process_payload(p[k], key)
95
- else
96
- value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
97
- "#{key}=#{value}"
98
- end
99
- end.join("&")
100
- end
101
- end
102
-
103
- def transmit(uri, req, payload)
104
- setup_credentials(req)
105
-
106
- net = net_http_class.new(uri.host, uri.port)
107
- net.use_ssl = uri.is_a?(URI::HTTPS)
108
- net.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl == false
109
- net.cert = @ssl_client_cert if @ssl_client_cert
110
- net.key = @ssl_client_key if @ssl_client_key
111
- net.ca_file = @ssl_ca_file if @ssl_ca_file
112
- net.read_timeout = @timeout if @timeout
113
- net.open_timeout = @open_timeout if @open_timeout
114
-
115
- display_log request_log
116
-
117
- net.start do |http|
118
- res = http.request(req, payload) { |http_response| fetch_body(http_response) }
119
- result = process_result(res)
120
- display_log response_log(res)
121
-
122
- if result.kind_of?(String) or @method == :head
123
- Response.new(result, res)
124
- elsif @raw_response
125
- RawResponse.new(@tf, res)
126
- else
127
- nil
128
- end
129
- end
130
- rescue EOFError
131
- raise RestClient::ServerBrokeConnection
132
- rescue Timeout::Error
133
- raise RestClient::RequestTimeout
134
- end
135
-
136
- def setup_credentials(req)
137
- req.basic_auth(user, password) if user
138
- end
139
-
140
- def fetch_body(http_response)
141
- if @raw_response
142
- # Taken from Chef, which as in turn...
143
- # Stolen from http://www.ruby-forum.com/topic/166423
144
- # Kudos to _why!
145
- @tf = Tempfile.new("rest-client")
146
- size, total = 0, http_response.header['Content-Length'].to_i
147
- http_response.read_body do |chunk|
148
- @tf.write(chunk)
149
- size += chunk.size
150
- if size == 0
151
- display_log("#{@method} #{@url} done (0 length file)")
152
- elsif total == 0
153
- display_log("#{@method} #{@url} (zero content length)")
154
- else
155
- display_log("#{@method} #{@url} %d%% done (%d of %d)" % [(size * 100) / total, size, total])
156
- end
157
- end
158
- @tf.close
159
- @tf
160
- else
161
- http_response.read_body
162
- end
163
- http_response
164
- end
165
-
166
- def process_result(res)
167
- if res.code =~ /\A2\d{2}\z/
168
- # We don't decode raw requests
169
- unless @raw_response
170
- decode res['content-encoding'], res.body if res.body
171
- end
172
- elsif %w(301 302 303).include? res.code
173
- url = res.header['Location']
174
-
175
- if url !~ /^http/
176
- uri = URI.parse(@url)
177
- uri.path = "/#{url}".squeeze('/')
178
- url = uri.to_s
179
- end
180
-
181
- raise Redirect, url
182
- elsif res.code == "304"
183
- raise NotModified, res
184
- elsif res.code == "401"
185
- raise Unauthorized, res
186
- elsif res.code == "404"
187
- raise ResourceNotFound, res
188
- else
189
- raise RequestFailed, res
190
- end
191
- end
192
-
193
- def decode(content_encoding, body)
194
- if content_encoding == 'gzip' and not body.empty?
195
- Zlib::GzipReader.new(StringIO.new(body)).read
196
- elsif content_encoding == 'deflate'
197
- Zlib::Inflate.new.inflate(body)
198
- else
199
- body
200
- end
201
- end
202
-
203
- def request_log
204
- out = []
205
- out << "RestClient.#{method} #{url.inspect}"
206
- out << (payload.size > 100 ? "(#{payload.size} byte payload)".inspect : payload.inspect) if payload
207
- out << headers.inspect.gsub(/^\{/, '').gsub(/\}$/, '') unless headers.empty?
208
- out.join(', ')
209
- end
210
-
211
- def response_log(res)
212
- size = @raw_response ? File.size(@tf.path) : res.body.size
213
- "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes"
214
- end
215
-
216
- def display_log(msg)
217
- return unless log_to = RestClient.log
218
-
219
- if log_to == 'stdout'
220
- STDOUT.puts msg
221
- elsif log_to == 'stderr'
222
- STDERR.puts msg
223
- else
224
- File.open(log_to, 'a') { |f| f.puts msg }
225
- end
226
- end
227
-
228
- def default_headers
229
- { :accept => 'application/xml', :accept_encoding => 'gzip, deflate' }
230
- end
231
- end
232
- end
@@ -1,146 +0,0 @@
1
- module RestClient
2
- # A class that can be instantiated for access to a RESTful resource,
3
- # including authentication.
4
- #
5
- # Example:
6
- #
7
- # resource = RestClient::Resource.new('http://some/resource')
8
- # jpg = resource.get(:accept => 'image/jpg')
9
- #
10
- # With HTTP basic authentication:
11
- #
12
- # resource = RestClient::Resource.new('http://protected/resource', :user => 'user', :password => 'password')
13
- # resource.delete
14
- #
15
- # With a timeout (seconds):
16
- #
17
- # RestClient::Resource.new('http://slow', :timeout => 10)
18
- #
19
- # With an open timeout (seconds):
20
- #
21
- # RestClient::Resource.new('http://behindfirewall', :open_timeout => 10)
22
- #
23
- # You can also use resources to share common headers. For headers keys,
24
- # symbols are converted to strings. Example:
25
- #
26
- # resource = RestClient::Resource.new('http://some/resource', :headers => { :client_version => 1 })
27
- #
28
- # This header will be transported as X-Client-Version (notice the X prefix,
29
- # capitalization and hyphens)
30
- #
31
- # Use the [] syntax to allocate subresources:
32
- #
33
- # site = RestClient::Resource.new('http://example.com', :user => 'adam', :password => 'mypasswd')
34
- # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
35
- #
36
- class Resource
37
- attr_reader :url, :options
38
-
39
- def initialize(url, options={}, backwards_compatibility=nil)
40
- @url = url
41
- if options.class == Hash
42
- @options = options
43
- else # compatibility with previous versions
44
- @options = { :user => options, :password => backwards_compatibility }
45
- end
46
- end
47
-
48
- def get(additional_headers={})
49
- Request.execute(options.merge(
50
- :method => :get,
51
- :url => url,
52
- :headers => headers.merge(additional_headers)
53
- ))
54
- end
55
-
56
- def post(payload, additional_headers={})
57
- Request.execute(options.merge(
58
- :method => :post,
59
- :url => url,
60
- :payload => payload,
61
- :headers => headers.merge(additional_headers)
62
- ))
63
- end
64
-
65
- def put(payload, additional_headers={})
66
- Request.execute(options.merge(
67
- :method => :put,
68
- :url => url,
69
- :payload => payload,
70
- :headers => headers.merge(additional_headers)
71
- ))
72
- end
73
-
74
- def delete(additional_headers={})
75
- Request.execute(options.merge(
76
- :method => :delete,
77
- :url => url,
78
- :headers => headers.merge(additional_headers)
79
- ))
80
- end
81
-
82
- def to_s
83
- url
84
- end
85
-
86
- def user
87
- options[:user]
88
- end
89
-
90
- def password
91
- options[:password]
92
- end
93
-
94
- def headers
95
- options[:headers] || {}
96
- end
97
-
98
- def timeout
99
- options[:timeout]
100
- end
101
-
102
- def open_timeout
103
- options[:open_timeout]
104
- end
105
-
106
- # Construct a subresource, preserving authentication.
107
- #
108
- # Example:
109
- #
110
- # site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
111
- # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
112
- #
113
- # This is especially useful if you wish to define your site in one place and
114
- # call it in multiple locations:
115
- #
116
- # def orders
117
- # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
118
- # end
119
- #
120
- # orders.get # GET http://example.com/orders
121
- # orders['1'].get # GET http://example.com/orders/1
122
- # orders['1/items'].delete # DELETE http://example.com/orders/1/items
123
- #
124
- # Nest resources as far as you want:
125
- #
126
- # site = RestClient::Resource.new('http://example.com')
127
- # posts = site['posts']
128
- # first_post = posts['1']
129
- # comments = first_post['comments']
130
- # comments.post 'Hello', :content_type => 'text/plain'
131
- #
132
- def [](suburl)
133
- self.class.new(concat_urls(url, suburl), options)
134
- end
135
-
136
- def concat_urls(url, suburl) # :nodoc:
137
- url = url.to_s
138
- suburl = suburl.to_s
139
- if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
140
- url + suburl
141
- else
142
- "#{url}/#{suburl}"
143
- end
144
- end
145
- end
146
- end