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

Replacing string parameters with Net::HTTP classes

- send_request now uses the Net::HTTP classes directly
  rather than using const_get to grab them from 'get', etc.
This commit is contained in:
Rein Henrichs 2008-11-08 12:05:59 -05:00
parent ae864580ed
commit a2fd09256f
2 changed files with 13 additions and 13 deletions

View file

@ -17,6 +17,7 @@ module HTTParty
end
AllowedFormats = {:xml => 'text/xml', :json => 'application/json'}
SupportedHTTPMethods = [Net::HTTP::Get, Net::HTTP::Post, Net::HTTP::Put, Net::HTTP::Delete]
module ClassMethods
#
@ -66,22 +67,22 @@ module HTTParty
# TODO: spec out this
def get(path, options={})
send_request 'get', path, options
send_request Net::HTTP::Get, path, options
end
# TODO: spec out this
def post(path, options={})
send_request 'post', path, options
send_request Net::HTTP::Post, path, options
end
# TODO: spec out this
def put(path, options={})
send_request 'put', path, options
send_request Net::HTTP::Put, path, options
end
# TODO: spec out this
def delete(path, options={})
send_request 'delete', path, options
send_request Net::HTTP::Delete, path, options
end
private
@ -99,12 +100,12 @@ module HTTParty
# headers => hash of headers to send request with
# basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
# Raises exception Net::XXX (http error code) if an http error occured
def send_request(method, path, options={}) #:nodoc:
def send_request(klass, path, options={}) #:nodoc:
options = {:limit => 5}.merge(options)
options[:limit] = 0 if options.delete(:no_follow)
raise HTTParty::RedirectionTooDeep, 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
raise ArgumentError, 'only get, post, put and delete methods are supported' unless SupportedHTTPMethods.include?(klass)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
@ -117,7 +118,6 @@ module HTTParty
existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
end
klass = Net::HTTP.const_get method.to_s.downcase.capitalize
request = klass.new(uri.request_uri)
request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
basic_auth = options.delete(:basic_auth) || @auth
@ -131,7 +131,7 @@ module HTTParty
parse_response(response.body)
when Net::HTTPRedirection
options[:limit] -= 1
send_request(method, response['location'], options)
send_request(klass, response['location'], options)
else
response.instance_eval { class << self; attr_accessor :body_parsed; end }
begin; response.body_parsed = parse_response(response.body); rescue; end
@ -166,4 +166,4 @@ module HTTParty
AllowedFormats.each { |k, v| return k if mimetype.include?(v) }
end
end
end
end

View file

@ -139,10 +139,10 @@ describe HTTParty do
Foo.headers.clear # clear out bogus settings from other specs
Foo.format :xml
Foo.send(:send_request, 'get', '/bar').should be_nil
Foo.get('/bar').should be_nil
response.stub!(:body).and_return("")
Foo.send(:send_request, 'get', 'bar').should be_nil
Foo.get('bar').should be_nil
end
describe "that respond with redirects" do
@ -186,7 +186,7 @@ describe HTTParty do
http.stub!(:request).and_return(redirect)
lambda do
Foo.send(:send_request, 'get', '/foo')
Foo.get('/foo')
end.should raise_error(HTTParty::RedirectionTooDeep)
end
@ -218,4 +218,4 @@ describe HTTParty do
end
end
end
end
end