cft_smartcloud 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (135) hide show
  1. data/.gitignore +2 -0
  2. data/CHANGELOG +8 -0
  3. data/LICENSE +178 -0
  4. data/README.rdoc +71 -0
  5. data/Rakefile +53 -0
  6. data/VERSION +1 -0
  7. data/bin/smartcloud +37 -0
  8. data/cft_smartcloud.gemspec +176 -0
  9. data/lib/cli_tools/README.txt +50 -0
  10. data/lib/cli_tools/ic-add-keypair.cmd +29 -0
  11. data/lib/cli_tools/ic-add-keypair.sh +15 -0
  12. data/lib/cli_tools/ic-allocate-address.cmd +29 -0
  13. data/lib/cli_tools/ic-allocate-address.sh +14 -0
  14. data/lib/cli_tools/ic-attach-volume.cmd +27 -0
  15. data/lib/cli_tools/ic-attach-volume.sh +27 -0
  16. data/lib/cli_tools/ic-clone-image.cmd +27 -0
  17. data/lib/cli_tools/ic-clone-image.sh +14 -0
  18. data/lib/cli_tools/ic-clone-volume.cmd +27 -0
  19. data/lib/cli_tools/ic-clone-volume.sh +60 -0
  20. data/lib/cli_tools/ic-cmd.cmd +41 -0
  21. data/lib/cli_tools/ic-cmd.sh +38 -0
  22. data/lib/cli_tools/ic-create-instance.cmd +27 -0
  23. data/lib/cli_tools/ic-create-instance.sh +14 -0
  24. data/lib/cli_tools/ic-create-password.cmd +27 -0
  25. data/lib/cli_tools/ic-create-password.sh +14 -0
  26. data/lib/cli_tools/ic-create-volume.cmd +27 -0
  27. data/lib/cli_tools/ic-create-volume.sh +14 -0
  28. data/lib/cli_tools/ic-delete-image.cmd +27 -0
  29. data/lib/cli_tools/ic-delete-image.sh +14 -0
  30. data/lib/cli_tools/ic-delete-instance.cmd +27 -0
  31. data/lib/cli_tools/ic-delete-instance.sh +14 -0
  32. data/lib/cli_tools/ic-delete-volume.cmd +27 -0
  33. data/lib/cli_tools/ic-delete-volume.sh +14 -0
  34. data/lib/cli_tools/ic-describe-address-offerings.cmd +27 -0
  35. data/lib/cli_tools/ic-describe-address-offerings.sh +14 -0
  36. data/lib/cli_tools/ic-describe-addresses.cmd +27 -0
  37. data/lib/cli_tools/ic-describe-addresses.sh +14 -0
  38. data/lib/cli_tools/ic-describe-image-agreement.cmd +27 -0
  39. data/lib/cli_tools/ic-describe-image-agreement.sh +14 -0
  40. data/lib/cli_tools/ic-describe-image.cmd +27 -0
  41. data/lib/cli_tools/ic-describe-image.sh +14 -0
  42. data/lib/cli_tools/ic-describe-images.cmd +27 -0
  43. data/lib/cli_tools/ic-describe-images.sh +14 -0
  44. data/lib/cli_tools/ic-describe-instance.cmd +27 -0
  45. data/lib/cli_tools/ic-describe-instance.sh +14 -0
  46. data/lib/cli_tools/ic-describe-instances.cmd +27 -0
  47. data/lib/cli_tools/ic-describe-instances.sh +14 -0
  48. data/lib/cli_tools/ic-describe-keypair.cmd +27 -0
  49. data/lib/cli_tools/ic-describe-keypair.sh +14 -0
  50. data/lib/cli_tools/ic-describe-keypairs.cmd +27 -0
  51. data/lib/cli_tools/ic-describe-keypairs.sh +14 -0
  52. data/lib/cli_tools/ic-describe-location.cmd +14 -0
  53. data/lib/cli_tools/ic-describe-location.sh +14 -0
  54. data/lib/cli_tools/ic-describe-locations.cmd +14 -0
  55. data/lib/cli_tools/ic-describe-locations.sh +14 -0
  56. data/lib/cli_tools/ic-describe-request.cmd +27 -0
  57. data/lib/cli_tools/ic-describe-request.sh +14 -0
  58. data/lib/cli_tools/ic-describe-vlans.cmd +27 -0
  59. data/lib/cli_tools/ic-describe-vlans.sh +14 -0
  60. data/lib/cli_tools/ic-describe-volume-offerings.cmd +27 -0
  61. data/lib/cli_tools/ic-describe-volume-offerings.sh +14 -0
  62. data/lib/cli_tools/ic-describe-volume.cmd +27 -0
  63. data/lib/cli_tools/ic-describe-volume.sh +14 -0
  64. data/lib/cli_tools/ic-describe-volumes.cmd +27 -0
  65. data/lib/cli_tools/ic-describe-volumes.sh +14 -0
  66. data/lib/cli_tools/ic-detach-volume.cmd +27 -0
  67. data/lib/cli_tools/ic-detach-volume.sh +27 -0
  68. data/lib/cli_tools/ic-extend-reservation.cmd +27 -0
  69. data/lib/cli_tools/ic-extend-reservation.sh +14 -0
  70. data/lib/cli_tools/ic-generate-keypair.cmd +27 -0
  71. data/lib/cli_tools/ic-generate-keypair.sh +14 -0
  72. data/lib/cli_tools/ic-release-address.cmd +27 -0
  73. data/lib/cli_tools/ic-release-address.sh +14 -0
  74. data/lib/cli_tools/ic-remove-keypair.cmd +27 -0
  75. data/lib/cli_tools/ic-remove-keypair.sh +14 -0
  76. data/lib/cli_tools/ic-restart-instance.cmd +27 -0
  77. data/lib/cli_tools/ic-restart-instance.sh +14 -0
  78. data/lib/cli_tools/ic-save-instance.cmd +27 -0
  79. data/lib/cli_tools/ic-save-instance.sh +14 -0
  80. data/lib/cli_tools/ic-set-default-key.cmd +27 -0
  81. data/lib/cli_tools/ic-set-default-key.sh +14 -0
  82. data/lib/cli_tools/ic-update-instance.cmd +27 -0
  83. data/lib/cli_tools/ic-update-instance.sh +14 -0
  84. data/lib/cli_tools/ic-update-keypair.cmd +27 -0
  85. data/lib/cli_tools/ic-update-keypair.sh +14 -0
  86. data/lib/cli_tools/lib/DeveloperCloud_API_Client_JAR.jar +0 -0
  87. data/lib/cli_tools/lib/DeveloperCloud_CMD_Tool.jar +0 -0
  88. data/lib/cli_tools/lib/commons-beanutils-1.6.1.jar +0 -0
  89. data/lib/cli_tools/lib/commons-cli-1.2.jar +0 -0
  90. data/lib/cli_tools/lib/commons-codec-1.3.jar +0 -0
  91. data/lib/cli_tools/lib/commons-collections-3.2.1.jar +0 -0
  92. data/lib/cli_tools/lib/commons-digester-1.8.jar +0 -0
  93. data/lib/cli_tools/lib/commons-httpclient-3.1.jar +0 -0
  94. data/lib/cli_tools/lib/commons-lang-2.3.jar +0 -0
  95. data/lib/cli_tools/lib/commons-logging-1.1.1.jar +0 -0
  96. data/lib/cli_tools/logging.properties +7 -0
  97. data/lib/cli_tools/manifest.rmd +26 -0
  98. data/lib/config/config.yml +50 -0
  99. data/lib/hash_fix.rb +37 -0
  100. data/lib/mime-types-1.16/History.txt +107 -0
  101. data/lib/mime-types-1.16/Install.txt +17 -0
  102. data/lib/mime-types-1.16/Licence.txt +15 -0
  103. data/lib/mime-types-1.16/Manifest.txt +12 -0
  104. data/lib/mime-types-1.16/README.txt +28 -0
  105. data/lib/mime-types-1.16/Rakefile +316 -0
  106. data/lib/mime-types-1.16/lib/mime/types.rb +751 -0
  107. data/lib/mime-types-1.16/lib/mime/types.rb.data +1324 -0
  108. data/lib/mime-types-1.16/mime-types.gemspec +43 -0
  109. data/lib/mime-types-1.16/setup.rb +1585 -0
  110. data/lib/mime-types-1.16/test/test_mime_type.rb +356 -0
  111. data/lib/mime-types-1.16/test/test_mime_types.rb +122 -0
  112. data/lib/mock_smartcloud.rb +53 -0
  113. data/lib/rest-client-1.6.3/README.rdoc +276 -0
  114. data/lib/rest-client-1.6.3/Rakefile +66 -0
  115. data/lib/rest-client-1.6.3/VERSION +1 -0
  116. data/lib/rest-client-1.6.3/bin/restclient +92 -0
  117. data/lib/rest-client-1.6.3/history.md +112 -0
  118. data/lib/rest-client-1.6.3/lib/rest-client.rb +2 -0
  119. data/lib/rest-client-1.6.3/lib/rest_client.rb +2 -0
  120. data/lib/rest-client-1.6.3/lib/restclient/abstract_response.rb +106 -0
  121. data/lib/rest-client-1.6.3/lib/restclient/exceptions.rb +193 -0
  122. data/lib/rest-client-1.6.3/lib/restclient/net_http_ext.rb +21 -0
  123. data/lib/rest-client-1.6.3/lib/restclient/payload.rb +220 -0
  124. data/lib/rest-client-1.6.3/lib/restclient/raw_response.rb +34 -0
  125. data/lib/rest-client-1.6.3/lib/restclient/request.rb +314 -0
  126. data/lib/rest-client-1.6.3/lib/restclient/resource.rb +169 -0
  127. data/lib/rest-client-1.6.3/lib/restclient/response.rb +24 -0
  128. data/lib/rest-client-1.6.3/lib/restclient.rb +174 -0
  129. data/lib/restclient_fix.rb +41 -0
  130. data/lib/smartcloud.rb +616 -0
  131. data/lib/smartcloud_logger.rb +20 -0
  132. data/lib/xml-simple-1.0.12/lib/xmlsimple.rb +1028 -0
  133. data/script/console +3 -0
  134. data/test/helper.rb +22 -0
  135. metadata +196 -0
