Class: Fintoc::V1::Account

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

Constant Summary collapse

HEADERS =
['#', 'Amount', 'Currency', 'Description', 'Date'].freeze

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.



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

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::V1::Balance.new(**balance)
  @movements = movements || []
  @client = client
end

Instance Attribute Details

#balanceObject (readonly)

Returns the value of attribute balance.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def balance
  @balance
end

#currencyObject (readonly)

Returns the value of attribute currency.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def currency
  @currency
end

#holder_idObject (readonly)

Returns the value of attribute holder_id.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def holder_id
  @holder_id
end

#holder_nameObject (readonly)

Returns the value of attribute holder_name.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def holder_name
  @holder_name
end

#idObject (readonly)

Returns the value of attribute id.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def id
  @id
end

#movementsObject (readonly)

Returns the value of attribute movements.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def movements
  @movements
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def name
  @name
end

#numberObject (readonly)

Returns the value of attribute number.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def number
  @number
end

#official_nameObject (readonly)

Returns the value of attribute official_name.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def official_name
  @official_name
end

#refreshed_atObject (readonly)

Returns the value of attribute refreshed_at.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def refreshed_at
  @refreshed_at
end

#typeObject (readonly)

Returns the value of attribute type.



11
12
13
# File 'lib/fintoc/v1/resources/account.rb', line 11

def type
  @type
end

Instance Method Details

#get_movements(**params) ⇒ Object



49
50
51
52
53
# File 'lib/fintoc/v1/resources/account.rb', line 49

def get_movements(**params)
  _get_movements(**params).lazy.map do
    |movement| Fintoc::V1::Movement.new(**movement, client: @client)
  end
end

#show_movements(rows = 5) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fintoc/v1/resources/account.rb', line 60

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

  puts
  puts tabulate(HEADERS, movements, indent: 4, style: 'fancy')
end

#to_sObject



77
78
79
# File 'lib/fintoc/v1/resources/account.rb', line 77

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

#update_balanceObject



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

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

#update_movements(**params) ⇒ Object



55
56
57
58
# File 'lib/fintoc/v1/resources/account.rb', line 55

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