Permalink
Browse files

Removed to_params as it is not being used anymore.

  • Loading branch information...
1 parent 9828637 commit 7e04e2549202477667fd0b6d8b844d13008137c5 @jnunemaker committed Sep 17, 2011
Showing with 0 additions and 77 deletions.
  1. +0 −48 lib/crack/core_extensions.rb
  2. +0 −29 test/hash_test.rb
@@ -14,54 +14,6 @@ def blank?
end # class Object
class Hash #:nodoc:
- # @return <String> This hash as a query string
- #
- # @example
- # { :name => "Bob",
- # :address => {
- # :street => '111 Ruby Ave.',
- # :city => 'Ruby Central',
- # :phones => ['111-111-1111', '222-222-2222']
- # }
- # }.to_params
- # #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
- def to_params
- params = self.map { |k,v| normalize_param(k,v) }.join
- params.chop! # trailing &
- params
- end
-
- # @param key<Object> The key for the param.
- # @param value<Object> The value for the param.
- #
- # @return <String> This key value pair as a param
- #
- # @example normalize_param(:name, "Bob Jones") #=> "name=Bob%20Jones&"
- def normalize_param(key, value)
- param = ''
- stack = []
-
- if value.is_a?(Array)
- param << value.map { |element| normalize_param("#{key}[]", element) }.join
- elsif value.is_a?(Hash)
- stack << [key,value]
- else
- param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
- end
-
- stack.each do |parent, hash|
- hash.each do |key, value|
- if value.is_a?(Hash)
- stack << ["#{parent}[#{key}]", value]
- else
- param << normalize_param("#{parent}[#{key}]", value)
- end
- end
- end
-
- param
- end
-
# @return <String> The hash as attributes for an XML tag.
#
# @example
View
@@ -25,33 +25,4 @@ class CrackTest < Test::Unit::TestCase
attrs.should =~ /crash="burn"/
end
end
-
- context "to_params" do
- {
- { "foo" => "bar", "baz" => "bat" } => "foo=bar&baz=bat",
- { "foo" => [ "bar", "baz" ] } => "foo[]=bar&foo[]=baz",
- { "foo" => [ {"bar" => "1"}, {"bar" => 2} ] } => "foo[][bar]=1&foo[][bar]=2",
- { "foo" => { "bar" => [ {"baz" => 1}, {"baz" => "2"} ] } } => "foo[bar][][baz]=1&foo[bar][][baz]=2",
- { "foo" => {"1" => "bar", "2" => "baz"} } => "foo[1]=bar&foo[2]=baz"
- }.each do |hash, params|
- should "should covert hash: #{hash.inspect} to params: #{params.inspect}" do
- hash.to_params.split('&').sort.should == params.split('&').sort
- end
- end
-
- should 'not leave a trailing &' do
- {
- :name => 'Bob',
- :address => {
- :street => '111 Ruby Ave.',
- :city => 'Ruby Central',
- :phones => ['111-111-1111', '222-222-2222']
- }
- }.to_params.should_not =~ /&$/
- end
-
- should 'URL encode unsafe characters' do
- {:q => "?&\" +"}.to_params.should == "q=%3F%26%22%20%2B"
- end
- end
end

0 comments on commit 7e04e25

Please sign in to comment.