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: Thor::Shell::Basic.new) ⇒ Credentials

:reek:DuplicateMethodCall



30
31
32
33
34
35
36
37
# File 'lib/gemsmith/credentials.rb', line 30

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

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



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

def key
  @key
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Class Method Details

.authenticatorsObject



25
26
27
# File 'lib/gemsmith/credentials.rb', line 25

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

.default_keyObject



13
14
15
# File 'lib/gemsmith/credentials.rb', line 13

def self.default_key
  :rubygems_api_key
end

.default_urlObject



17
18
19
# File 'lib/gemsmith/credentials.rb', line 17

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

.file_pathObject



21
22
23
# File 'lib/gemsmith/credentials.rb', line 21

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

Instance Method Details

#authenticatorObject



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

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

#createObject

rubocop:disable Metrics/AbcSize :reek:TooManyStatements



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gemsmith/credentials.rb', line 54

def create
  return if valid?

   = shell.ask %(What is your "#{url}" login?)
  password = shell.ask %(What is your "#{url}" password?), echo: false
  shell.say

  new_credentials = credentials.merge key => authenticator.new(, password).authorization

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

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gemsmith/credentials.rb', line 48

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

#valueObject



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

def value
  String credentials[key]
end