lib/commands/abstract_command.rb in gemcutter-0.0.7 vs lib/commands/abstract_command.rb in gemcutter-0.0.8

- old
+ new

@@ -1,6 +1,10 @@ +require 'rubygems/local_remote_options' + class Gem::AbstractCommand < Gem::Command + include Gem::LocalRemoteOptions + def api_key Gem.configuration[:gemcutter_key] end def gemcutter_url @@ -20,27 +24,52 @@ say "Enter your Gemcutter credentials. Don't have an account yet? Create one at #{URL}/sign_up" email = ask("Email: ") password = ask_for_password("Password: ") - url = URI.parse("#{gemcutter_url}/api_key") + response = make_request(:get, "api_key") do |request| + request.basic_auth email, password + end - http = Net::HTTP.new(url.host, url.port) - http.verify_mode = OpenSSL::SSL::VERIFY_NONE - http.use_ssl = (url.scheme == 'https') - request = Net::HTTP::Get.new(url.path) - request.basic_auth email, password - response = http.request(request) - case response when Net::HTTPSuccess Gem.configuration[:gemcutter_key] = response.body Gem.configuration.write say "Signed in. Your api key has been stored in ~/.gemrc" else say response.body terminate_interaction end + end + + def make_request(method, path) + require 'net/http' + require 'net/https' + + url = URI.parse("#{gemcutter_url}/#{path}") + + http = proxy_class.new(url.host, url.port) + + if url.scheme == 'https' + http.verify_mode = OpenSSL::SSL::VERIFY_NONE + http.use_ssl = true + end + + request_method = + case method + when :get + proxy_class::Get + when :post + proxy_class::Post + when :put + proxy_class::Put + else + raise ArgumentError + end + + request = request_method.new(url.path) + yield request if block_given? + http.request(request) end def use_proxy! proxy_uri = http_proxy @proxy_class = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)