Class: Gemsmith::Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/gemsmith/credentials.rb

Overview

Generates gem credentials for RubyGems and/or alternative servers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: self.class.default_key, url: self.class.default_url, shell: Bundler::UI::Shell.new) ⇒ Credentials

Returns a new instance of Credentials.



27
28
29
30
31
32
# File 'lib/gemsmith/credentials.rb', line 27

def initialize key: self.class.default_key, url: self.class.default_url, shell: Bundler::UI::Shell.new
  @key = key
  @url = url
  @shell = shell
  @credentials = load_credentials
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



9
10
11
# File 'lib/gemsmith/credentials.rb', line 9

def key
  @key
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/gemsmith/credentials.rb', line 9

def url
  @url
end

Class Method Details

.authenticatorsObject



23
24
25
# File 'lib/gemsmith/credentials.rb', line 23

def self.authenticators
  [Authenticators::RubyGems, Authenticators::Basic]
end

.default_keyObject



11
12
13
# File 'lib/gemsmith/credentials.rb', line 11

def self.default_key
  :rubygems_api_key
end

.default_urlObject



15
16
17
# File 'lib/gemsmith/credentials.rb', line 15

def self.default_url
  "https://rubygems.org"
end

.file_pathObject



19
20
21
# File 'lib/gemsmith/credentials.rb', line 19

def self.file_path
  File.join ENV.fetch("HOME"), ".gem", "credentials"
end

Instance Method Details

#authenticatorObject



34
35
36
37
# File 'lib/gemsmith/credentials.rb', line 34

def authenticator
  selected = self.class.authenticators.detect { |auth| auth.url.include? url }
  selected ? selected : Authenticators::Basic
end

#createObject



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/gemsmith/credentials.rb', line 47

def create
  return if valid?

   = shell.ask %(What is your "#{url}" login?)
  password = shell.ask %(What is your "#{url}" password?)
  new_credentials = credentials.merge key => authenticator.new(, password).authorization

  FileUtils.mkdir_p File.dirname self.class.file_path
  File.open(self.class.file_path, "w") { |file| file << YAML.dump(new_credentials) }
  FileUtils.chmod(0o600, self.class.file_path)
end

#valid?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/gemsmith/credentials.rb', line 43

def valid?
  credentials? && !String(credentials[key]).empty?
end

#valueObject



39
40
41
# File 'lib/gemsmith/credentials.rb', line 39

def value
  String credentials[key]
end