Class: FaaStRuby::NewCredentials::CredentialsFile
- Inherits:
-
Object
- Object
- FaaStRuby::NewCredentials::CredentialsFile
- Defined in:
- lib/faastruby/cli/new_credentials.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #create_credentials_folder ⇒ Object
- #get ⇒ Object
- #has_user_logged_in? ⇒ Boolean
-
#initialize ⇒ CredentialsFile
constructor
A new instance of CredentialsFile.
- #read ⇒ Object
- #rename_if_old_file_exists ⇒ Object
- #save(email:, api_key:, api_secret:) ⇒ Object
Constructor Details
#initialize ⇒ CredentialsFile
Returns a new instance of CredentialsFile.
6 7 8 9 10 11 |
# File 'lib/faastruby/cli/new_credentials.rb', line 6 def initialize @folder = File.("~/.faastruby") @file = "#{@folder}/credentials.yml" rename_if_old_file_exists create_credentials_folder end |
Instance Method Details
#clear ⇒ Object
55 56 57 58 59 60 |
# File 'lib/faastruby/cli/new_credentials.rb', line 55 def clear yaml = { 'credentials' => nil }.to_yaml File.write(@file, yaml) end |
#create_credentials_folder ⇒ Object
21 22 23 24 25 |
# File 'lib/faastruby/cli/new_credentials.rb', line 21 def create_credentials_folder return true if File.file?(@file) FileUtils.mkdir_p(@folder) clear end |
#get ⇒ Object
31 32 33 34 35 36 37 |
# File 'lib/faastruby/cli/new_credentials.rb', line 31 def get creds = read['credentials'] || {} unless creds['email'] && creds['api_key'] && creds['api_secret'] FaaStRuby::CLI.error("\nYou are not logged in. To login, run: faastruby login\n\nIf you don't have an account, run 'faastruby signup' to create one.\n", color: nil) end creds end |
#has_user_logged_in? ⇒ Boolean
39 40 41 42 |
# File 'lib/faastruby/cli/new_credentials.rb', line 39 def has_user_logged_in? creds = read['credentials'] || {} creds['email'] && creds['api_key'] && creds['api_secret'] end |
#read ⇒ Object
27 28 29 |
# File 'lib/faastruby/cli/new_credentials.rb', line 27 def read YAML.load(File.read(@file)) rescue {} end |
#rename_if_old_file_exists ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/faastruby/cli/new_credentials.rb', line 13 def rename_if_old_file_exists old_file = File.("~/.faastruby") return true unless File.file?(old_file) new_file = File.("~/.faastruby.tor1") FileUtils.mv(old_file, new_file) return true end |
#save(email:, api_key:, api_secret:) ⇒ Object
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/faastruby/cli/new_credentials.rb', line 44 def save(email:, api_key:, api_secret:) yaml = { 'credentials' => { 'email' => email, 'api_key' => api_key, 'api_secret' => api_secret } }.to_yaml File.write(@file, yaml) end |