@@ -0,0 +1,314 @@
1
+ require 'tempfile'
2
+ require 'mime/types'
3
+ require 'cgi'
4
+
5
+ module RestClient
6
+ # This class is used internally by RestClient to send the request, but you can also
7
+ # call it directly if you'd like to use a method not supported by the
8
+ # main API. For example:
9
+ #
10
+ # RestClient::Request.execute(:method => :head, :url => 'http://example.com')
11
+ #
12
+ # Mandatory parameters:
13
+ # * :method
14
+ # * :url
15
+ # Optional parameters (have a look at ssl and/or uri for some explanations):
16
+ # * :headers a hash containing the request headers
17
+ # * :cookies will replace possible cookies in the :headers
18
+ # * :user and :password for basic auth, will be replaced by a user/password available in the :url
19
+ # * :block_response call the provided block with the HTTPResponse as parameter
20
+ # * :raw_response return a low-level RawResponse instead of a Response
21
+ # * :max_redirects maximum number of redirections (default to 10)
22
+ # * :verify_ssl enable ssl verification, possible values are constants from OpenSSL::SSL
23
+ # * :timeout and :open_timeout
24
+ # * :ssl_client_cert, :ssl_client_key, :ssl_ca_file
25
+ class Request
26
+
27
+ attr_reader :method, :url, :headers, :cookies,
28
+ :payload, :user, :password, :timeout, :max_redirects,
29
+ :open_timeout, :raw_response, :verify_ssl, :ssl_client_cert,
30
+ :ssl_client_key, :ssl_ca_file, :processed_headers, :args
31
+
32
+ def self.execute(args, & block)
33
+ new(args).execute(& block)
34
+ end
35
+
36
+ def initialize args
37
+ @method = args[:method] or raise ArgumentError, "must pass :method"
38
+ @headers = args[:headers] || {}
39
+ if args[:url]
40
+ @url = process_get_params(args[:url], headers)
41
+ else
42
+ raise ArgumentError, "must pass :url"
43
+ end
44
+ @cookies = @headers.delete(:cookies) || args[:cookies] || {}
45
+ @payload = Payload.generate(args[:payload])
46
+ @user = args[:user]
47
+ @password = args[:password]
48
+ @timeout = args[:timeout]
49
+ @open_timeout = args[:open_timeout]
50
+ @block_response = args[:block_response]
51
+ @raw_response = args[:raw_response] || false
52
+ @verify_ssl = args[:verify_ssl] || false
53
+ @ssl_client_cert = args[:ssl_client_cert] || nil
54
+ @ssl_client_key = args[:ssl_client_key] || nil
55
+ @ssl_ca_file = args[:ssl_ca_file] || nil
56
+ @tf = nil # If you are a raw request, this is your tempfile
57
+ @max_redirects = args[:max_redirects] || 10
58
+ @processed_headers = make_headers headers
59
+ @args = args
60
+ end
61
+
62
+ def execute & block
63
+ uri = parse_url_with_auth(url)
64
+ transmit uri, net_http_request_class(method).new(uri.request_uri, processed_headers), payload, & block
65
+ end
66
+
67
+ # Extract the query parameters for get request and append them to the url
68
+ def process_get_params url, headers
69
+ if [:get, :head, :delete].include? method
70
+ get_params = {}
71
+ headers.delete_if do |key, value|
72
+ if 'params' == key.to_s.downcase && value.is_a?(Hash)
73
+ get_params.merge! value
74
+ true
75
+ else
76
+ false
77
+ end
78
+ end
79
+ unless get_params.empty?
80
+ query_string = get_params.collect { |k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}" }.join('&')
81
+ url + "?#{query_string}"
82
+ else
83
+ url
84
+ end
85
+ else
86
+ url
87
+ end
88
+ end
89
+
90
+ def make_headers user_headers
91
+ unless @cookies.empty?
92
+ user_headers[:cookie] = @cookies.map { |(key, val)| "#{key.to_s}=#{CGI::unescape(val)}" }.sort.join(';')
93
+ end
94
+ headers = stringify_headers(default_headers).merge(stringify_headers(user_headers))
95
+ headers.merge!(@payload.headers) if @payload
96
+ headers
97
+ end
98
+
99
+ def net_http_class
100
+ if RestClient.proxy
101
+ proxy_uri = URI.parse(RestClient.proxy)
102
+ Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
103
+ else
104
+ Net::HTTP
105
+ end
106
+ end
107
+
108
+ def net_http_request_class(method)
109
+ Net::HTTP.const_get(method.to_s.capitalize)
110
+ end
111
+
112
+ def parse_url(url)
113
+ url = "http://#{url}" unless url.match(/^http/)
114
+ URI.parse(url)
115
+ end
116
+
117
+ def parse_url_with_auth(url)
118
+ uri = parse_url(url)
119
+ @user = CGI.unescape(uri.user) if uri.user
120
+ @password = CGI.unescape(uri.password) if uri.password
121
+ uri
122
+ end
123
+
124
+ def process_payload(p=nil, parent_key=nil)
125
+ unless p.is_a?(Hash)
126
+ p
127
+ else
128
+ @headers[:content_type] ||= 'application/x-www-form-urlencoded'
129
+ p.keys.map do |k|
130
+ key = parent_key ? "#{parent_key}[#{k}]" : k
131
+ if p[k].is_a? Hash
132
+ process_payload(p[k], key)
133
+ else
134
+ value = URI.escape(p[k].to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
135
+ "#{key}=#{value}"
136
+ end
137
+ end.join("&")
138
+ end
139
+ end
140
+
141
+ def transmit uri, req, payload, & block
142
+ setup_credentials req
143
+
144
+ net = net_http_class.new(uri.host, uri.port)
145
+ net.use_ssl = uri.is_a?(URI::HTTPS)
146
+ if (@verify_ssl == false) || (@verify_ssl == OpenSSL::SSL::VERIFY_NONE)
147
+ net.verify_mode = OpenSSL::SSL::VERIFY_NONE
148
+ elsif @verify_ssl.is_a? Integer
149
+ net.verify_mode = @verify_ssl
150
+ net.verify_callback = lambda do |preverify_ok, ssl_context|
151
+ if (!preverify_ok) || ssl_context.error != 0
152
+ err_msg = "SSL Verification failed -- Preverify: #{preverify_ok}, Error: #{ssl_context.error_string} (#{ssl_context.error})"
153
+ raise SSLCertificateNotVerified.new(err_msg)
154
+ end
155
+ true
156
+ end
157
+ end
158
+ net.cert = @ssl_client_cert if @ssl_client_cert
159
+ net.key = @ssl_client_key if @ssl_client_key
160
+ net.ca_file = @ssl_ca_file if @ssl_ca_file
161
+ net.read_timeout = @timeout if @timeout
162
+ net.open_timeout = @open_timeout if @open_timeout
163
+
164
+ RestClient.before_execution_procs.each do |before_proc|
165
+ before_proc.call(req, args)
166
+ end
167
+
168
+ log_request
169
+
170
+ net.start do |http|
171
+ if @block_response
172
+ http.request(req, payload ? payload.to_s : nil, & @block_response)
173
+ else
174
+ res = http.request(req, payload ? payload.to_s : nil) { |http_response| fetch_body(http_response) }
175
+ log_response res
176
+ process_result res, & block
177
+ end
178
+ end
179
+ rescue EOFError
180
+ raise RestClient::ServerBrokeConnection
181
+ rescue Timeout::Error
182
+ raise RestClient::RequestTimeout
183
+ end
184
+
185
+ def setup_credentials(req)
186
+ req.basic_auth(user, password) if user
187
+ end
188
+
189
+ def fetch_body(http_response)
190
+ if @raw_response
191
+ # Taken from Chef, which as in turn...
192
+ # Stolen from http://www.ruby-forum.com/topic/166423
193
+ # Kudos to _why!
194
+ @tf = Tempfile.new("rest-client")
195
+ size, total = 0, http_response.header['Content-Length'].to_i
196
+ http_response.read_body do |chunk|
197
+ @tf.write chunk
198
+ size += chunk.size
199
+ if RestClient.log
200
+ if size == 0
201
+ RestClient.log << "#{@method} #{@url} done (0 length file\n)"
202
+ elsif total == 0
203
+ RestClient.log << "#{@method} #{@url} (zero content length)\n"
204
+ else
205
+ RestClient.log << "#{@method} #{@url} %d%% done (%d of %d)\n" % [(size * 100) / total, size, total]
206
+ end
207
+ end
208
+ end
209
+ @tf.close
210
+ @tf
211
+ else
212
+ http_response.read_body
213
+ end
214
+ http_response
215
+ end
216
+
217
+ def process_result res, & block
218
+ if @raw_response
219
+ # We don't decode raw requests
220
+ response = RawResponse.new(@tf, res, args)
221
+ else
222
+ response = Response.create(Request.decode(res['content-encoding'], res.body), res, args)
223
+ end
224
+
225
+ if block_given?
226
+ block.call(response, self, res, & block)
227
+ else
228
+ response.return!(self, res, & block)
229
+ end
230
+
231
+ end
232
+
233
+ def self.decode content_encoding, body
234
+ if (!body) || body.empty?
235
+ body
236
+ elsif content_encoding == 'gzip'
237
+ Zlib::GzipReader.new(StringIO.new(body)).read
238
+ elsif content_encoding == 'deflate'
239
+ begin
240
+ Zlib::Inflate.new.inflate body
241
+ rescue Zlib::DataError
242
+ # No luck with Zlib decompression. Let's try with raw deflate,
243
+ # like some broken web servers do.
244
+ Zlib::Inflate.new(-Zlib::MAX_WBITS).inflate body
245
+ end
246
+ else
247
+ body
248
+ end
249
+ end
250
+
251
+ def log_request
252
+ if RestClient.log
253
+ out = []
254
+ out << "RestClient.#{method} #{url.inspect}"
255
+ out << payload.short_inspect if payload
256
+ out << processed_headers.to_a.sort.map { |(k, v)| [k.inspect, v.inspect].join("=>") }.join(", ")
257
+ RestClient.log << out.join(', ') + "\n"
258
+ end
259
+ end
260
+
261
+ def log_response res
262
+ if RestClient.log
263
+ size = @raw_response ? File.size(@tf.path) : (res.body.nil? ? 0 : res.body.size)
264
+ RestClient.log << "# => #{res.code} #{res.class.to_s.gsub(/^Net::HTTP/, '')} | #{(res['Content-type'] || '').gsub(/;.*$/, '')} #{size} bytes\n"
265
+ end
266
+ end
267
+
268
+ # Return a hash of headers whose keys are capitalized strings
269
+ def stringify_headers headers
270
+ headers.inject({}) do |result, (key, value)|
271
+ if key.is_a? Symbol
272
+ key = key.to_s.split(/_/).map { |w| w.capitalize }.join('-')
273
+ end
274
+ if 'CONTENT-TYPE' == key.upcase
275
+ target_value = value.to_s
276
+ result[key] = MIME::Types.type_for_extension target_value
277
+ elsif 'ACCEPT' == key.upcase
278
+ # Accept can be composed of several comma-separated values
279
+ if value.is_a? Array
280
+ target_values = value
281
+ else
282
+ target_values = value.to_s.split ','
283
+ end
284
+ result[key] = target_values.map { |ext| MIME::Types.type_for_extension(ext.to_s.strip) }.join(', ')
285
+ else
286
+ result[key] = value.to_s
287
+ end
288
+ result
289
+ end
290
+ end
291
+
292
+ def default_headers
293
+ {:accept => '*/*; q=0.5, application/xml', :accept_encoding => 'gzip, deflate'}
294
+ end
295
+
296
+ end
297
+ end
298
+
299
+ module MIME
300
+ class Types
301
+
302
+ # Return the first found content-type for a value considered as an extension or the value itself
303
+ def type_for_extension ext
304
+ candidates = @extension_index[ext]
305
+ candidates.empty? ? ext : candidates[0].content_type
306
+ end
307
+
308
+ class << self
309
+ def type_for_extension ext
310
+ @__types__.type_for_extension ext
311
+ end
312
+ end
313
+ end
314
+ end
@@ -0,0 +1,169 @@
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, :block
38
+
39
+ def initialize(url, options={}, backwards_compatibility=nil, &block)
40
+ @url = url
41
+ @block = block
42
+ if options.class == Hash
43
+ @options = options
44
+ else # compatibility with previous versions
45
+ @options = { :user => options, :password => backwards_compatibility }
46
+ end
47
+ end
48
+
49
+ def get(additional_headers={}, &block)
50
+ headers = (options[:headers] || {}).merge(additional_headers)
51
+ Request.execute(options.merge(
52
+ :method => :get,
53
+ :url => url,
54
+ :headers => headers), &(block || @block))
55
+ end
56
+
57
+ def head(additional_headers={}, &block)
58
+ headers = (options[:headers] || {}).merge(additional_headers)
59
+ Request.execute(options.merge(
60
+ :method => :head,
61
+ :url => url,
62
+ :headers => headers), &(block || @block))
63
+ end
64
+
65
+ def post(payload, additional_headers={}, &block)
66
+ headers = (options[:headers] || {}).merge(additional_headers)
67
+ Request.execute(options.merge(
68
+ :method => :post,
69
+ :url => url,
70
+ :payload => payload,
71
+ :headers => headers), &(block || @block))
72
+ end
73
+
74
+ def put(payload, additional_headers={}, &block)
75
+ headers = (options[:headers] || {}).merge(additional_headers)
76
+ Request.execute(options.merge(
77
+ :method => :put,
78
+ :url => url,
79
+ :payload => payload,
80
+ :headers => headers), &(block || @block))
81
+ end
82
+
83
+ def patch(payload, additional_headers={}, &block)
84
+ headers = (options[:headers] || {}).merge(additional_headers)
85
+ Request.execute(options.merge(
86
+ :method => :patch,
87
+ :url => url,
88
+ :payload => payload,
89
+ :headers => headers), &(block || @block))
90
+ end
91
+
92
+ def delete(additional_headers={}, &block)
93
+ headers = (options[:headers] || {}).merge(additional_headers)
94
+ Request.execute(options.merge(
95
+ :method => :delete,
96
+ :url => url,
97
+ :headers => headers), &(block || @block))
98
+ end
99
+
100
+ def to_s
101
+ url
102
+ end
103
+
104
+ def user
105
+ options[:user]
106
+ end
107
+
108
+ def password
109
+ options[:password]
110
+ end
111
+
112
+ def headers
113
+ options[:headers] || {}
114
+ end
115
+
116
+ def timeout
117
+ options[:timeout]
118
+ end
119
+
120
+ def open_timeout
121
+ options[:open_timeout]
122
+ end
123
+
124
+ # Construct a subresource, preserving authentication.
125
+ #
126
+ # Example:
127
+ #
128
+ # site = RestClient::Resource.new('http://example.com', 'adam', 'mypasswd')
129
+ # site['posts/1/comments'].post 'Good article.', :content_type => 'text/plain'
130
+ #
131
+ # This is especially useful if you wish to define your site in one place and
132
+ # call it in multiple locations:
133
+ #
134
+ # def orders
135
+ # RestClient::Resource.new('http://example.com/orders', 'admin', 'mypasswd')
136
+ # end
137
+ #
138
+ # orders.get # GET http://example.com/orders
139
+ # orders['1'].get # GET http://example.com/orders/1
140
+ # orders['1/items'].delete # DELETE http://example.com/orders/1/items
141
+ #
142
+ # Nest resources as far as you want:
143
+ #
144
+ # site = RestClient::Resource.new('http://example.com')
145
+ # posts = site['posts']
146
+ # first_post = posts['1']
147
+ # comments = first_post['comments']
148
+ # comments.post 'Hello', :content_type => 'text/plain'
149
+ #
150
+ def [](suburl, &new_block)
151
+ case
152
+ when block_given? then self.class.new(concat_urls(url, suburl), options, &new_block)
153
+ when block then self.class.new(concat_urls(url, suburl), options, &block)
154
+ else
155
+ self.class.new(concat_urls(url, suburl), options)
156
+ end
157
+ end
158
+
159
+ def concat_urls(url, suburl) # :nodoc:
160
+ url = url.to_s
161
+ suburl = suburl.to_s
162
+ if url.slice(-1, 1) == '/' or suburl.slice(0, 1) == '/'
163
+ url + suburl
164
+ else
165
+ "#{url}/#{suburl}"
166
+ end
167
+ end
168
+ end
169
+ end
@@ -0,0 +1,24 @@
1
+ module RestClient
2
+
3
+ # A Response from RestClient, you can access the response body, the code or the headers.
4
+ #
5
+ module Response
6
+
7
+ include AbstractResponse
8
+
9
+ attr_accessor :args, :body, :net_http_res
10
+
11
+ def body
12
+ self
13
+ end
14
+
15
+ def Response.create body, net_http_res, args
16
+ result = body || ''
17
+ result.extend Response
18
+ result.net_http_res = net_http_res
19
+ result.args = args
20
+ result
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,174 @@
1
+ require 'uri'
2
+ require 'zlib'
3
+ require 'stringio'
4
+
5
+ begin
6
+ require 'net/https'
7
+ rescue LoadError => e
8
+ raise e unless RUBY_PLATFORM =~ /linux/
9
+ raise LoadError, "no such file to load -- net/https. Try running apt-get install libopenssl-ruby"
10
+ end
11
+
12
+ require File.dirname(__FILE__) + '/restclient/exceptions'
13
+ require File.dirname(__FILE__) + '/restclient/request'
14
+ require File.dirname(__FILE__) + '/restclient/abstract_response'
15
+ require File.dirname(__FILE__) + '/restclient/response'
16
+ require File.dirname(__FILE__) + '/restclient/raw_response'
17
+ require File.dirname(__FILE__) + '/restclient/resource'
18
+ require File.dirname(__FILE__) + '/restclient/payload'
19
+ require File.dirname(__FILE__) + '/restclient/net_http_ext'
20
+
21
+ # This module's static methods are the entry point for using the REST client.
22
+ #
23
+ # # GET
24
+ # xml = RestClient.get 'http://example.com/resource'
25
+ # jpg = RestClient.get 'http://example.com/resource', :accept => 'image/jpg'
26
+ #
27
+ # # authentication and SSL
28
+ # RestClient.get 'https://user:password@example.com/private/resource'
29
+ #
30
+ # # POST or PUT with a hash sends parameters as a urlencoded form body
31
+ # RestClient.post 'http://example.com/resource', :param1 => 'one'
32
+ #
33
+ # # nest hash parameters
34
+ # RestClient.post 'http://example.com/resource', :nested => { :param1 => 'one' }
35
+ #
36
+ # # POST and PUT with raw payloads
37
+ # RestClient.post 'http://example.com/resource', 'the post body', :content_type => 'text/plain'
38
+ # RestClient.post 'http://example.com/resource.xml', xml_doc
39
+ # RestClient.put 'http://example.com/resource.pdf', File.read('my.pdf'), :content_type => 'application/pdf'
40
+ #
41
+ # # DELETE
42
+ # RestClient.delete 'http://example.com/resource'
43
+ #
44
+ # # retreive the response http code and headers
45
+ # res = RestClient.get 'http://example.com/some.jpg'
46
+ # res.code # => 200
47
+ # res.headers[:content_type] # => 'image/jpg'
48
+ #
49
+ # # HEAD
50
+ # RestClient.head('http://example.com').headers
51
+ #
52
+ # To use with a proxy, just set RestClient.proxy to the proper http proxy:
53
+ #
54
+ # RestClient.proxy = "http://proxy.example.com/"
55
+ #
56
+ # Or inherit the proxy from the environment:
57
+ #
58
+ # RestClient.proxy = ENV['http_proxy']
59
+ #
60
+ # For live tests of RestClient, try using http://rest-test.heroku.com, which echoes back information about the rest call:
61
+ #
62
+ # >> RestClient.put 'http://rest-test.heroku.com/resource', :foo => 'baz'
63
+ # => "PUT http://rest-test.heroku.com/resource with a 7 byte payload, content type application/x-www-form-urlencoded {\"foo\"=>\"baz\"}"
64
+ #
65
+ module RestClient
66
+
67
+ def self.get(url, headers={}, &block)
68
+ Request.execute(:method => :get, :url => url, :headers => headers, &block)
69
+ end
70
+
71
+ def self.post(url, payload, headers={}, &block)
72
+ Request.execute(:method => :post, :url => url, :payload => payload, :headers => headers, &block)
73
+ end
74
+
75
+ def self.patch(url, payload, headers={}, &block)
76
+ Request.execute(:method => :patch, :url => url, :payload => payload, :headers => headers, &block)
77
+ end
78
+
79
+ def self.put(url, payload, headers={}, &block)
80
+ Request.execute(:method => :put, :url => url, :payload => payload, :headers => headers, &block)
81
+ end
82
+
83
+ def self.delete(url, headers={}, &block)
84
+ Request.execute(:method => :delete, :url => url, :headers => headers, &block)
85
+ end
86
+
87
+ def self.head(url, headers={}, &block)
88
+ Request.execute(:method => :head, :url => url, :headers => headers, &block)
89
+ end
90
+
91
+ def self.options(url, headers={}, &block)
92
+ Request.execute(:method => :options, :url => url, :headers => headers, &block)
93
+ end
94
+
95
+ class << self
96
+ attr_accessor :proxy
97
+ end
98
+
99
+ # Setup the log for RestClient calls.
100
+ # Value should be a logger but can can be stdout, stderr, or a filename.
101
+ # You can also configure logging by the environment variable RESTCLIENT_LOG.
102
+ def self.log= log
103
+ @@log = create_log log
104
+ end
105
+
106
+ def self.version
107
+ version_path = File.dirname(__FILE__) + "/../VERSION"
108
+ return File.read(version_path).chomp if File.file?(version_path)
109
+ "0.0.0"
110
+ end
111
+
112
+ # Create a log that respond to << like a logger
113
+ # param can be 'stdout', 'stderr', a string (then we will log to that file) or a logger (then we return it)
114
+ def self.create_log param
115
+ if param
116
+ if param.is_a? String
117
+ if param == 'stdout'
118
+ stdout_logger = Class.new do
119
+ def << obj
120
+ STDOUT.puts obj
121
+ end
122
+ end
123
+ stdout_logger.new
124
+ elsif param == 'stderr'
125
+ stderr_logger = Class.new do
126
+ def << obj
127
+ STDERR.puts obj
128
+ end
129
+ end
130
+ stderr_logger.new
131
+ else
132
+ file_logger = Class.new do
133
+ attr_writer :target_file
134
+
135
+ def << obj
136
+ File.open(@target_file, 'a') { |f| f.puts obj }
137
+ end
138
+ end
139
+ logger = file_logger.new
140
+ logger.target_file = param
141
+ logger
142
+ end
143
+ else
144
+ param
145
+ end
146
+ end
147
+ end
148
+
149
+ @@env_log = create_log ENV['RESTCLIENT_LOG']
150
+
151
+ @@log = nil
152
+
153
+ def self.log # :nodoc:
154
+ @@env_log || @@log
155
+ end
156
+
157
+ @@before_execution_procs = []
158
+
159
+ # Add a Proc to be called before each request in executed.
160
+ # The proc parameters will be the http request and the request params.
161
+ def self.add_before_execution_proc &proc
162
+ @@before_execution_procs << proc
163
+ end
164
+
165
+ # Reset the procs to be called before each request is executed.
166
+ def self.reset_before_execution_procs
167
+ @@before_execution_procs = []
168
+ end
169
+
170
+ def self.before_execution_procs # :nodoc:
171
+ @@before_execution_procs
172
+ end
173
+
174
+ end