Class: Gemsmith::Credentials
- Inherits:
-
Object
- Object
- Gemsmith::Credentials
- Defined in:
- lib/gemsmith/credentials.rb
Overview
Generates gem credentials for RubyGems and/or alternative servers.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticator ⇒ Object
-
#create ⇒ Object
rubocop:disable Metrics/AbcSize :reek:TooManyStatements.
-
#initialize(key: self.class.default_key, url: self.class.default_url, shell: Thor::Shell::Basic.new) ⇒ Credentials
constructor
:reek:DuplicateMethodCall.
- #valid? ⇒ Boolean
- #value ⇒ Object
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
#key ⇒ Object (readonly)
Returns the value of attribute key.
11 12 13 |
# File 'lib/gemsmith/credentials.rb', line 11 def key @key end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/gemsmith/credentials.rb', line 11 def url @url end |
Class Method Details
.authenticators ⇒ Object
25 26 27 |
# File 'lib/gemsmith/credentials.rb', line 25 def self.authenticators [Authenticators::RubyGems, Authenticators::Basic] end |
.default_key ⇒ Object
13 14 15 |
# File 'lib/gemsmith/credentials.rb', line 13 def self.default_key :rubygems_api_key end |
.default_url ⇒ Object
17 18 19 |
# File 'lib/gemsmith/credentials.rb', line 17 def self.default_url "https://rubygems.org" end |
.file_path ⇒ Object
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
#authenticator ⇒ Object
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 |
#create ⇒ Object
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? login = 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(login, password). 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
48 49 50 |
# File 'lib/gemsmith/credentials.rb', line 48 def valid? credentials? && !String(credentials[key]).empty? end |
#value ⇒ Object
44 45 46 |
# File 'lib/gemsmith/credentials.rb', line 44 def value String credentials[key] end |