Class: Credentials
- Inherits:
-
Object
- Object
- Credentials
- Defined in:
- lib/credentials.rb
Overview
class to handle the credentials
usage:
c = Credentials.new c.username c.pa_token
Instance Method Summary collapse
- #config ⇒ Object
- #ensure_config_file_is_created ⇒ Object
-
#initialize ⇒ Credentials
constructor
A new instance of Credentials.
- #overwrite_config ⇒ Object
- #pa_token ⇒ Object
- #prompt_pa_token ⇒ Object
- #prompt_username ⇒ Object
- #reset_config_file ⇒ Object
- #username ⇒ Object
Constructor Details
#initialize ⇒ Credentials
Returns a new instance of Credentials.
13 14 15 16 |
# File 'lib/credentials.rb', line 13 def initialize @config_file = ENV['GITHUB_CREDENTIALS_FILE'] || "#{ENV['HOME']}/.cob/github_credentials.json" end |
Instance Method Details
#config ⇒ Object
18 19 20 |
# File 'lib/credentials.rb', line 18 def config @config ||= parse_config end |
#ensure_config_file_is_created ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/credentials.rb', line 65 def ensure_config_file_is_created return if File.exist?(@config_file) segments = @config_file.split('/') paths = segments[0..-2] file = segments[-1] dir = paths.join('/') FileUtils.mkdir_p(dir) FileUtils.touch(dir + '/' + file) reset_config_file end |
#overwrite_config ⇒ Object
32 33 34 |
# File 'lib/credentials.rb', line 32 def overwrite_config File.write(@config_file, config.to_json) end |
#pa_token ⇒ Object
27 28 29 30 |
# File 'lib/credentials.rb', line 27 def pa_token # PA_TOKEN config['paToken'] || prompt_pa_token end |
#prompt_pa_token ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/credentials.rb', line 49 def prompt_pa_token prompt = TTY::Prompt.new(active_color: :cyan) token = prompt.ask('What is your github personal access token? You can get one from here: https://github.com/settings/tokens') do |q| q.required true end config['paToken'] = token overwrite_config token end |
#prompt_username ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/credentials.rb', line 36 def prompt_username prompt = TTY::Prompt.new(active_color: :cyan) username = prompt.ask('What is your github username?') do |q| q.required true q.modify :lowercase end config['username'] = username overwrite_config username end |
#reset_config_file ⇒ Object
61 62 63 |
# File 'lib/credentials.rb', line 61 def reset_config_file File.write(@config_file, '{}') end |
#username ⇒ Object
22 23 24 25 |
# File 'lib/credentials.rb', line 22 def username # USERNAME config['username'] || prompt_username end |