Permalink
Browse files

You can now party without crack.

  • Loading branch information...
1 parent 7788ac9 commit 971e1d150f54f5627a82f39b7a3590fef6944da4 @jnunemaker committed Sep 13, 2011
Showing with 60 additions and 20 deletions.
  1. +5 −3 Gemfile
  2. +1 −1 README.rdoc
  3. +0 −1 features/steps/env.rb
  4. +0 −7 httparty.gemspec
  5. +1 −6 lib/httparty.rb
  6. +51 −0 lib/httparty/hash_conversions.rb
  7. +2 −2 lib/httparty/request.rb
View
@@ -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'
View
@@ -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
View
@@ -1,5 +1,4 @@
require 'mongrel'
-require 'active_support'
require './lib/httparty'
require 'spec/expectations'
View
@@ -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")
View
@@ -3,7 +3,6 @@
require 'net/https'
require 'uri'
require 'zlib'
-require 'crack'
require 'multi_xml'
require 'multi_json'
@@ -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
@@ -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
View
@@ -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
@@ -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

6 comments on commit 971e1d1

👍

This is the first hit when you google "party without crack."

@hakanensari that's because no one has every suggested partying without crack. Also Github has ridiculous pagerank.

No one has every suggested partying without crack?

#75 (comment)
#89

Owner

jnunemaker replied Sep 15, 2011

@galaxylord Pretty sure @chriseppstein was making a joke. :)

*self.facepalm...

Please sign in to comment.