lib/gems/request.rb in gems-1.0.0 vs lib/gems/request.rb in gems-1.1.0
- old
+ new
@@ -21,11 +21,11 @@
end
private
def request(method, path, data, content_type, request_host = host) # rubocop:disable AbcSize, CyclomaticComplexity, MethodLength, ParameterLists, PerceivedComplexity
- path += hash_to_query_string(data) if [:delete, :get].include? method
+ path += hash_to_query_string(data) if %i[delete get].include? method
uri = URI.parse [request_host, path].join
request_class = Net::HTTP.const_get method.to_s.capitalize
request = request_class.new uri.request_uri
request.add_field 'Authorization', key if key
request.add_field 'Connection', 'keep-alive'
@@ -33,11 +33,11 @@
request.add_field 'User-Agent', user_agent
request.basic_auth username, password if username && password
request.content_type = content_type
case content_type
when 'application/x-www-form-urlencoded'
- request.form_data = data if [:post, :put].include? method
+ request.form_data = data if %i[post put].include? method
when 'application/octet-stream'
request.body = data
request.content_length = data.size
end
proxy = uri.find_proxy
@@ -57,12 +57,10 @@
end
def hash_to_query_string(hash)
return '' if hash.empty?
- hash.keys.each_with_object('?') do |key, query_string|
- query_string << "#{URI.encode(key.to_s)}=#{URI.encode(hash[key])}&"
- end.chop!
+ '?' + URI.encode_www_form(hash)
end
def body_from_response(response, method, content_type)
if response.is_a?(Net::HTTPRedirection)
uri = URI.parse(response['location'])