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

Merge pull request #314 from khelll/master

More flixable type system
This commit is contained in:
John Nunemaker 2014-08-05 09:32:19 -04:00
commit 274060c1ca
3 changed files with 27 additions and 27 deletions

View file

@ -41,13 +41,13 @@ module HTTParty
# == Common Request Options
# Request methods (get, post, patch, put, delete, head, options) all take a common set of options. These are:
#
# [:+body+:] Body of the request. If passed a Hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
# [:+body+:] Body of the request. If passed an object that responds to #to_hash, will try to normalize it first, by default passing it to ActiveSupport::to_params. Any other kind of object will get used as-is.
# [:+http_proxyaddr+:] Address of proxy server to use.
# [:+http_proxyport+:] Port of proxy server to use.
# [:+http_proxyuser+:] User for proxy server authentication.
# [:+http_proxypass+:] Password for proxy server authentication.
# [:+limit+:] Maximum number of redirects to follow. Takes precedences over :+no_follow+.
# [:+query+:] Query string, or a Hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use a Hash. See also HTTParty::ClassMethods.default_params.
# [:+query+:] Query string, or an object that responds to #to_hash representing it. Normalized according to the same rules as :+body+. If you specify this on a POST, you must use an object which responds to #to_hash. See also HTTParty::ClassMethods.default_params.
# [:+timeout+:] Timeout for opening connection and reading data.
# [:+local_host:] Local address to bind to before connecting.
# [:+local_port:] Local port to bind to before connecting.
@ -58,7 +58,7 @@ module HTTParty
# * :+debug_output+: see HTTParty::ClassMethods.debug_output.
# * :+digest_auth+: see HTTParty::ClassMethods.digest_auth. Only one of :+basic_auth+ and :+digest_auth+ can be used at a time; if you try using both, you'll get an ArgumentError.
# * :+format+: see HTTParty::ClassMethods.format.
# * :+headers+: see HTTParty::ClassMethods.headers. Must be a Hash.
# * :+headers+: see HTTParty::ClassMethods.headers. Must be a an object which responds to #to_hash.
# * :+maintain_method_across_redirects+: see HTTParty::ClassMethods.maintain_method_across_redirects.
# * :+no_follow+: see HTTParty::ClassMethods.no_follow.
# * :+parser+: see HTTParty::ClassMethods.parser.
@ -159,7 +159,7 @@ module HTTParty
# default_params api_key: 'secret', another: 'foo'
# end
def default_params(h={})
raise ArgumentError, 'Default params must be a hash' unless h.is_a?(Hash)
raise ArgumentError, 'Default params must an object which respond to #to_hash' unless h.respond_to?(:to_hash)
default_options[:default_params] ||= {}
default_options[:default_params].merge!(h)
end
@ -216,13 +216,13 @@ module HTTParty
# headers 'Accept' => 'text/html'
# end
def headers(h={})
raise ArgumentError, 'Headers must be a hash' unless h.is_a?(Hash)
raise ArgumentError, 'Headers must an object which responds to #to_hash' unless h.respond_to?(:to_hash)
default_options[:headers] ||= {}
default_options[:headers].merge!(h)
default_options[:headers].merge!(h.to_hash)
end
def cookies(h={})
raise ArgumentError, 'Cookies must be a hash' unless h.is_a?(Hash)
raise ArgumentError, 'Cookies must an object which respond to #to_hash' unless h.respond_to?(:to_hash)
default_cookies.add_cookies(h)
end

View file

@ -12,7 +12,7 @@ module HTTParty
# }.to_params
# #=> "name=Bob&address[city]=Ruby Central&address[phones][]=111-111-1111&address[phones][]=222-222-2222&address[street]=111 Ruby Ave."
def self.to_params(hash)
params = hash.map { |k,v| normalize_param(k,v) }.join
params = hash.to_hash.map { |k,v| normalize_param(k,v) }.join
params.chop! # trailing &
params
end
@ -27,18 +27,18 @@ module HTTParty
param = ''
stack = []
if value.is_a?(Array)
param << value.map { |element| normalize_param("#{key}[]", element) }.join
elsif value.is_a?(Hash)
stack << [key,value]
if value.respond_to?(:to_ary)
param << value.to_ary.map { |element| normalize_param("#{key}[]", element) }.join
elsif value.respond_to?(:to_hash)
stack << [key,value.to_hash]
else
param << "#{key}=#{URI.encode(value.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}&"
end
stack.each do |parent, hash|
hash.each do |k, v|
if v.is_a?(Hash)
stack << ["#{parent}[#{k}]", v]
if v.respond_to?(:to_hash)
stack << ["#{parent}[#{k}]", v.to_hash]
else
param << normalize_param("#{parent}[#{k}]", v)
end

