Class: Locomotive::Wagon::AuthenticateCommand

Inherits:
Struct
  • Object
show all
Includes:
ApiConcern, NetrcConcern
Defined in:
lib/locomotive/wagon/commands/authenticate_command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NetrcConcern

#read_credentials_from_netrc, #write_credentials_to_netrc

Methods included from ApiConcern

#api_client, #api_credentials, #api_host, #api_site_client, #connection_information_from_env_and_path

Instance Attribute Details

#emailObject

Returns the value of attribute email

Returns:

  • (Object)

    the current value of email



6
7
8
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 6

def email
  @email
end

#passwordObject

Returns the value of attribute password

Returns:

  • (Object)

    the current value of password



6
7
8
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 6

def password
  @password
end

#platform_urlObject

Returns the value of attribute platform_url

Returns:

  • (Object)

    the current value of platform_url



6
7
8
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 6

def platform_url
  @platform_url
end

#shellObject

Returns the value of attribute shell

Returns:

  • (Object)

    the current value of shell



6
7
8
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 6

def shell
  @shell
end

Class Method Details

.authenticate(platform_url, email, password, shell) ⇒ Object



11
12
13
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 11

def self.authenticate(platform_url, email, password, shell)
  self.new(platform_url, email, password, shell).authenticate
end

Instance Method Details

#authenticateObject



15
16
17
18
19
20
21
22
23
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 15

def authenticate
  if api_key = fetch_api_key
    write_credentials_to_netrc(api_host, email, api_key)
  else
    shell.say "Sorry, we were unable to authenticate you on \"#{platform_url}\"", :red
  end

  !api_key.nil?
end

#create_accountObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 41

def 
  name = shell.ask 'What is your name?'

  begin
     = api_client..create(name: name, email: email, password: password)
    shell.say "Your account has been successfully created.", :green
    .api_key
  rescue Locomotive::Coal::Error => e
    shell.say "We were unable to create your account, reason(s): #{e.message}", :red
    false
  end
end

#fetch_api_keyObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/locomotive/wagon/commands/authenticate_command.rb', line 25

def fetch_api_key
  if 
    shell.say "\nYou have been successfully authenticated.", :green
    .api_key
  else
    shell.say "\nNo account found for #{email} or invalid credentials", :yellow

    # shall we create a new account?
    if shell.yes?('Do you want to create a new account? [Y/N]')
      
    else
      false
    end
  end
end