Sha256: ece706ebfe17066a4c311b972d43bb00c88131e8afa0acc887e98703ef68e913

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'highline/import'

module Liri
  class Manager
    class Credential
      FILE_NAME = 'liri-credentials.yml'
      def initialize(folder_path)
        @folder_path = folder_path
        @file_path = File.join(@folder_path, '/', FILE_NAME)
      end

      # Obtiene ususario y contraseƱa del sistema en el que se ejecuta el programa
      def get
        user, password = get_credentials
        unless user || password
          user, password = ask_credentials
          save_credentials(user, password)
        end
        return user, password
      end

      private
      def get_local_user
        %x[whoami].delete!("\n")
      end

      def get_credentials
        if File.exist?(@file_path)
          data = YAML.load(File.read(@file_path))
          return data['user'], data['password']
        else
          return nil, nil
        end
      end

      def ask_credentials
        local_user = get_local_user
        password = ask("Ingrese contraseƱa del usuario #{local_user}: ") { |q| q.echo = "*" }
        return local_user, password
      end

      def save_credentials(user, password)
        File.write(@file_path, "user: #{user}\npassword: #{password}")
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
liri-0.3.0 lib/manager/credential.rb
liri-0.2.1 lib/manager/credential.rb
liri-0.2.0 lib/manager/credential.rb
liri-0.1.0 lib/manager/credential.rb