right_support 2.3.1 → 2.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -174,14 +174,15 @@ module RightSupport::Net
174
174
  private
175
175
 
176
176
  # Process a query-string option and append it to the URL as a properly
177
- # encoded query string. The input must be either a String or Hash.
177
+ # encoded query string. The input must be either a String or Hash and the
178
+ # Hash may recursively contain String, Hash, and Array values.
178
179
  #
179
180
  # === Parameters
180
181
  # url(String):: the URL to request, including any query-string parameters
181
182
  # query(Hash|String):: the URL params, that will be added to URL, Hash or String
182
183
  #
183
184
  # === Return
184
- # Returns url with concated with parameters.
185
+ # (String) url concatenated with parameters
185
186
  def process_query_string(url='', query={})
186
187
  url_params = ''
187
188
 
@@ -193,7 +194,7 @@ module RightSupport::Net
193
194
  uri.query_values = query
194
195
  url_params = uri.query
195
196
  else
196
- url_params = query.collect { |k, v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
197
+ url_params = hash_to_query_string(query)
197
198
  end
198
199
  else
199
200
  raise ArgumentError.new("Parameter query should be String or Hash")
@@ -205,6 +206,56 @@ module RightSupport::Net
205
206
  url + url_params
206
207
  end
207
208
 
209
+ # Convert a hash into a string suitable for use as a URL query string
210
+ #
211
+ # === Examples
212
+ # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
213
+ #
214
+ # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
215
+ #
216
+ # === Parameters
217
+ # hash(Hash):: Hash to be converted
218
+ # namespace(String|nil):: Optional namespace to enclose param names
219
+ #
220
+ # === Return
221
+ # (String):: Converted hash
222
+ def hash_to_query_string(hash, namespace = nil)
223
+ hash.collect do |key, value|
224
+ key = namespace ? "#{namespace}[#{key}]" : key
225
+ if value.kind_of?(Hash)
226
+ hash_to_query_string(value, key)
227
+ elsif value.kind_of?(Array)
228
+ array_to_query_string(value, key)
229
+ else
230
+ "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
231
+ end
232
+ end.sort.join('&')
233
+ end
234
+
235
+ # Convert an array into a string suitable for use as a URL query string
236
+ #
237
+ # === Examples
238
+ # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
239
+ #
240
+ # === Parameters
241
+ # array(Array):: Array to be converted
242
+ # key(String):: Param name
243
+ #
244
+ # === Return
245
+ # (String):: Converted array
246
+ def array_to_query_string(array, key)
247
+ prefix = "#{key}[]"
248
+ array.collect do |value|
249
+ if value.kind_of?(Hash)
250
+ hash_to_query_string(value, prefix)
251
+ elsif value.kind_of?(Array)
252
+ array_to_query_string(value, prefix)
253
+ else
254
+ "#{CGI.escape(prefix)}=#{CGI.escape(value.to_s)}"
255
+ end
256
+ end.join('&')
257
+ end
258
+
208
259
  # Wrapper around RestClient::Request.execute -- see class documentation for details.
209
260
  def request_internal(options, &block)
210
261
  if HAS_REST_CLIENT
@@ -7,8 +7,8 @@ spec = Gem::Specification.new do |s|
7
7
  s.required_ruby_version = Gem::Requirement.new(">= 1.8.7")
8
8
 
9
9
  s.name = 'right_support'
10
- s.version = '2.3.1'
11
- s.date = '2012-07-12'
10
+ s.version = '2.3.2'
11
+ s.date = '2012-08-06'
12
12
 
13
13
  s.authors = ['Tony Spataro', 'Sergey Sergyenko', 'Ryan Williamson', 'Lee Kirchhoff', 'Sergey Enin']
14
14
  s.email = 'support@rightscale.com'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right_support
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
8
  - 3
9
- - 1
10
- version: 2.3.1
9
+ - 2
10
+ version: 2.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tony Spataro
@@ -19,7 +19,7 @@ autorequire:
19
19
  bindir: bin
20
20
  cert_chain: []
21
21
 
22
- date: 2012-07-12 00:00:00 -07:00
22
+ date: 2012-08-06 00:00:00 -07:00
23
23
  default_executable:
24
24
  dependencies: []
25
25