Sha256: cc0c7cd62867cee41228a34ea0327295bcc11bb993cdf82091e555f2aed8e06c

Contents?: true

Size: 1.69 KB

Versions: 5

Compression:

Stored size: 1.69 KB

Contents

# typed: true
# frozen_string_literal: true

require_relative '../command'
require_relative '../api/access_token.rb'
require_relative '../api/user.rb'

module Moneylovercli
  module Commands
    class Login < Moneylovercli::Command
      def initialize(options)
        @options = options
      end

      def execute(*)
        username = prompt.ask('Enter your username/email?')
        password = prompt.mask('Enter your password?')
        status, access_token = parsed_access_token(username, password)

        if status
          prompt.ok('Login successfully')
          write_config(access_token)
        else
          prompt.error(access_token['message'])
        end
      end

      private

      sig { returns([String, String]) }
      def parsed_login_url
        login_url = Moneylovercli::Api::User.new.login_url.parsed_response
        client = login_url['data']['login_url'].match(/client=(.+?)&/)[1].to_s
        token = login_url['data']['request_token']

        [client, token]
      end

      sig { params(username: String, password: String).returns([T::Boolean, T::Hash[String, String]]) }
      def parsed_access_token(username, password)
        client, token = parsed_login_url
        request = Moneylovercli::Api::AccessToken.new.access_token(
          authorization_token: token.to_s,
          client: client, email: username, password: password
        )

        [request.parsed_response['status'], request.parsed_response]
      end

      sig { params(access_token: T::Hash[String, String]).void }
      def write_config(access_token)
        access_token.each do |key, value|
          config.set(key, value: value)
        end
        config.write(force: true)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
moneylovercli-0.1.7 lib/moneylovercli/commands/login.rb
moneylovercli-0.1.6 lib/moneylovercli/commands/login.rb
moneylovercli-0.1.4 lib/moneylovercli/commands/login.rb
moneylovercli-0.1.3 lib/moneylovercli/commands/login.rb
moneylovercli-0.1.2 lib/moneylovercli/commands/login.rb