Class: Credentials

Inherits:
Object
  • Object
show all
Defined in:
lib/credentials.rb

Overview

class to handle the credentials

usage:

c = Credentials.new c.username c.pa_token

Instance Method Summary collapse

Constructor Details

#initializeCredentials

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

#configObject



18
19
20
# File 'lib/credentials.rb', line 18

def config
  @config ||= parse_config
end

#ensure_config_file_is_createdObject



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_configObject



32
33
34
# File 'lib/credentials.rb', line 32

def overwrite_config
  File.write(@config_file, config.to_json)
end

#pa_tokenObject



27
28
29
30
# File 'lib/credentials.rb', line 27

def pa_token
  # PA_TOKEN
  config['paToken'] || prompt_pa_token
end

#prompt_pa_tokenObject



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_usernameObject



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_fileObject



61
62
63
# File 'lib/credentials.rb', line 61

def reset_config_file
  File.write(@config_file, '{}')
end

#usernameObject



22
23
24
25
# File 'lib/credentials.rb', line 22

def username
  # USERNAME
  config['username'] || prompt_username
end