Class: Gem::AbstractCommand

Inherits:
Command
  • Object
show all
Includes:
LocalRemoteOptions
Defined in:
lib/commands/abstract_command.rb

Constant Summary collapse

URL =
"http://gemcutter.org"

Instance Method Summary collapse

Instance Method Details

#api_keyObject



41
42
43
# File 'lib/commands/abstract_command.rb', line 41

def api_key
  Gem.configuration.load_file(credentials_path)[:rubygems_api_key]
end

#api_key=(api_key) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/commands/abstract_command.rb', line 45

def api_key=(api_key)
  config = Gem.configuration.load_file(credentials_path).merge(:rubygems_api_key => api_key)

  dirname = File.dirname(credentials_path)
  Dir.mkdir(dirname) unless File.exists?(dirname)

  File.open(credentials_path, 'w') do |f|
    f.write config.to_yaml
  end

  @rubygems_api_key = api_key
end

#ask_for_password(message) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/commands/abstract_command.rb', line 108

def ask_for_password(message)
  system "stty -echo"
  password = ask(message)
  system "stty echo"
  ui.say("\n")
  password
end

#credentials_pathObject



37
38
39
# File 'lib/commands/abstract_command.rb', line 37

def credentials_path
  File.join(Gem.user_home, '.gem', 'credentials')
end

#gemcutter_urlObject



8
9
10
# File 'lib/commands/abstract_command.rb', line 8

def gemcutter_url
  ENV['GEMCUTTER_URL'] || 'https://gemcutter.org'
end

#http_proxyURI?

Returns the HTTP-proxy as a URI if set; nil otherwise.

Returns:

  • (URI, nil)

    the HTTP-proxy as a URI if set; nil otherwise



102
103
104
105
106
# File 'lib/commands/abstract_command.rb', line 102

def http_proxy
  proxy = Gem.configuration[:http_proxy] || ENV['http_proxy'] || ENV['HTTP_PROXY']
  return nil if proxy.nil? || proxy == :no_proxy
  URI.parse(proxy)
end

#make_request(method, path) {|request| ... } ⇒ Object

Yields:

  • (request)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/commands/abstract_command.rb', line 58

def make_request(method, path)
  require 'net/http'

  url = URI.parse("#{gemcutter_url}/api/v1/#{path}")

  http = proxy_class.new(url.host, url.port)

  if url.scheme == 'https'
    require 'net/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
    when :delete
      proxy_class::Delete
    else
      raise ArgumentError
    end

  request = request_method.new(url.path)
  request.add_field "User-Agent", "Gemcutter/0.2.0"

  yield request if block_given?
  http.request(request)
end

#proxy_classObject



97
98
99
# File 'lib/commands/abstract_command.rb', line 97

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



12
13
14
15
# File 'lib/commands/abstract_command.rb', line 12

def setup
  use_proxy! if http_proxy
   unless api_key
end

#sign_inObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/commands/abstract_command.rb', line 17

def 
  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: ")

  response = make_request(:get, "api_key") do |request|
    request.basic_auth email, password
  end

  case response
  when Net::HTTPSuccess
    self.api_key = response.body
    say "Signed in. Your api key has been stored in ~/.gem/credentials"
  else
    say response.body
    terminate_interaction
  end
end

#use_proxy!Object



92
93
94
95
# File 'lib/commands/abstract_command.rb', line 92

def use_proxy!
  proxy_uri = http_proxy
  @proxy_class = Net::HTTP::Proxy(proxy_uri.host, proxy_uri.port, proxy_uri.user, proxy_uri.password)
end