Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed weird uri normalizing issue that occurred when I was doing norm…

…alization in request object. Also caught a piece of ActiveSupport Hash#to_query that I was using. Added Hash#to_params to fix that bug.
  • Loading branch information...
commit 411340826ba683397df84e611956b34eae398d93 1 parent 7fab301
@jnunemaker authored
View
8 Manifest
@@ -6,15 +6,21 @@ examples/rubyurl.rb
examples/twitter.rb
examples/whoismyrep.rb
History
-httparty.gemspec
+lib/core_extensions.rb
+lib/httparty/exceptions.rb
lib/httparty/request.rb
lib/httparty/version.rb
lib/httparty.rb
+lib/module_level_inheritable_attributes.rb
Manifest
MIT-LICENSE
Rakefile
README
setup.rb
+spec/fixtures/delicious.xml
+spec/fixtures/google.html
+spec/fixtures/twitter.json
+spec/fixtures/twitter.xml
spec/httparty/request_spec.rb
spec/httparty_spec.rb
spec/spec.opts
View
3  examples/aaws.rb
@@ -1,3 +1,6 @@
+require 'rubygems'
+require 'activesupport'
+
dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require File.join(dir, 'httparty')
require 'pp'
View
2  examples/delicious.rb
@@ -18,7 +18,6 @@ def initialize(u, p)
# ie: posts(:query => {:tag => 'ruby'})
def posts(options={})
options.merge!({:basic_auth => @auth})
- # get posts and convert to structs so we can do .key instead of ['key'] with results
self.class.get('/posts/get', options)
end
@@ -33,4 +32,5 @@ def recent(options={})
delicious = Delicious.new(config['username'], config['password'])
pp delicious.posts(:query => {:tag => 'ruby'})
+pp delicious.recent
View
2  examples/twitter.rb
@@ -28,4 +28,4 @@ def post(text)
pp twitter.timeline
# pp twitter.timeline(:friends, :query => {:since_id => 868482746})
# pp twitter.timeline(:friends, :query => 'since_id=868482746')
-# pp twitter.post('this is a test')
+pp twitter.post('this is a test of 0.2.0')
View
14 httparty.gemspec
@@ -2,15 +2,15 @@
Gem::Specification.new do |s|
s.name = %q{httparty}
- s.version = "0.1.8"
+ s.version = "0.2.0"
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
s.authors = ["John Nunemaker"]
- s.date = %q{2008-12-05}
+ s.date = %q{2008-12-06}
s.description = %q{Makes http fun! Also, makes consuming restful web services dead easy.}
s.email = %q{nunemaker@gmail.com}
- s.extra_rdoc_files = ["lib/httparty/request.rb", "lib/httparty/version.rb", "lib/httparty.rb", "README"]
- s.files = ["examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "History", "httparty.gemspec", "lib/httparty/request.rb", "lib/httparty/version.rb", "lib/httparty.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/httparty/request_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "website/css/common.css", "website/index.html"]
+ s.extra_rdoc_files = ["lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "README"]
+ s.files = ["examples/aaws.rb", "examples/basic.rb", "examples/delicious.rb", "examples/google.rb", "examples/rubyurl.rb", "examples/twitter.rb", "examples/whoismyrep.rb", "History", "httparty.gemspec", "lib/core_extensions.rb", "lib/httparty/exceptions.rb", "lib/httparty/request.rb", "lib/httparty/version.rb", "lib/httparty.rb", "lib/module_level_inheritable_attributes.rb", "Manifest", "MIT-LICENSE", "Rakefile", "README", "setup.rb", "spec/fixtures/delicious.xml", "spec/fixtures/google.html", "spec/fixtures/twitter.json", "spec/fixtures/twitter.xml", "spec/httparty/request_spec.rb", "spec/httparty_spec.rb", "spec/spec.opts", "spec/spec_helper.rb", "website/css/common.css", "website/index.html"]
s.has_rdoc = true
s.homepage = %q{http://httparty.rubyforge.org}
s.post_install_message = %q{When you HTTParty, you must party hard!}
@@ -25,14 +25,14 @@ Gem::Specification.new do |s|
s.specification_version = 2
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
- s.add_runtime_dependency(%q<activesupport>, [">= 2.1"])
+ s.add_runtime_dependency(%q<json>, ["~> 1.1"])
s.add_development_dependency(%q<echoe>, [">= 0"])
else
- s.add_dependency(%q<activesupport>, [">= 2.1"])
+ s.add_dependency(%q<json>, ["~> 1.1"])
s.add_dependency(%q<echoe>, [">= 0"])
end
else
- s.add_dependency(%q<activesupport>, [">= 2.1"])
+ s.add_dependency(%q<json>, ["~> 1.1"])
s.add_dependency(%q<echoe>, [">= 0"])
end
end
View
48 lib/core_extensions.rb
@@ -289,4 +289,52 @@ class Hash
def self.from_xml(xml)
ToHashParser.from_xml(xml)
end
+
+ # @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") #=> "name=Bob&"
+ 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}=#{value}&"
+ 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
View
14 lib/httparty.rb
@@ -31,7 +31,7 @@ def http_proxy(addr=nil, port = nil)
def base_uri(uri=nil)
return default_options[:base_uri] unless uri
- default_options[:base_uri] = uri
+ default_options[:base_uri] = HTTParty.normalize_base_uri(uri)
end
def basic_auth(u, p)
@@ -73,10 +73,20 @@ def delete(path, options={})
private
def perform_request(http_method, path, options) #:nodoc:
- Request.new(http_method, path, default_options.merge(options)).perform
+ Request.new(http_method, path, default_options.dup.merge(options)).perform
end
end
+ def self.normalize_base_uri(url) #:nodoc:
+ use_ssl = (url =~ /^https/) || url.include?(':443')
+ ends_with_slash = url =~ /\/$/
+
+ url.chop! if ends_with_slash
+ url.gsub!(/^https?:\/\//i, '')
+
+ "http#{'s' if use_ssl}://#{url}"
+ end
+
class Basement
include HTTParty
end
View
26 lib/httparty/request.rb
@@ -1,29 +1,18 @@
require 'uri'
module HTTParty
- class Request
- # Makes it so uri is sure to parse stuff like google.com without the http
- def self.normalize_base_uri(url) #:nodoc:
- use_ssl = (url =~ /^https/) || url.include?(':443')
- ends_with_slash = url =~ /\/$/
-
- url.chop! if ends_with_slash
- url.gsub!(/^https?:\/\//i, '')
-
- "http#{'s' if use_ssl}://#{url}"
- end
-
+ class Request
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
attr_accessor :http_method, :path, :options
- def initialize(http_method, path, options={})
+ def initialize(http_method, path, o={})
self.http_method = http_method
self.path = path
self.options = {
- :limit => options.delete(:no_follow) ? 0 : 5,
+ :limit => o.delete(:no_follow) ? 0 : 5,
:default_params => {},
- }.merge(options.dup)
+ }.merge(o)
end
def path=(uri)
@@ -31,7 +20,6 @@ def path=(uri)
end
def uri
- options[:base_uri] = self.class.normalize_base_uri(options[:base_uri]) unless options[:base_uri].nil?
uri = path.relative? ? URI.parse("#{options[:base_uri]}#{path}") : path
uri.query = query_string(uri)
uri
@@ -61,7 +49,7 @@ def get_response(uri) #:nodoc:
request.set_form_data(options[:query])
end
- request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
+ request.body = options[:body].is_a?(Hash) ? options[:body].to_params : options[:body] unless options[:body].blank?
request.initialize_http_header options[:headers]
if options[:basic_auth]
@@ -78,9 +66,9 @@ def query_string(uri) #:nodoc:
query_string_parts << uri.query unless uri.query.blank?
if options[:query].is_a?(Hash)
- query_string_parts << options[:default_params].merge(options[:query]).to_query
+ query_string_parts << options[:default_params].merge(options[:query]).to_params
else
- query_string_parts << options[:default_params].to_query unless options[:default_params].blank?
+ query_string_parts << options[:default_params].to_params unless options[:default_params].blank?
query_string_parts << options[:query] unless options[:query].blank?
end
View
2  lib/httparty/version.rb
@@ -1,3 +1,3 @@
module HTTParty
- Version = '0.1.8'
+ Version = '0.2.0'
end
View
24 spec/httparty/request_spec.rb
@@ -5,35 +5,11 @@
@request = HTTParty::Request.new(Net::HTTP::Get, 'http://api.foo.com/v1', :format => :xml)
end
- describe "#normalize_base_uri" do
- it "should add http if not present for non ssl requests" do
- uri = HTTParty::Request.normalize_base_uri('api.foobar.com')
- uri.should == 'http://api.foobar.com'
- end
-
- it "should add https if not present for ssl requests" do
- uri = HTTParty::Request.normalize_base_uri('api.foo.com/v1:443')
- uri.should == 'https://api.foo.com/v1:443'
- end
-
- it "should not remove https for ssl requests" do
- uri = HTTParty::Request.normalize_base_uri('https://api.foo.com/v1:443')
- uri.should == 'https://api.foo.com/v1:443'
- end
- end
-
describe "#format" do
it "should return the correct parsing format" do
@request.format.should == :xml
end
end
-
- describe "uri" do
- it "should be normalized" do
- request = HTTParty::Request.new(Net::HTTP::Get, '', :base_uri => 'api.foo.com')
- request.uri.to_s.should == 'http://api.foo.com'
- end
- end
describe 'http' do
it "should use ssl for port 443" do
View
23 spec/httparty_spec.rb
@@ -25,7 +25,7 @@ class HRest
end
it "should have reader" do
- Foo.base_uri.should == 'api.foo.com/v1'
+ Foo.base_uri.should == 'http://api.foo.com/v1'
end
it 'should have writer' do
@@ -34,6 +34,23 @@ class HRest
end
end
+ describe "#normalize_base_uri" do
+ it "should add http if not present for non ssl requests" do
+ uri = HTTParty.normalize_base_uri('api.foobar.com')
+ uri.should == 'http://api.foobar.com'
+ end
+
+ it "should add https if not present for ssl requests" do
+ uri = HTTParty.normalize_base_uri('api.foo.com/v1:443')
+ uri.should == 'https://api.foo.com/v1:443'
+ end
+
+ it "should not remove https for ssl requests" do
+ uri = HTTParty.normalize_base_uri('https://api.foo.com/v1:443')
+ uri.should == 'https://api.foo.com/v1:443'
+ end
+ end
+
describe "headers" do
it "should default to empty hash" do
Foo.headers.should == {}
@@ -112,8 +129,8 @@ class HRest
describe "with multiple class definitions" do
it "should not run over each others options" do
- HRest.default_options.should == {:base_uri => 'hrest.com', :default_params => {:two => 'three'}}
- GRest.default_options.should == {:base_uri => 'grest.com', :default_params => {:one => 'two'}}
+ HRest.default_options.should == {:base_uri => 'http://hrest.com', :default_params => {:two => 'three'}}
+ GRest.default_options.should == {:base_uri => 'http://grest.com', :default_params => {:one => 'two'}}
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.