PageRenderTime 19ms CodeModel.GetById 12ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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