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
-
#initialize(key: self.class.default_key, url: self.class.default_url, shell: Bundler::UI::Shell.new) ⇒ Credentials
constructor
A new instance of Credentials.
- #valid? ⇒ Boolean
- #value ⇒ Object
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 33 34 |
# 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
#key ⇒ Object (readonly)
Returns the value of attribute key.
9 10 11 |
# File 'lib/gemsmith/credentials.rb', line 9 def key @key end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
9 10 11 |
# File 'lib/gemsmith/credentials.rb', line 9 def url @url end |
Class Method Details
.authenticators ⇒ Object
23 24 25 |
# File 'lib/gemsmith/credentials.rb', line 23 def self.authenticators [Authenticators::RubyGems, Authenticators::Basic] end |
.default_key ⇒ Object
11 12 13 |
# File 'lib/gemsmith/credentials.rb', line 11 def self.default_key :rubygems_api_key end |
.default_url ⇒ Object
15 16 17 |
# File 'lib/gemsmith/credentials.rb', line 15 def self.default_url "https://rubygems.org" end |
.file_path ⇒ Object
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
#authenticator ⇒ Object
36 37 38 39 |
# File 'lib/gemsmith/credentials.rb', line 36 def authenticator selected = self.class.authenticators.detect { |auth| auth.url.include? url } selected ? selected : Authenticators::Basic end |
#create ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/gemsmith/credentials.rb', line 49 def create return if valid? login = shell.ask %(What is your "#{url}" login?) password = shell.ask %(What is your "#{url}" password?) new_credentials = credentials.merge key => authenticator.new(login, password). 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
45 46 47 |
# File 'lib/gemsmith/credentials.rb', line 45 def valid? credentials? && !String(credentials[key]).empty? end |
#value ⇒ Object
41 42 43 |
# File 'lib/gemsmith/credentials.rb', line 41 def value String credentials[key] end |