@@ -180,38 +180,13 @@ def values_to_query(new_query_values, options = {})
180180 return if new_query_values . nil?
181181
182182 unless new_query_values . is_a? ( Array )
183- unless new_query_values . respond_to? ( :to_hash )
184- raise TypeError ,
185- "Can't convert #{ new_query_values . class } into Hash."
186- end
187- new_query_values = new_query_values . to_hash
188- new_query_values = new_query_values . inject ( [ ] ) do |object , ( key , value ) |
189-
190- value = QueryValueStringifier . stringify ( value )
191-
192- key = key . to_s if key . is_a? ( ::Symbol ) || key . nil?
193- if value . is_a? ( Array )
194- value . each { |v | object << [ key . to_s + '[]' , v ] }
195- elsif value . is_a? ( Hash )
196- value . each { |k , v | object << [ "#{ key . to_s } [#{ k } ]" , v ] }
197- else
198- object << [ key . to_s , value ]
199- end
200- object
201- end
202- # Useful default for OAuth and caching.
203- # Only to be used for non-Array inputs. Arrays should preserve order.
204- begin
205- new_query_values . sort! # may raise for non-comparable values
206- rescue NoMethodError , ArgumentError
207- # ignore
208- end
183+ new_query_values = convert_query_string_into_array ( new_query_values )
209184 end
210185
211186 buffer = ''
212187 new_query_values . each do |parent , value |
213188 encoded_parent = ::Addressable ::URI . encode_component (
214- parent . dup , ::Addressable ::URI ::CharacterClasses ::UNRESERVED
189+ parent . dup , ::Addressable ::URI ::CharacterClasses ::UNRESERVED
215190 )
216191 buffer << "#{ to_query ( encoded_parent , value , options ) } &"
217192 end
@@ -250,7 +225,10 @@ def to_query(parent, value, options = {})
250225 when ::Hash
251226 value = value . map do |key , val |
252227 [
253- ::Addressable ::URI . encode_component ( key . to_s . dup , ::Addressable ::URI ::CharacterClasses ::UNRESERVED ) ,
228+ ::Addressable ::URI . encode_component (
229+ key . to_s . dup ,
230+ ::Addressable ::URI ::CharacterClasses ::UNRESERVED
231+ ) ,
254232 val
255233 ]
256234 end
@@ -275,7 +253,43 @@ def to_query(parent, value, options = {})
275253 "#{ parent } =#{ encoded_value } "
276254 end
277255 end
256+
257+ def convert_query_string_into_array ( new_query_values )
258+ unless new_query_values . respond_to? ( :to_hash )
259+ raise TypeError ,
260+ "Can't convert #{ new_query_values . class } into Hash."
261+ end
262+
263+ new_query_values = new_query_values . to_hash
264+
265+ new_query_values = new_query_values . inject ( [ ] ) do |object , ( key , value ) |
266+ key = key . to_s if key . is_a? ( ::Symbol ) || key . nil?
267+
268+ case value = QueryValueStringifier . stringify ( value )
269+ when Array
270+ value . each { |v | object << [ key . to_s + '[]' , v ] }
271+ when Hash
272+ value . each { |k , v | object << [ "#{ key . to_s } [#{ k } ]" , v ] }
273+ else
274+ object << [ key . to_s , value ]
275+ end
276+
277+ object
278+ end
279+
280+ # Useful default for OAuth and caching.
281+ # Only to be used for non-Array inputs. Arrays should preserve order.
282+ begin
283+ new_query_values . sort! # may raise for non-comparable values
284+ rescue NoMethodError , ArgumentError
285+ # ignore
286+ end
287+
288+ new_query_values
289+ end
290+
278291 end
279292
293+ private_class_method :convert_query_string_into_array
280294 end
281295end
0 commit comments