forked from jnunemaker/httparty
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7788ac9
commit 971e1d1
Showing
7 changed files
with
60 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
source :rubygems | ||
gemspec | ||
|
||
group(:development) do | ||
gem 'rake', '~> 0.8.7' | ||
end | ||
gem 'rake', '~> 0.8.7' | ||
gem 'cucumber', '~> 0.7' | ||
gem 'fakeweb', '~> 1.2' | ||
gem 'rspec', '~> 1.3' | ||
gem 'mongrel', '1.2.0.pre2' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
require 'mongrel' | ||
require 'active_support' | ||
require './lib/httparty' | ||
require 'spec/expectations' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module HTTParty | ||
module HashConversions | ||
# @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 self.to_params(hash) | ||
params = hash.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 self.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 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters