Skip to content

Commit

Permalink
You can now party without crack.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Sep 13, 2011
1 parent 7788ac9 commit 971e1d1
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 20 deletions.
8 changes: 5 additions & 3 deletions 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'
2 changes: 1 addition & 1 deletion README.rdoc
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion features/steps/env.rb
@@ -1,5 +1,4 @@
require 'mongrel'
require 'active_support'
require './lib/httparty'
require 'spec/expectations'

Expand Down
7 changes: 0 additions & 7 deletions httparty.gemspec
Expand Up @@ -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")
Expand Down
7 changes: 1 addition & 6 deletions lib/httparty.rb
Expand Up @@ -3,7 +3,6 @@
require 'net/https'
require 'uri'
require 'zlib'
require 'crack'
require 'multi_xml'
require 'multi_json'

Expand Down Expand Up @@ -443,15 +442,11 @@ def self.head(*args)
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 changes: 51 additions & 0 deletions lib/httparty/hash_conversions.rb
@@ -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
4 changes: 2 additions & 2 deletions lib/httparty/request.rb
Expand Up @@ -18,7 +18,7 @@ class Request #:nodoc:
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
Expand Down Expand Up @@ -143,7 +143,7 @@ def normalize_query(query)
if query_string_normalizer
query_string_normalizer.call(query)
else
query.to_params
HashConversions.to_params(query)
end
end

Expand Down

0 comments on commit 971e1d1

Please sign in to comment.