View file

@ -18,8 +18,8 @@ module HTTParty
Array(query).sort_by { |a| a[0].to_s }.map do |key, value|
if value.nil?
key.to_s
elsif value.is_a?(Array)
value.map {|v| "#{key}=#{URI.encode(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}
elsif value.respond_to?(:to_ary)
value.to_ary.map {|v| "#{key}=#{URI.encode(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"}
else
HashConversions.to_params(key => value)
end
@ -118,11 +118,11 @@ module HTTParty
end
def body
options[:body].is_a?(Hash) ? normalize_query(options[:body]) : options[:body]
options[:body].respond_to?(:to_hash) ? normalize_query(options[:body]) : options[:body]
end
def credentials
options[:basic_auth] || options[:digest_auth]
(options[:basic_auth] || options[:digest_auth]).to_hash
end
def username
@ -149,14 +149,14 @@ module HTTParty
@raw_request = http_method.new(request_uri(uri))
@raw_request.body = body if body
@raw_request.body_stream = options[:body_stream] if options[:body_stream]
@raw_request.initialize_http_header(options[:headers])
@raw_request.initialize_http_header(options[:headers].to_hash) if options[:headers].respond_to?(:to_hash)
@raw_request.basic_auth(username, password) if options[:basic_auth]
setup_digest_auth if options[:digest_auth]
end
def setup_digest_auth
auth_request = http_method.new(uri.request_uri)
auth_request.initialize_http_header(options[:headers])
auth_request.initialize_http_header(options[:headers].to_hash) if options[:headers].respond_to?(:to_hash)
res = http.request(auth_request)
if res['www-authenticate'] != nil && res['www-authenticate'].length > 0
@ -168,8 +168,8 @@ module HTTParty
query_string_parts = []
query_string_parts << uri.query unless uri.query.nil?
if options[:query].is_a?(Hash)
query_string_parts << normalize_query(options[:default_params].merge(options[:query]))
if options[:query].respond_to?(:to_hash)
query_string_parts << normalize_query(options[:default_params].merge(options[:query].to_hash))
else
query_string_parts << normalize_query(options[:default_params]) unless options[:default_params].empty?
query_string_parts << options[:query] unless options[:query].nil?
@ -307,7 +307,7 @@ module HTTParty
def capture_cookies(response)
return unless response['Set-Cookie']
cookies_hash = HTTParty::CookieHash.new()
cookies_hash.add_cookies(options[:headers]['Cookie']) if options[:headers] && options[:headers]['Cookie']
cookies_hash.add_cookies(options[:headers].to_hash['Cookie']) if options[:headers] && options[:headers].to_hash['Cookie']
response.get_fields('Set-Cookie').each { |cookie| cookies_hash.add_cookies(cookie) }
options[:headers] ||= {}
options[:headers]['Cookie'] = cookies_hash.to_cookie_string
@ -325,11 +325,11 @@ module HTTParty
def validate
raise HTTParty::RedirectionTooDeep.new(last_response), 'HTTP redirects too deep' if options[:limit].to_i <= 0
raise ArgumentError, 'only get, post, patch, put, delete, head, and options methods are supported' unless SupportedHTTPMethods.include?(http_method)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].is_a?(Hash)
raise ArgumentError, ':headers must be a hash' if options[:headers] && !options[:headers].respond_to?(:to_hash)
raise ArgumentError, 'only one authentication method, :basic_auth or :digest_auth may be used at a time' if options[:basic_auth] && options[:digest_auth]
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].is_a?(Hash)
raise ArgumentError, ':digest_auth must be a hash' if options[:digest_auth] && !options[:digest_auth].is_a?(Hash)
raise ArgumentError, ':query must be hash if using HTTP Post' if post? && !options[:query].nil? && !options[:query].is_a?(Hash)
raise ArgumentError, ':basic_auth must be a hash' if options[:basic_auth] && !options[:basic_auth].respond_to?(:to_hash)
raise ArgumentError, ':digest_auth must be a hash' if options[:digest_auth] && !options[:digest_auth].respond_to?(:to_hash)
raise ArgumentError, ':query must be hash if using HTTP Post' if post? && !options[:query].nil? && !options[:query].respond_to?(:to_hash)
end
def post?