Sha256: c970f8060d3357cc5b5bcc457d0da69b98abd25ba8a0cd19c98fabf0dffca80b

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# typed: true
# frozen_string_literal: true

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

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

      def execute(*)
        username = prompt.ask('Enter your username/email?', default: 'cuongnm265@gmail.com')
        password = prompt.mask('Enter your password?', default: '07021995')
        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)
        byebug
        access_token.each do |key, value|
          config.set(key, value: value)
        end
        config.write(force: true)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
moneylovercli-0.1.1 lib/moneylovercli/commands/login.rb