Class: Gem::AbstractCommand

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

Instance Method Summary collapse

Instance Method Details

#api_keyObject



6
7
8
# File 'lib/commands/abstract_command.rb', line 6

def api_key
  Gem.configuration[:gemcutter_key]
end

#ask_for_password(message) ⇒ Object



88
89
90
91
92
# File 'lib/commands/abstract_command.rb', line 88

def ask_for_password(message)
  password = ui.ask_for_password(message)
  ui.say("\n")
  password
end

#gemcutter_urlObject



10
11
12
# File 'lib/commands/abstract_command.rb', line 10

def gemcutter_url
  ENV['GEMCUTTER_URL'] || 'https://gemcutter.heroku.com'
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



82
83
84
85
86
# File 'lib/commands/abstract_command.rb', line 82

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

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

Yields:

  • (request)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/commands/abstract_command.rb', line 40

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
    when :delete
      proxy_class::Delete
    else
      raise ArgumentError
    end

  request = request_method.new(url.path)
  yield request if block_given?
  http.request(request)
end

#proxy_classObject



77
78
79
# File 'lib/commands/abstract_command.rb', line 77

def proxy_class
  @proxy_class || Net::HTTP
end

#setupObject



14
15
16
17
# File 'lib/commands/abstract_command.rb', line 14

def setup
  use_proxy! if http_proxy
   unless api_key
end

#sign_inObject



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

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
    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

#use_proxy!Object



72
73
74
75
# File 'lib/commands/abstract_command.rb', line 72

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