Class: Plaid::Account

Inherits:
Object
  • Object
show all
Defined in:
lib/plaid/account.rb

Overview

Public: Representation of user account data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ Account

Returns a new instance of Account.



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/plaid/account.rb', line 56

def initialize(hash)
  @id          = hash['_id']
  @item_id     = hash['_item']
  @meta        = hash['meta']
  @type        = hash['type'].to_sym
  @subtype     = hash['subtype']
  @institution = hash['institution_type'].to_sym

  unless (bal = hash['balance']).nil?
    @available_balance = bal['available']
    @current_balance   = bal['current']
  end

  if (risk = hash['risk'])
    @risk = Plaid::Risk.new(risk)
  end

  @numbers = Plaid.symbolize_hash(hash['numbers'])
end

Instance Attribute Details

#available_balanceObject (readonly)

Public: The Float value of the available balance for the account.

The Available Balance is the Current Balance less any outstanding holds or debits that have not yet posted to the account. Note that not all institutions calculate the Available Balance. In the case that Available Balance is unavailable from the institution, Plaid will either return an Available Balance value of null or only return a Current Balance.



24
25
26
# File 'lib/plaid/account.rb', line 24

def available_balance
  @available_balance
end

#current_balanceObject (readonly)

Public: The Float value of the current balance for the account.



15
16
17
# File 'lib/plaid/account.rb', line 15

def current_balance
  @current_balance
end

#idObject (readonly)

Public: The String unique ID of the account. E.g. “QPO8Jo8vdDHMepg41PBwckXm4KdK1yUdmXOwK”.



8
9
10
# File 'lib/plaid/account.rb', line 8

def id
  @id
end

#institutionObject (readonly)

Public: The Symbol institution type, e.g. :wells.



27
28
29
# File 'lib/plaid/account.rb', line 27

def institution
  @institution
end

#item_idObject (readonly)

Public: The String account ID unique to the accounts of a particular access token. E.g. “KdDjmojBERUKx3JkDd9RuxA5EvejA4SENO4AA”.



12
13
14
# File 'lib/plaid/account.rb', line 12

def item_id
  @item_id
end

#metaObject (readonly)

Public: The Hash with additional information pertaining to the account such as the limit, name, or last few digits of the account number. E.g. {“name”: “Plaid Savings”, “number”: “9606” }.



32
33
34
# File 'lib/plaid/account.rb', line 32

def meta
  @meta
end

#numbersObject (readonly)

Public: The Hash with account and routing numbers for the account.

This attribute would be nil unless you used Auth product for the user.

The Hash contains Symbol keys and String values. E.g. “021000021”, account: “9900009606”, wireRouting: “021000021”.



49
50
51
# File 'lib/plaid/account.rb', line 49

def numbers
  @numbers
end

#riskObject (readonly)

Public: The Risk information associated with the account.

This attribute would be nil unless you used Risk product for the user.



54
55
56
# File 'lib/plaid/account.rb', line 54

def risk
  @risk
end

#subtypeObject (readonly)

Public: The String account subtype. E.g. “savings”.

Read more about subtypes in the Plaid API docs.



41
42
43
# File 'lib/plaid/account.rb', line 41

def subtype
  @subtype
end

#typeObject (readonly)

Public: The Symbol account type. One of :depository, :credit, :loan, :mortgage, :brokerage, and :other.



36
37
38
# File 'lib/plaid/account.rb', line 36

def type
  @type
end

Class Method Details

.merge(accounts, new_accounts) ⇒ Object

Internal: Merge account information.

accounts - The Array of Account instances. new_accounts - The Array of Account instances.

Returns accounts.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/plaid/account.rb', line 104

def self.merge(accounts, new_accounts)
  # Index accounts by their ID.
  #
  # Same as index = accounts.index_by(&:id) in ActiveSupport.
  index = Hash[accounts.map { |a| [a.id, a] }]

  new_accounts.each do |acc|
    if (old_acc = index[acc.id])
      old_acc.update_from(acc)
    else
      accounts << acc
    end
  end

  accounts
end

Instance Method Details

#inspectObject Also known as: to_s

Public: Get a String representation of the account.

Returns a String.



79
80
81
82
# File 'lib/plaid/account.rb', line 79

def inspect
  "#<Plaid::Account id=#{id.inspect}, type=#{type.inspect}, " \
  "name=#{name.inspect}, institution=#{institution.inspect}>"
end

#nameObject

Public: Get the account name.

The name is obtained from #meta Hash.

Returns the String name.



94
95
96
# File 'lib/plaid/account.rb', line 94

def name
  meta && meta['name']
end

#update_from(another) ⇒ Object

Internal: Update account information.

All fields which are not nil in another are copied to self.

another - The Account instance with new information.

Returns self.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/plaid/account.rb', line 128

def update_from(another)
  # A sanity check. Nobody would want to update information from totally
  # different account!
  if id != another.id
    raise ArgumentError, 'Plaid::Account#update_from: id != another.id!'
  end

  %i(item_id meta name type subtype institution available_balance
     current_balance numbers risk).each do |field|
    value = another.send(field)
    instance_variable_set("@#{field}", value) unless value.nil?
  end

  self
end