class Gemsmith::Credentials

Generates gem credentials for RubyGems and/or alternative servers.

Constants

DEFAULT_KEY
DEFAULT_URL

Attributes

credentials[R]
key[R]
shell[R]
url[R]

Public Class Methods

authenticators() click to toggle source
# File lib/gemsmith/credentials.rb, line 22
def self.authenticators
  [Authenticators::RubyGems, Authenticators::Basic]
end
file_path() click to toggle source
# File lib/gemsmith/credentials.rb, line 18
def self.file_path
  Pathname(ENV.fetch("HOME")).join ".gem", "credentials"
end
new(key: DEFAULT_KEY, url: DEFAULT_URL, shell: Thor::Shell::Basic.new) click to toggle source
# File lib/gemsmith/credentials.rb, line 26
def initialize key: DEFAULT_KEY, url: DEFAULT_URL, shell: Thor::Shell::Basic.new
  @key = key
  @url = url
  @shell = shell
  @credentials = read
end

Public Instance Methods

authenticator() click to toggle source
# File lib/gemsmith/credentials.rb, line 33
def authenticator
  self.class
      .authenticators
      .find { |auth| auth.url.include? url }
      .then { |selected| selected || Authenticators::Basic }
end
create() click to toggle source
# File lib/gemsmith/credentials.rb, line 48
def create
  write unless valid?
end
valid?() click to toggle source
# File lib/gemsmith/credentials.rb, line 44
def valid?
  exist? && !String(credentials[key]).empty?
end
value() click to toggle source
# File lib/gemsmith/credentials.rb, line 40
def value
  String credentials[key]
end

Private Instance Methods

exist?() click to toggle source
# File lib/gemsmith/credentials.rb, line 56
def exist?
  self.class.file_path.exist?
end
read() click to toggle source
# File lib/gemsmith/credentials.rb, line 60
def read
  Hash YAML.load_file(self.class.file_path)
rescue StandardError
  {}
end
update() click to toggle source
# File lib/gemsmith/credentials.rb, line 74
def update
  login = shell.ask %(What is your "#{url}" login?)
  password = shell.ask %(What is your "#{url}" password?), echo: false
  shell.say

  credentials.merge key => authenticator.new(login, password).authorization
end
write() click to toggle source
# File lib/gemsmith/credentials.rb, line 66
def write
  self.class
      .file_path
      .tap { |path| path.parent.make_path }
      .write(YAML.dump(update))
      .chmod 0o600
end