Class: Fintoc::Link

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#flatten, #pick, #pluralize, #snake_to_pascal

Constructor Details

#initialize(id:, username:, holder_type:, institution:, created_at:, mode:, accounts: nil, link_token: nil, client: nil) ⇒ Link

Returns a new instance of Link.



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/link.rb', line 13

def initialize(
  id:,
  username:,
  holder_type:,
  institution:,
  created_at:,
  mode:,
  accounts: nil,
  link_token: nil,
  client: nil,
  **
)
  @id = id
  @username = username
  @holder_type = holder_type
  @institution = Fintoc::Institution.new(**institution)
  @created_at = Date.iso8601(created_at)
  @mode = mode
  @accounts = if accounts.nil?
                []
              else
                accounts.map { |data| Fintoc::Account.new(**data, client: client) }
              end
  @token = link_token
  @client = client
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def accounts
  @accounts
end

#created_atObject (readonly)

Returns the value of attribute created_at.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def created_at
  @created_at
end

#holder_typeObject (readonly)

Returns the value of attribute holder_type.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def holder_type
  @holder_type
end

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def id
  @id
end

#institutionObject (readonly)

Returns the value of attribute institution.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def institution
  @institution
end

Returns the value of attribute link_token.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def link_token
  @link_token
end

#modeObject (readonly)

Returns the value of attribute mode.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def mode
  @mode
end

#usernameObject (readonly)

Returns the value of attribute username.



10
11
12
# File 'lib/fintoc/resources/link.rb', line 10

def username
  @username
end

Instance Method Details

#deleteObject



75
76
77
# File 'lib/fintoc/resources/link.rb', line 75

def delete
  @client.delete_link(@id)
end

#find(**kwargs) ⇒ Object



49
50
51
52
# File 'lib/fintoc/resources/link.rb', line 49

def find(**kwargs)
  results = find_all(**kwargs)
  results.any? ? results.first : nil
end

#find_all(**kwargs) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/fintoc/resources/link.rb', line 40

def find_all(**kwargs)
  raise 'You must provide *exactly one* account field.' if kwargs.size != 1

  field, value = kwargs.to_a.first
  @accounts.select do ||
    .send(field.to_sym) == value
  end
end

#show_accounts(rows = 5) ⇒ Object



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

def show_accounts(rows = 5)
  puts "This links has #{Utils.plurlize(@accounts.size, 'account')}"

  return unless @accounts.any?

  accounts = @accounts.to_a.slice(0, rows)
                      .map.with_index do |acc, index|
                        [index + 1, acc.name, acc.holder_name, .currency]
                      end
  headers = ['#', 'Name', 'Holder', 'Currency']
  puts
  puts tabulate(headers, accounts, indent: 4, style: 'fancy')
end

#to_sObject



79
80
81
# File 'lib/fintoc/resources/link.rb', line 79

def to_s
  "<#{@username}@#{@institution.name}> 🔗 <Fintoc>"
end

#update_accountsObject



68
69
70
71
72
73
# File 'lib/fintoc/resources/link.rb', line 68

def update_accounts
  @accounts.each do ||
    .update_balance
    .update_movements
  end
end