Class: Winnie::Commands::Auth
- Inherits:
-
Base
- Object
- Base
- Winnie::Commands::Auth
show all
- Defined in:
- lib/winnie/commands/auth.rb
Instance Attribute Summary
Attributes inherited from Base
#api_key, #args, #winnie
Instance Method Summary
collapse
Methods inherited from Base
#code_name, #config_path, #extract_options
Methods included from Helpers
#ask, #confirm, #display, #display_columns, #error, #line
Constructor Details
#initialize(*args) ⇒ Auth
Returns a new instance of Auth.
7
8
9
10
|
# File 'lib/winnie/commands/auth.rb', line 7
def initialize(*args)
super(args)
load_api_key if (api_key.nil? and api_key_exists?)
end
|
Instance Method Details
#api_key_exists? ⇒ Boolean
40
41
42
|
# File 'lib/winnie/commands/auth.rb', line 40
def api_key_exists?
File.exists?(api_key_path)
end
|
#api_key_path ⇒ Object
44
45
46
|
# File 'lib/winnie/commands/auth.rb', line 44
def api_key_path
File.join(config_path, 'api_key')
end
|
#ask_for_api_key ⇒ Object
19
20
21
22
23
|
# File 'lib/winnie/commands/auth.rb', line 19
def ask_for_api_key
display "Type your Winnie API key"
display "Key: ", false
@api_key = ask
end
|
#load_api_key ⇒ Object
31
32
33
|
# File 'lib/winnie/commands/auth.rb', line 31
def load_api_key
@api_key = File.read(api_key_path)
end
|
#run ⇒ Object
12
13
14
15
16
17
|
# File 'lib/winnie/commands/auth.rb', line 12
def run
confirm('Do you want to change your API key?') if api_key_exists?
ask_for_api_key
save_api_key
validate_api_key
end
|
#save_api_key ⇒ Object
25
26
27
28
29
|
# File 'lib/winnie/commands/auth.rb', line 25
def save_api_key
FileUtils.mkdir_p(config_path)
File.open(api_key_path, 'w') { |file| file << api_key }
set_credentials_permissions
end
|
#set_credentials_permissions ⇒ Object
48
49
50
51
|
# File 'lib/winnie/commands/auth.rb', line 48
def set_credentials_permissions
FileUtils.chmod(0700, config_path)
FileUtils.chmod(0600, api_key_path)
end
|
#validate_api_key ⇒ Object
35
36
37
38
|
# File 'lib/winnie/commands/auth.rb', line 35
def validate_api_key
winnie.account display 'Your winnie API key is OK!'
end
|