From 236b3ad4e0dc5a3f3414f4dd52f6ddbc565676a2 Mon Sep 17 00:00:00 2001 From: John Nunemaker Date: Mon, 28 Jul 2008 12:40:40 -0400 Subject: [PATCH] Made it so default_params actually get merged into request. --- lib/httparty.rb | 30 ++++++++++++++++++------------ spec/httparty_spec.rb | 12 ++++++++++++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/lib/httparty.rb b/lib/httparty.rb index 0fc8309..430bdd4 100644 --- a/lib/httparty.rb +++ b/lib/httparty.rb @@ -32,8 +32,8 @@ module HTTParty @auth = {:username => u, :password => p} end - def default_params(h) - raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash) + def default_params(h={}) + raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash) @default_params ||= {} return @default_params if h.blank? @default_params.merge!(h) @@ -46,6 +46,12 @@ module HTTParty @headers.merge!(h) end + def format(f) + f = f.to_s + raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f) + @format = f + end + def http if @http.blank? uri = URI.parse(base_uri) @@ -57,28 +63,26 @@ module HTTParty @http end + # TODO: spec out this def get(path, options={}) send_request 'get', path, options end - + + # TODO: spec out this def post(path, options={}) send_request 'post', path, options end - + + # TODO: spec out this def put(path, options={}) send_request 'put', path, options end - + + # TODO: spec out this def delete(path, options={}) send_request 'delete', path, options end - def format(f) - f = f.to_s - raise UnsupportedFormat, "Must be one of: #{AllowedFormats.join(', ')}" unless AllowedFormats.include?(f) - @format = f - end - private # options can be any or all of: # query => hash of keys/values to be converted to query string @@ -89,7 +93,9 @@ module HTTParty path = path.starts_with?('/') ? path : "/#{path}" @format = format_from_path(path) unless @format uri = URI.parse("#{base_uri}#{path}") - uri.query = options[:query].to_query unless options[:query].blank? + params = default_params + params.merge!(options[:query] || {}) + uri.query = params.to_query klass = Net::HTTP.const_get method.to_s.downcase.capitalize request = klass.new(uri.request_uri) request.body = options[:body] unless options[:body].blank? diff --git a/spec/httparty_spec.rb b/spec/httparty_spec.rb index 29a0796..fa756be 100644 --- a/spec/httparty_spec.rb +++ b/spec/httparty_spec.rb @@ -44,6 +44,18 @@ describe HTTParty do end end + describe "default params" do + it "should default to empty hash" do + Foo.default_params.should == {} + end + + it "should be able to be updated" do + new_defaults = {:foo => 'bar', :baz => 'spax'} + Foo.default_params new_defaults + Foo.default_params.should == new_defaults + end + end + describe "basic http authentication" do it "should work" do Foo.basic_auth 'foobar', 'secret'