Sha256: 5d7b56bb259128fb9f28ad88b9702b61da8372e7d8ddd2f257dd322697a82458

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

# frozen_string_literal: true

require 'uffizzi'
require 'uffizzi/response_helper'
require 'uffizzi/clients/api/api_client'

module Uffizzi
  class Cli::Login
    include ApiClient

    def initialize(options)
      @options = options
    end

    def run
      Uffizzi.ui.say('Login to Uffizzi to view and manage your previews.')
      server = set_server
      username = set_username
      password = set_password
      params = prepare_request_params(username, password)
      response = create_session(server, params)

      if ResponseHelper.created?(response)
        handle_succeed_response(response, server, username)
      else
        ResponseHelper.handle_failed_response(response)
      end
    end

    private

    def set_server
      config_server = ConfigFile.exists? && ConfigFile.option_has_value?(:server) ? ConfigFile.read_option(:server) : nil
      @options[:server] || config_server || Uffizzi.ui.ask('Server: ')
    end

    def set_username
      config_username = ConfigFile.exists? && ConfigFile.option_has_value?(:username) ? ConfigFile.read_option(:username) : nil
      @options[:username] || config_username || Uffizzi.ui.ask('Username: ')
    end

    def set_password
      ENV['UFFIZZI_PASSWORD'] || Uffizzi.ui.ask('Password: ', echo: false)
    end

    def prepare_request_params(username, password)
      {
        user: {
          email: username,
          password: password,
        },
      }
    end

    def handle_succeed_response(response, server, username)
      account = response[:body][:user][:accounts].first
      return Uffizzi.ui.say('No account related to this email') unless account_valid?(account)

      ConfigFile.write_option(:server, server)
      ConfigFile.write_option(:username, username)
      ConfigFile.write_option(:cookie, response[:headers])
      ConfigFile.write_option(:account_id, account[:id])
    end

    def account_valid?(account)
      account[:state] == 'active'
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
uffizzi-cli-0.9.0 lib/uffizzi/cli/login.rb
uffizzi-cli-0.8.0 lib/uffizzi/cli/login.rb
uffizzi-cli-0.7.3 lib/uffizzi/cli/login.rb
uffizzi-cli-0.7.2 lib/uffizzi/cli/login.rb