1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Made it so default_params actually get merged into request.

This commit is contained in:
John Nunemaker 2008-07-28 12:40:40 -04:00
parent 0c05dcda55
commit 236b3ad4e0
2 changed files with 30 additions and 12 deletions

View file

@ -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?

View file

@ -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'