Skip to content

Commit

Permalink
Refactor out .convert_query_string_into_array
Browse files Browse the repository at this point in the history
   this commit refactors out a new private class method
  .convert_query_string_into_array from the .values_to_query method
  in WebMock::Util::QueryMapper
  • Loading branch information
davidbegin committed Sep 13, 2015
1 parent 5287372 commit c879f2e
Showing 1 changed file with 42 additions and 28 deletions.
70 changes: 42 additions & 28 deletions lib/webmock/util/query_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,38 +180,13 @@ def values_to_query(new_query_values, options = {})
return if new_query_values.nil?

unless new_query_values.is_a?(Array)
unless new_query_values.respond_to?(:to_hash)
raise TypeError,
"Can't convert #{new_query_values.class} into Hash."
end
new_query_values = new_query_values.to_hash
new_query_values = new_query_values.inject([]) do |object, (key, value)|

value = QueryValueStringifier.stringify(value)

key = key.to_s if key.is_a?(::Symbol) || key.nil?
if value.is_a?(Array)
value.each { |v| object << [key.to_s + '[]', v] }
elsif value.is_a?(Hash)
value.each { |k, v| object << ["#{key.to_s}[#{k}]", v]}
else
object << [key.to_s, value]
end
object
end
# Useful default for OAuth and caching.
# Only to be used for non-Array inputs. Arrays should preserve order.
begin
new_query_values.sort! # may raise for non-comparable values
rescue NoMethodError, ArgumentError
# ignore
end
new_query_values = convert_query_string_into_array(new_query_values)
end

buffer = ''
new_query_values.each do |parent, value|
encoded_parent = ::Addressable::URI.encode_component(
parent.dup, ::Addressable::URI::CharacterClasses::UNRESERVED
parent.dup, ::Addressable::URI::CharacterClasses::UNRESERVED
)
buffer << "#{to_query(encoded_parent, value, options)}&"
end
Expand Down Expand Up @@ -250,7 +225,10 @@ def to_query(parent, value, options = {})
when ::Hash
value = value.map do |key, val|
[
::Addressable::URI.encode_component(key.to_s.dup, ::Addressable::URI::CharacterClasses::UNRESERVED),
::Addressable::URI.encode_component(
key.to_s.dup,
::Addressable::URI::CharacterClasses::UNRESERVED
),
val
]
end
Expand All @@ -275,7 +253,43 @@ def to_query(parent, value, options = {})
"#{parent}=#{encoded_value}"
end
end

def convert_query_string_into_array(new_query_values)
unless new_query_values.respond_to?(:to_hash)
raise TypeError,
"Can't convert #{new_query_values.class} into Hash."
end

new_query_values = new_query_values.to_hash

new_query_values = new_query_values.inject([]) do |object, (key, value)|
key = key.to_s if key.is_a?(::Symbol) || key.nil?

case value = QueryValueStringifier.stringify(value)
when Array
value.each { |v| object << [key.to_s + '[]', v] }
when Hash
value.each { |k, v| object << ["#{key.to_s}[#{k}]", v]}
else
object << [key.to_s, value]
end

object
end

# Useful default for OAuth and caching.
# Only to be used for non-Array inputs. Arrays should preserve order.
begin
new_query_values.sort! # may raise for non-comparable values
rescue NoMethodError, ArgumentError
# ignore
end

new_query_values
end

end

private_class_method :convert_query_string_into_array
end
end

0 comments on commit c879f2e

Please sign in to comment.