cft_smartcloud 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/CHANGELOG +8 -0
- data/LICENSE +178 -0
- data/README.rdoc +71 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/bin/smartcloud +37 -0
- data/cft_smartcloud.gemspec +176 -0
- data/lib/cli_tools/README.txt +50 -0
- data/lib/cli_tools/ic-add-keypair.cmd +29 -0
- data/lib/cli_tools/ic-add-keypair.sh +15 -0
- data/lib/cli_tools/ic-allocate-address.cmd +29 -0
- data/lib/cli_tools/ic-allocate-address.sh +14 -0
- data/lib/cli_tools/ic-attach-volume.cmd +27 -0
- data/lib/cli_tools/ic-attach-volume.sh +27 -0
- data/lib/cli_tools/ic-clone-image.cmd +27 -0
- data/lib/cli_tools/ic-clone-image.sh +14 -0
- data/lib/cli_tools/ic-clone-volume.cmd +27 -0
- data/lib/cli_tools/ic-clone-volume.sh +60 -0
- data/lib/cli_tools/ic-cmd.cmd +41 -0
- data/lib/cli_tools/ic-cmd.sh +38 -0
- data/lib/cli_tools/ic-create-instance.cmd +27 -0
- data/lib/cli_tools/ic-create-instance.sh +14 -0
- data/lib/cli_tools/ic-create-password.cmd +27 -0
- data/lib/cli_tools/ic-create-password.sh +14 -0
- data/lib/cli_tools/ic-create-volume.cmd +27 -0
- data/lib/cli_tools/ic-create-volume.sh +14 -0
- data/lib/cli_tools/ic-delete-image.cmd +27 -0
- data/lib/cli_tools/ic-delete-image.sh +14 -0
- data/lib/cli_tools/ic-delete-instance.cmd +27 -0
- data/lib/cli_tools/ic-delete-instance.sh +14 -0
- data/lib/cli_tools/ic-delete-volume.cmd +27 -0
- data/lib/cli_tools/ic-delete-volume.sh +14 -0
- data/lib/cli_tools/ic-describe-address-offerings.cmd +27 -0
- data/lib/cli_tools/ic-describe-address-offerings.sh +14 -0
- data/lib/cli_tools/ic-describe-addresses.cmd +27 -0
- data/lib/cli_tools/ic-describe-addresses.sh +14 -0
- data/lib/cli_tools/ic-describe-image-agreement.cmd +27 -0
- data/lib/cli_tools/ic-describe-image-agreement.sh +14 -0
- data/lib/cli_tools/ic-describe-image.cmd +27 -0
- data/lib/cli_tools/ic-describe-image.sh +14 -0
- data/lib/cli_tools/ic-describe-images.cmd +27 -0
- data/lib/cli_tools/ic-describe-images.sh +14 -0
- data/lib/cli_tools/ic-describe-instance.cmd +27 -0
- data/lib/cli_tools/ic-describe-instance.sh +14 -0
- data/lib/cli_tools/ic-describe-instances.cmd +27 -0
- data/lib/cli_tools/ic-describe-instances.sh +14 -0
- data/lib/cli_tools/ic-describe-keypair.cmd +27 -0
- data/lib/cli_tools/ic-describe-keypair.sh +14 -0
- data/lib/cli_tools/ic-describe-keypairs.cmd +27 -0
- data/lib/cli_tools/ic-describe-keypairs.sh +14 -0
- data/lib/cli_tools/ic-describe-location.cmd +14 -0
- data/lib/cli_tools/ic-describe-location.sh +14 -0
- data/lib/cli_tools/ic-describe-locations.cmd +14 -0
- data/lib/cli_tools/ic-describe-locations.sh +14 -0
- data/lib/cli_tools/ic-describe-request.cmd +27 -0
- data/lib/cli_tools/ic-describe-request.sh +14 -0
- data/lib/cli_tools/ic-describe-vlans.cmd +27 -0
- data/lib/cli_tools/ic-describe-vlans.sh +14 -0
- data/lib/cli_tools/ic-describe-volume-offerings.cmd +27 -0
- data/lib/cli_tools/ic-describe-volume-offerings.sh +14 -0
- data/lib/cli_tools/ic-describe-volume.cmd +27 -0
- data/lib/cli_tools/ic-describe-volume.sh +14 -0
- data/lib/cli_tools/ic-describe-volumes.cmd +27 -0
- data/lib/cli_tools/ic-describe-volumes.sh +14 -0
- data/lib/cli_tools/ic-detach-volume.cmd +27 -0
- data/lib/cli_tools/ic-detach-volume.sh +27 -0
- data/lib/cli_tools/ic-extend-reservation.cmd +27 -0
- data/lib/cli_tools/ic-extend-reservation.sh +14 -0
- data/lib/cli_tools/ic-generate-keypair.cmd +27 -0
- data/lib/cli_tools/ic-generate-keypair.sh +14 -0
- data/lib/cli_tools/ic-release-address.cmd +27 -0
- data/lib/cli_tools/ic-release-address.sh +14 -0
- data/lib/cli_tools/ic-remove-keypair.cmd +27 -0
- data/lib/cli_tools/ic-remove-keypair.sh +14 -0
- data/lib/cli_tools/ic-restart-instance.cmd +27 -0
- data/lib/cli_tools/ic-restart-instance.sh +14 -0
- data/lib/cli_tools/ic-save-instance.cmd +27 -0
- data/lib/cli_tools/ic-save-instance.sh +14 -0
- data/lib/cli_tools/ic-set-default-key.cmd +27 -0
- data/lib/cli_tools/ic-set-default-key.sh +14 -0
- data/lib/cli_tools/ic-update-instance.cmd +27 -0
- data/lib/cli_tools/ic-update-instance.sh +14 -0
- data/lib/cli_tools/ic-update-keypair.cmd +27 -0
- data/lib/cli_tools/ic-update-keypair.sh +14 -0
- data/lib/cli_tools/lib/DeveloperCloud_API_Client_JAR.jar +0 -0
- data/lib/cli_tools/lib/DeveloperCloud_CMD_Tool.jar +0 -0
- data/lib/cli_tools/lib/commons-beanutils-1.6.1.jar +0 -0
- data/lib/cli_tools/lib/commons-cli-1.2.jar +0 -0
- data/lib/cli_tools/lib/commons-codec-1.3.jar +0 -0
- data/lib/cli_tools/lib/commons-collections-3.2.1.jar +0 -0
- data/lib/cli_tools/lib/commons-digester-1.8.jar +0 -0
- data/lib/cli_tools/lib/commons-httpclient-3.1.jar +0 -0
- data/lib/cli_tools/lib/commons-lang-2.3.jar +0 -0
- data/lib/cli_tools/lib/commons-logging-1.1.1.jar +0 -0
- data/lib/cli_tools/logging.properties +7 -0
- data/lib/cli_tools/manifest.rmd +26 -0
- data/lib/config/config.yml +50 -0
- data/lib/hash_fix.rb +37 -0
- data/lib/mime-types-1.16/History.txt +107 -0
- data/lib/mime-types-1.16/Install.txt +17 -0
- data/lib/mime-types-1.16/Licence.txt +15 -0
- data/lib/mime-types-1.16/Manifest.txt +12 -0
- data/lib/mime-types-1.16/README.txt +28 -0
- data/lib/mime-types-1.16/Rakefile +316 -0
- data/lib/mime-types-1.16/lib/mime/types.rb +751 -0
- data/lib/mime-types-1.16/lib/mime/types.rb.data +1324 -0
- data/lib/mime-types-1.16/mime-types.gemspec +43 -0
- data/lib/mime-types-1.16/setup.rb +1585 -0
- data/lib/mime-types-1.16/test/test_mime_type.rb +356 -0
- data/lib/mime-types-1.16/test/test_mime_types.rb +122 -0
- data/lib/mock_smartcloud.rb +53 -0
- data/lib/rest-client-1.6.3/README.rdoc +276 -0
- data/lib/rest-client-1.6.3/Rakefile +66 -0
- data/lib/rest-client-1.6.3/VERSION +1 -0
- data/lib/rest-client-1.6.3/bin/restclient +92 -0
- data/lib/rest-client-1.6.3/history.md +112 -0
- data/lib/rest-client-1.6.3/lib/rest-client.rb +2 -0
- data/lib/rest-client-1.6.3/lib/rest_client.rb +2 -0
- data/lib/rest-client-1.6.3/lib/restclient/abstract_response.rb +106 -0
- data/lib/rest-client-1.6.3/lib/restclient/exceptions.rb +193 -0
- data/lib/rest-client-1.6.3/lib/restclient/net_http_ext.rb +21 -0
- data/lib/rest-client-1.6.3/lib/restclient/payload.rb +220 -0
- data/lib/rest-client-1.6.3/lib/restclient/raw_response.rb +34 -0
- data/lib/rest-client-1.6.3/lib/restclient/request.rb +314 -0
- data/lib/rest-client-1.6.3/lib/restclient/resource.rb +169 -0
- data/lib/rest-client-1.6.3/lib/restclient/response.rb +24 -0
- data/lib/rest-client-1.6.3/lib/restclient.rb +174 -0
- data/lib/restclient_fix.rb +41 -0
- data/lib/smartcloud.rb +616 -0
- data/lib/smartcloud_logger.rb +20 -0
- data/lib/xml-simple-1.0.12/lib/xmlsimple.rb +1028 -0
- data/script/console +3 -0
- data/test/helper.rb +22 -0
- metadata +196 -0
@@ -0,0 +1,193 @@
|
|
1
|
+
module RestClient
|
2
|
+
|
3
|
+
STATUSES = {100 => 'Continue',
|
4
|
+
101 => 'Switching Protocols',
|
5
|
+
102 => 'Processing', #WebDAV
|
6
|
+
|
7
|
+
200 => 'OK',
|
8
|
+
201 => 'Created',
|
9
|
+
202 => 'Accepted',
|
10
|
+
203 => 'Non-Authoritative Information', # http/1.1
|
11
|
+
204 => 'No Content',
|
12
|
+
205 => 'Reset Content',
|
13
|
+
206 => 'Partial Content',
|
14
|
+
207 => 'Multi-Status', #WebDAV
|
15
|
+
|
16
|
+
300 => 'Multiple Choices',
|
17
|
+
301 => 'Moved Permanently',
|
18
|
+
302 => 'Found',
|
19
|
+
303 => 'See Other', # http/1.1
|
20
|
+
304 => 'Not Modified',
|
21
|
+
305 => 'Use Proxy', # http/1.1
|
22
|
+
306 => 'Switch Proxy', # no longer used
|
23
|
+
307 => 'Temporary Redirect', # http/1.1
|
24
|
+
|
25
|
+
400 => 'Bad Request',
|
26
|
+
401 => 'Unauthorized',
|
27
|
+
402 => 'Payment Required',
|
28
|
+
403 => 'Forbidden',
|
29
|
+
404 => 'Resource Not Found',
|
30
|
+
405 => 'Method Not Allowed',
|
31
|
+
406 => 'Not Acceptable',
|
32
|
+
407 => 'Proxy Authentication Required',
|
33
|
+
408 => 'Request Timeout',
|
34
|
+
409 => 'Conflict',
|
35
|
+
410 => 'Gone',
|
36
|
+
411 => 'Length Required',
|
37
|
+
412 => 'Precondition Failed',
|
38
|
+
413 => 'Request Entity Too Large',
|
39
|
+
414 => 'Request-URI Too Long',
|
40
|
+
415 => 'Unsupported Media Type',
|
41
|
+
416 => 'Requested Range Not Satisfiable',
|
42
|
+
417 => 'Expectation Failed',
|
43
|
+
418 => 'I\'m A Teapot',
|
44
|
+
421 => 'Too Many Connections From This IP',
|
45
|
+
422 => 'Unprocessable Entity', #WebDAV
|
46
|
+
423 => 'Locked', #WebDAV
|
47
|
+
424 => 'Failed Dependency', #WebDAV
|
48
|
+
425 => 'Unordered Collection', #WebDAV
|
49
|
+
426 => 'Upgrade Required',
|
50
|
+
449 => 'Retry With', #Microsoft
|
51
|
+
450 => 'Blocked By Windows Parental Controls', #Microsoft
|
52
|
+
|
53
|
+
500 => 'Internal Server Error',
|
54
|
+
501 => 'Not Implemented',
|
55
|
+
502 => 'Bad Gateway',
|
56
|
+
503 => 'Service Unavailable',
|
57
|
+
504 => 'Gateway Timeout',
|
58
|
+
505 => 'HTTP Version Not Supported',
|
59
|
+
506 => 'Variant Also Negotiates',
|
60
|
+
507 => 'Insufficient Storage', #WebDAV
|
61
|
+
509 => 'Bandwidth Limit Exceeded', #Apache
|
62
|
+
510 => 'Not Extended'}
|
63
|
+
|
64
|
+
# Compatibility : make the Response act like a Net::HTTPResponse when needed
|
65
|
+
module ResponseForException
|
66
|
+
def method_missing symbol, *args
|
67
|
+
if net_http_res.respond_to? symbol
|
68
|
+
warn "[warning] The response contained in an RestClient::Exception is now a RestClient::Response instead of a Net::HTTPResponse, please update your code"
|
69
|
+
net_http_res.send symbol, *args
|
70
|
+
else
|
71
|
+
super
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# This is the base RestClient exception class. Rescue it if you want to
|
77
|
+
# catch any exception that your request might raise
|
78
|
+
# You can get the status code by e.http_code, or see anything about the
|
79
|
+
# response via e.response.
|
80
|
+
# For example, the entire result body (which is
|
81
|
+
# probably an HTML error page) is e.response.
|
82
|
+
class Exception < RuntimeError
|
83
|
+
attr_accessor :response
|
84
|
+
attr_writer :message
|
85
|
+
|
86
|
+
def initialize response = nil, initial_response_code = nil
|
87
|
+
@response = response
|
88
|
+
@initial_response_code = initial_response_code
|
89
|
+
|
90
|
+
# compatibility: this make the exception behave like a Net::HTTPResponse
|
91
|
+
response.extend ResponseForException if response
|
92
|
+
end
|
93
|
+
|
94
|
+
def http_code
|
95
|
+
# return integer for compatibility
|
96
|
+
if @response
|
97
|
+
@response.code.to_i
|
98
|
+
else
|
99
|
+
@initial_response_code
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def http_body
|
104
|
+
@response.body if @response
|
105
|
+
end
|
106
|
+
|
107
|
+
def inspect
|
108
|
+
"#{message}: #{http_body}"
|
109
|
+
end
|
110
|
+
|
111
|
+
def to_s
|
112
|
+
inspect
|
113
|
+
end
|
114
|
+
|
115
|
+
def message
|
116
|
+
@message || self.class.name
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
# Compatibility
|
122
|
+
class ExceptionWithResponse < Exception
|
123
|
+
end
|
124
|
+
|
125
|
+
# The request failed with an error code not managed by the code
|
126
|
+
class RequestFailed < ExceptionWithResponse
|
127
|
+
|
128
|
+
def message
|
129
|
+
"HTTP status code #{http_code}"
|
130
|
+
end
|
131
|
+
|
132
|
+
def to_s
|
133
|
+
message
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# We will a create an exception for each status code, see http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
|
138
|
+
module Exceptions
|
139
|
+
# Map http status codes to the corresponding exception class
|
140
|
+
EXCEPTIONS_MAP = {}
|
141
|
+
end
|
142
|
+
|
143
|
+
STATUSES.each_pair do |code, message|
|
144
|
+
|
145
|
+
# Compatibility
|
146
|
+
superclass = ([304, 401, 404].include? code) ? ExceptionWithResponse : RequestFailed
|
147
|
+
klass = Class.new(superclass) do
|
148
|
+
send(:define_method, :message) {"#{http_code ? "#{http_code} " : ''}#{message}"}
|
149
|
+
end
|
150
|
+
klass_constant = const_set message.delete(' \-\''), klass
|
151
|
+
Exceptions::EXCEPTIONS_MAP[code] = klass_constant
|
152
|
+
end
|
153
|
+
|
154
|
+
# A redirect was encountered; caught by execute to retry with the new url.
|
155
|
+
class Redirect < Exception
|
156
|
+
|
157
|
+
message = 'Redirect'
|
158
|
+
|
159
|
+
attr_accessor :url
|
160
|
+
|
161
|
+
def initialize(url)
|
162
|
+
@url = url
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
class MaxRedirectsReached < Exception
|
167
|
+
message = 'Maximum number of redirect reached'
|
168
|
+
end
|
169
|
+
|
170
|
+
# The server broke the connection prior to the request completing. Usually
|
171
|
+
# this means it crashed, or sometimes that your network connection was
|
172
|
+
# severed before it could complete.
|
173
|
+
class ServerBrokeConnection < Exception
|
174
|
+
def initialize(message = 'Server broke connection')
|
175
|
+
super nil, nil
|
176
|
+
self.message = message
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class SSLCertificateNotVerified < Exception
|
181
|
+
def initialize(message)
|
182
|
+
super nil, nil
|
183
|
+
self.message = message
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# backwards compatibility
|
189
|
+
class RestClient::Request
|
190
|
+
Redirect = RestClient::Redirect
|
191
|
+
Unauthorized = RestClient::Unauthorized
|
192
|
+
RequestFailed = RestClient::RequestFailed
|
193
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#
|
2
|
+
# Replace the request method in Net::HTTP to sniff the body type
|
3
|
+
# and set the stream if appropriate
|
4
|
+
#
|
5
|
+
# Taken from:
|
6
|
+
# http://www.missiondata.com/blog/ruby/29/streaming-data-to-s3-with-ruby/
|
7
|
+
|
8
|
+
module Net
|
9
|
+
class HTTP
|
10
|
+
alias __request__ request
|
11
|
+
|
12
|
+
def request(req, body=nil, &block)
|
13
|
+
if body != nil && body.respond_to?(:read)
|
14
|
+
req.body_stream = body
|
15
|
+
return __request__(req, nil, &block)
|
16
|
+
else
|
17
|
+
return __request__(req, body, &block)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'stringio'
|
3
|
+
require 'mime/types'
|
4
|
+
|
5
|
+
module RestClient
|
6
|
+
module Payload
|
7
|
+
extend self
|
8
|
+
|
9
|
+
def generate(params)
|
10
|
+
if params.is_a?(String)
|
11
|
+
Base.new(params)
|
12
|
+
elsif params.respond_to?(:read)
|
13
|
+
Streamed.new(params)
|
14
|
+
elsif params
|
15
|
+
if params.delete(:multipart) == true || has_file?(params)
|
16
|
+
Multipart.new(params)
|
17
|
+
else
|
18
|
+
UrlEncoded.new(params)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_file?(params)
|
26
|
+
params.any? do |_, v|
|
27
|
+
case v
|
28
|
+
when Hash
|
29
|
+
has_file?(v)
|
30
|
+
else
|
31
|
+
v.respond_to?(:path) && v.respond_to?(:read)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class Base
|
37
|
+
def initialize(params)
|
38
|
+
build_stream(params)
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_stream(params)
|
42
|
+
@stream = StringIO.new(params)
|
43
|
+
@stream.seek(0)
|
44
|
+
end
|
45
|
+
|
46
|
+
def read(bytes=nil)
|
47
|
+
@stream.read(bytes)
|
48
|
+
end
|
49
|
+
|
50
|
+
alias :to_s :read
|
51
|
+
|
52
|
+
# Flatten parameters by converting hashes of hashes to flat hashes
|
53
|
+
# {keys1 => {keys2 => value}} will be transformed into [keys1[key2], value]
|
54
|
+
def flatten_params(params, parent_key = nil)
|
55
|
+
result = []
|
56
|
+
params.each do |key, value|
|
57
|
+
calculated_key = parent_key ? "#{parent_key}[#{handle_key(key)}]" : handle_key(key)
|
58
|
+
if value.is_a? Hash
|
59
|
+
result += flatten_params(value, calculated_key)
|
60
|
+
elsif value.is_a? Array
|
61
|
+
result += flatten_params_array(value, calculated_key)
|
62
|
+
else
|
63
|
+
result << [calculated_key, value]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
result
|
67
|
+
end
|
68
|
+
|
69
|
+
def flatten_params_array value, calculated_key
|
70
|
+
result = []
|
71
|
+
value.each do |elem|
|
72
|
+
if elem.is_a? Hash
|
73
|
+
result += flatten_params(elem, calculated_key)
|
74
|
+
elsif elem.is_a? Array
|
75
|
+
result += flatten_params_array(elem, calculated_key)
|
76
|
+
else
|
77
|
+
result << ["#{calculated_key}[]", elem]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
result
|
81
|
+
end
|
82
|
+
|
83
|
+
def headers
|
84
|
+
{'Content-Length' => size.to_s}
|
85
|
+
end
|
86
|
+
|
87
|
+
def size
|
88
|
+
@stream.size
|
89
|
+
end
|
90
|
+
|
91
|
+
alias :length :size
|
92
|
+
|
93
|
+
def close
|
94
|
+
@stream.close
|
95
|
+
end
|
96
|
+
|
97
|
+
def inspect
|
98
|
+
result = to_s.inspect
|
99
|
+
@stream.seek(0)
|
100
|
+
result
|
101
|
+
end
|
102
|
+
|
103
|
+
def short_inspect
|
104
|
+
(size > 500 ? "#{size} byte(s) length" : inspect)
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
class Streamed < Base
|
110
|
+
def build_stream(params = nil)
|
111
|
+
@stream = params
|
112
|
+
end
|
113
|
+
|
114
|
+
def size
|
115
|
+
if @stream.respond_to?(:size)
|
116
|
+
@stream.size
|
117
|
+
elsif @stream.is_a?(IO)
|
118
|
+
@stream.stat.size
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
alias :length :size
|
123
|
+
end
|
124
|
+
|
125
|
+
class UrlEncoded < Base
|
126
|
+
def build_stream(params = nil)
|
127
|
+
@stream = StringIO.new(flatten_params(params).collect do |entry|
|
128
|
+
"#{entry[0]}=#{handle_key(entry[1])}"
|
129
|
+
end.join("&"))
|
130
|
+
@stream.seek(0)
|
131
|
+
end
|
132
|
+
|
133
|
+
# for UrlEncoded escape the keys
|
134
|
+
def handle_key key
|
135
|
+
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
136
|
+
end
|
137
|
+
|
138
|
+
def headers
|
139
|
+
super.merge({'Content-Type' => 'application/x-www-form-urlencoded'})
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
class Multipart < Base
|
144
|
+
EOL = "\r\n"
|
145
|
+
|
146
|
+
def build_stream(params)
|
147
|
+
b = "--#{boundary}"
|
148
|
+
|
149
|
+
@stream = Tempfile.new("RESTClient.Stream.#{rand(1000)}")
|
150
|
+
@stream.binmode
|
151
|
+
@stream.write(b + EOL)
|
152
|
+
|
153
|
+
if params.is_a? Hash
|
154
|
+
x = flatten_params(params)
|
155
|
+
else
|
156
|
+
x = params
|
157
|
+
end
|
158
|
+
|
159
|
+
last_index = x.length - 1
|
160
|
+
x.each_with_index do |a, index|
|
161
|
+
k, v = * a
|
162
|
+
if v.respond_to?(:read) && v.respond_to?(:path)
|
163
|
+
create_file_field(@stream, k, v)
|
164
|
+
else
|
165
|
+
create_regular_field(@stream, k, v)
|
166
|
+
end
|
167
|
+
@stream.write(EOL + b)
|
168
|
+
@stream.write(EOL) unless last_index == index
|
169
|
+
end
|
170
|
+
@stream.write('--')
|
171
|
+
@stream.write(EOL)
|
172
|
+
@stream.seek(0)
|
173
|
+
end
|
174
|
+
|
175
|
+
def create_regular_field(s, k, v)
|
176
|
+
s.write("Content-Disposition: form-data; name=\"#{k}\"")
|
177
|
+
s.write(EOL)
|
178
|
+
s.write(EOL)
|
179
|
+
s.write(v)
|
180
|
+
end
|
181
|
+
|
182
|
+
def create_file_field(s, k, v)
|
183
|
+
begin
|
184
|
+
s.write("Content-Disposition: form-data;")
|
185
|
+
s.write(" name=\"#{k}\";") unless (k.nil? || k=='')
|
186
|
+
s.write(" filename=\"#{v.respond_to?(:original_filename) ? v.original_filename : File.basename(v.path)}\"#{EOL}")
|
187
|
+
s.write("Content-Type: #{v.respond_to?(:content_type) ? v.content_type : mime_for(v.path)}#{EOL}")
|
188
|
+
s.write(EOL)
|
189
|
+
while data = v.read(8124)
|
190
|
+
s.write(data)
|
191
|
+
end
|
192
|
+
ensure
|
193
|
+
v.close if v.respond_to?(:close)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
def mime_for(path)
|
198
|
+
mime = MIME::Types.type_for path
|
199
|
+
mime.empty? ? 'text/plain' : mime[0].content_type
|
200
|
+
end
|
201
|
+
|
202
|
+
def boundary
|
203
|
+
@boundary ||= rand(1_000_000).to_s
|
204
|
+
end
|
205
|
+
|
206
|
+
# for Multipart do not escape the keys
|
207
|
+
def handle_key key
|
208
|
+
key
|
209
|
+
end
|
210
|
+
|
211
|
+
def headers
|
212
|
+
super.merge({'Content-Type' => %Q{multipart/form-data; boundary=#{boundary}}})
|
213
|
+
end
|
214
|
+
|
215
|
+
def close
|
216
|
+
@stream.close!
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module RestClient
|
2
|
+
# The response from RestClient on a raw request looks like a string, but is
|
3
|
+
# actually one of these. 99% of the time you're making a rest call all you
|
4
|
+
# care about is the body, but on the occassion you want to fetch the
|
5
|
+
# headers you can:
|
6
|
+
#
|
7
|
+
# RestClient.get('http://example.com').headers[:content_type]
|
8
|
+
#
|
9
|
+
# In addition, if you do not use the response as a string, you can access
|
10
|
+
# a Tempfile object at res.file, which contains the path to the raw
|
11
|
+
# downloaded request body.
|
12
|
+
class RawResponse
|
13
|
+
|
14
|
+
include AbstractResponse
|
15
|
+
|
16
|
+
attr_reader :file
|
17
|
+
|
18
|
+
def initialize tempfile, net_http_res, args
|
19
|
+
@net_http_res = net_http_res
|
20
|
+
@args = args
|
21
|
+
@file = tempfile
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_s
|
25
|
+
@file.open
|
26
|
+
@file.read
|
27
|
+
end
|
28
|
+
|
29
|
+
def size
|
30
|
+
File.size file
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
end
|