Sha256: ae81b36dc04e216e8c54d6aa562765e7121c38b68e86b2ee430aeb75e6b4c52e

Contents?: true

Size: 1.63 KB

Versions: 36

Compression:

Stored size: 1.63 KB

Contents

require_relative 'concerns/api_concern'
require_relative 'concerns/netrc_concern'

module Locomotive::Wagon

  class AuthenticateCommand < Struct.new(:platform_url, :email, :password, :shell)

    include ApiConcern
    include NetrcConcern

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

    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

    def fetch_api_key
      if my_account
        shell.say "\nYou have been successfully authenticated.", :green
        my_account.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]')
          create_account
        else
          false
        end
      end
    end

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

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

    private

    def my_account
      begin
        api_client.my_account.get
      rescue Locomotive::Coal::UnauthorizedError
        nil
      end
    end

  end

end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
locomotivecms_wagon-3.2.0.alpha2 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.2.0.alpha1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.1.1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.1.0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.1.0.beta1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.5 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.4 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.3 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.2 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.0.rc0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-3.0.0.beta2 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.4.1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.4.0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.4.0.rc2 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.4.0.rc1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.3.0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.3.0.rc1 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.2.0 lib/locomotive/wagon/commands/authenticate_command.rb
locomotivecms_wagon-2.2.0.rc3 lib/locomotive/wagon/commands/authenticate_command.rb