Class: Winnie::Commands::Auth

Inherits:
Base
  • Object
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

Returns:

  • (Boolean)


40
41
42
# File 'lib/winnie/commands/auth.rb', line 40

def api_key_exists?
  File.exists?(api_key_path)
end

#api_key_pathObject



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_keyObject



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_keyObject



31
32
33
# File 'lib/winnie/commands/auth.rb', line 31

def load_api_key
  @api_key = File.read(api_key_path)
end

#runObject



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_keyObject



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_permissionsObject



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_keyObject



35
36
37
38
# File 'lib/winnie/commands/auth.rb', line 35

def validate_api_key
  winnie. # If it's not correct, Winnie::Client::UnauthorizedException will be raised
  display 'Your winnie API key is OK!'
end