mirror of
https://github.com/jnunemaker/httparty
synced 2023-03-27 23:23:07 -04:00
You can now party without crack.
This commit is contained in:
parent
7788ac9479
commit
971e1d150f
7 changed files with 60 additions and 20 deletions
8
Gemfile
8
Gemfile
|
@ -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'
|
||||
|
|
|
@ -42,7 +42,7 @@ options. Below is an example of how easy it is.
|
|||
|
||||
== Requirements
|
||||
|
||||
* Crack http://github.com/jnunemaker/crack/ - For XML and JSON parsing.
|
||||
* multijson and multixml
|
||||
* You like to party!
|
||||
|
||||
== Install
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
require 'mongrel'
|
||||
require 'active_support'
|
||||
require './lib/httparty'
|
||||
require 'spec/expectations'
|
||||
|
||||
|
|
|
@ -12,16 +12,9 @@ Gem::Specification.new do |s|
|
|||
s.summary = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
||||
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
|
||||
|
||||
s.add_dependency 'crack', HTTParty::CRACK_DEPENDENCY
|
||||
s.add_dependency 'multi_json'
|
||||
s.add_dependency 'multi_xml'
|
||||
|
||||
s.add_development_dependency "activesupport", "~> 2.3"
|
||||
s.add_development_dependency "cucumber", "~> 0.7"
|
||||
s.add_development_dependency "fakeweb", "~> 1.2"
|
||||
s.add_development_dependency "rspec", "~> 1.3"
|
||||
s.add_development_dependency "mongrel", "1.2.0.pre2"
|
||||
|
||||
s.post_install_message = "When you HTTParty, you must party hard!"
|
||||
|
||||
s.files = `git ls-files`.split("\n")
|
||||
|
|
|
@ -3,7 +3,6 @@ require 'net/http'
|
|||
require 'net/https'
|
||||
require 'uri'
|
||||
require 'zlib'
|
||||
require 'crack'
|
||||
require 'multi_xml'
|
||||
require 'multi_json'
|
||||
|
||||
|
@ -443,15 +442,11 @@ module HTTParty
|
|||
def self.options(*args)
|
||||
Basement.options(*args)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
require 'httparty/core_extensions'
|
||||
require 'httparty/hash_conversions'
|
||||
require 'httparty/exceptions'
|
||||
require 'httparty/parser'
|
||||
require 'httparty/request'
|
||||
require 'httparty/response'
|
||||
|
||||
if Crack::VERSION != HTTParty::CRACK_DEPENDENCY
|
||||
warn "warning: HTTParty depends on version #{HTTParty::CRACK_DEPENDENCY} of crack, not #{Crack::VERSION}."
|
||||
end
|
||||
|
|
51
lib/httparty/hash_conversions.rb
Normal file
51
lib/httparty/hash_conversions.rb
Normal file
|
@ -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
|
|
@ -18,7 +18,7 @@ module HTTParty
|
|||
elsif value.is_a?(Array)
|
||||
value.map {|v| "#{key}=#{URI.encode(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}
|
||||
else
|
||||
{key => value}.to_params
|
||||
HashConversions.to_params(key => value)
|
||||
end
|
||||
end.flatten.sort.join('&')
|
||||
end
|
||||
|
@ -143,7 +143,7 @@ module HTTParty
|
|||
if query_string_normalizer
|
||||
query_string_normalizer.call(query)
|
||||
else
|
||||
query.to_params
|
||||
HashConversions.to_params(query)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue