Class: Fintoc::Account

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/fintoc/resources/account.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#flatten, #pick, #pluralize, #snake_to_pascal

Constructor Details

#initialize(id:, name:, official_name:, number:, holder_id:, holder_name:, type:, currency:, refreshed_at: nil, balance: nil, movements: nil, client: nil) ⇒ Account

Returns a new instance of Account.



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
# File 'lib/fintoc/resources/account.rb', line 11

def initialize(
  id:,
  name:,
  official_name:,
  number:,
  holder_id:,
  holder_name:,
  type:,
  currency:,
  refreshed_at: nil,
  balance: nil,
  movements: nil,
  client: nil,
  **
)
  @id = id
  @name = name
  @official_name = official_name
  @number = number
  @holder_id = holder_id
  @holder_name = holder_name
  @type = type
  @currency = currency
  @refreshed_at = DateTime.iso8601(refreshed_at) if refreshed_at
  @balance = Fintoc::Balance.new(**balance)
  @movements = movements || []
  @client = client
end

Instance Attribute Details

#balanceObject (readonly)

Returns the value of attribute balance.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def balance
  @balance
end

#currencyObject (readonly)

Returns the value of attribute currency.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def currency
  @currency
end

#holder_idObject (readonly)

Returns the value of attribute holder_id.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def holder_id
  @holder_id
end

#holder_nameObject (readonly)

Returns the value of attribute holder_name.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def holder_name
  @holder_name
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def id
  @id
end

#movementsObject (readonly)

Returns the value of attribute movements.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def movements
  @movements
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def number
  @number
end

#official_nameObject (readonly)

Returns the value of attribute official_name.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def official_name
  @official_name
end

#refreshed_atObject (readonly)

Returns the value of attribute refreshed_at.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def refreshed_at
  @refreshed_at
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/fintoc/resources/account.rb', line 9

def type
  @type
end

Instance Method Details

#get_movements(**params) ⇒ Object



44
45
46
# File 'lib/fintoc/resources/account.rb', line 44

def get_movements(**params)
  _get_movements(**params).lazy.map { |movement| Fintoc::Movement.new(**movement) }
end

#show_movements(rows = 5) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fintoc/resources/account.rb', line 53

def show_movements(rows = 5)
  puts("This account has #{Utils.pluralize(@movements.size, 'movement')}.")

  return unless @movements.any?

  movements = @movements.to_a.slice(0, rows)
                        .map.with_index do |mov, index|
                          [index + 1, mov.amount, mov.currency, mov.description, mov.locale_date]
                        end
  headers = ['#', 'Amount', 'Currency', 'Description', 'Date']
  puts
  puts tabulate(headers, movements, indent: 4, style: 'fancy')
end

#to_sObject



67
68
69
# File 'lib/fintoc/resources/account.rb', line 67

def to_s
  "💰 #{@holder_name}’s #{@name} #{@balance}"
end

#update_balanceObject



40
41
42
# File 'lib/fintoc/resources/account.rb', line 40

def update_balance
  @balance = Fintoc::Balance.new(**[:balance])
end

#update_movements(**params) ⇒ Object



48
49
50
51
# File 'lib/fintoc/resources/account.rb', line 48

def update_movements(**params)
  @movements += get_movements(**params).to_a
  @movements = @movements.uniq.sort_by(&:post_date)
end