0
@@ -90,29 +90,32 @@ module HTTParty
0
# FIXME: this method is doing way to much and needs to be split up
0
# options can be any or all of:
0
- # query => hash of keys/values to be converted to query string
0
- # body => string for raw post data
0
+ # query => hash of keys/values or a query string (foo=bar&baz=poo)
0
+ # body => hash of keys/values or a query string (foo=bar&baz=poo)
0
# headers => hash of headers to send request with
0
# basic_auth => :username and :password to use as basic http authentication (overrides @auth class instance variable)
0
def send_request(method, path, options={})
0
raise ArgumentError, 'only get, post, put and delete methods are supported' unless %w[get post put delete].include?(method.to_s)
0
- raise ArgumentError, ':query must be a hash' if options[:query] && !options[:query].is_a?(Hash)
0
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
0
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
0
# we always want path that begins with /
0
- path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
- @format ||= format_from_path(path)
0
- uri = URI.parse("#{base_uri}#{path}")
0
- current_qs = uri.query ? "#{uri.query}&" : ''
0
- uri.query = current_qs + default_params.merge(options[:query] || {}).to_query
0
- klass = Net::HTTP.const_get method.to_s.downcase.capitalize
0
- request = klass.new(uri.request_uri)
0
- request.body = options[:body] unless options[:body].blank?
0
- basic_auth = options.delete(:basic_auth) || @auth
0
+ path = path =~ /^(\/|https?:\/\/)/ ? path : "/#{path}"
0
+ @format ||= format_from_path(path)
0
+ uri = URI.parse("#{base_uri}#{path}")
0
+ existing_query = uri.query ? "#{uri.query}&" : ''
0
+ uri.query = if options[:query].blank?
0
+ existing_query + (options[:query].is_a?(Hash) ? default_params.merge(options[:query]).to_query : options[:query])
0
+ klass = Net::HTTP.const_get method.to_s.downcase.capitalize
0
+ request = klass.new(uri.request_uri)
0
+ request.body = options[:body].is_a?(Hash) ? options[:body].to_query : options[:body] unless options[:body].blank?
0
+ basic_auth = options.delete(:basic_auth) || @auth
0
request.initialize_http_header headers.merge(options[:headers] || {})
0
# note to self: self, do not put basic auth above headers because it removes basic auth
0
request.basic_auth(basic_auth[:username], basic_auth[:password]) if basic_auth
0
- response
= http(uri).request(request)
0
+ response
= http(uri).request(request)
0
parse_response(response.body)