/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/gemcutter_utilities.rb

http://github.com/agross/netopenspace · Ruby · 82 lines · 74 code · 6 blank · 2 comment · 2 complexity · f69b82dbb265150cc15c6669a76bf49e MD5 · raw file

  1. require 'rubygems/remote_fetcher'
  2. module Gem::GemcutterUtilities
  3. OptionParser.accept Symbol do |value|
  4. value.to_sym
  5. end
  6. ##
  7. # Add the --key option
  8. def add_key_option
  9. add_option('-k', '--key KEYNAME', Symbol,
  10. 'Use the given API key',
  11. 'from ~/.gem/credentials') do |value,options|
  12. options[:key] = value
  13. end
  14. end
  15. def api_key
  16. if options[:key] then
  17. verify_api_key options[:key]
  18. else
  19. Gem.configuration.rubygems_api_key
  20. end
  21. end
  22. def sign_in
  23. return if Gem.configuration.rubygems_api_key
  24. say "Enter your RubyGems.org credentials."
  25. say "Don't have an account yet? Create one at http://rubygems.org/sign_up"
  26. email = ask " Email: "
  27. password = ask_for_password "Password: "
  28. say "\n"
  29. response = rubygems_api_request :get, "api/v1/api_key" do |request|
  30. request.basic_auth email, password
  31. end
  32. with_response response do |resp|
  33. say "Signed in."
  34. Gem.configuration.rubygems_api_key = resp.body
  35. end
  36. end
  37. def rubygems_api_request(method, path, host = Gem.host, &block)
  38. require 'net/http'
  39. host = ENV['RUBYGEMS_HOST'] if ENV['RUBYGEMS_HOST']
  40. uri = URI.parse "#{host}/#{path}"
  41. say "Pushing gem to #{host}..."
  42. request_method = Net::HTTP.const_get method.to_s.capitalize
  43. Gem::RemoteFetcher.fetcher.request(uri, request_method, &block)
  44. end
  45. def with_response(resp)
  46. case resp
  47. when Net::HTTPSuccess then
  48. if block_given? then
  49. yield resp
  50. else
  51. say resp.body
  52. end
  53. else
  54. say resp.body
  55. terminate_interaction 1
  56. end
  57. end
  58. def verify_api_key(key)
  59. if Gem.configuration.api_keys.key? key then
  60. Gem.configuration.api_keys[key]
  61. else
  62. alert_error "No such API key. You can add it with gem keys --add #{key}"
  63. terminate_interaction 1
  64. end
  65. end
  66. end