Module: MercuryBanking::CLI::Accounts

Included in:
Main
Defined in:
lib/mercury_banking/cli/accounts.rb

Overview

Module for account-related commands

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Add account-related commands to the CLI class



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mercury_banking/cli/accounts.rb', line 8

def self.included(base)
  base.class_eval do
    desc 'accounts', 'Display all accounts'
    method_option :format, type: :string, default: 'table', enum: %w[table json],
                           desc: 'Output format (table or json)'
    def accounts
      with_api_client do |client|
        accounts = client.accounts

        if options[:json] || options[:format] == 'json'
          puts JSON.pretty_generate(accounts)
        else
          display_accounts_table(accounts)
        end

        puts "You have #{accounts.count} account/s" unless options[:json]
      end
    end

    desc 'balance ACCOUNT_ID_OR_NUMBER',
         "Display the current balance of an account (using account number from accounts table)"
    def balance()
      with_api_client do |client|
        # Determine if we're dealing with an account ID or account number
         = nil
        begin
           = client.()
           = ['id']
        rescue StandardError
          # If not found by number, assume it's an ID
           = 
        end

         = client.()

        balance = client.balance()

        if options[:json]
          puts JSON.pretty_generate({
                                      'account_id' => ,
                                      'account_number' => ['accountNumber'],
                                      'name' => ['name'],
                                      'balance' => balance
                                    })
        else
          puts "#{account['name']} (#{account['accountNumber']}): $#{format('%.2f', balance)}"
        end
      end
    end
  end
end