class OldPlaid::User
Attributes
Public Class Methods
API: semi-private This class method instantiates a new Account
object and updates it with the results from the API
# File lib/old_plaid/models/user.rb, line 70 def self.build(res, api_level = nil) self.new.update(res, api_level) end
Internal build methods
# File lib/old_plaid/models/user.rb, line 58 def initialize self.accounts = [] self.transactions = [] self.permissions = [] self.access_token = '' self.api_res = '' self.info = {} end
Public Instance Methods
API: public Use this method to delete a user from the OldPlaid
API
# File lib/old_plaid/models/user.rb, line 53 def delete_user Connection.delete('info', { access_token: self.access_token }) end
API: semi-private Gets auth, connect, or info of the user TODO: (2.0) auth_level should be symbols instead of string
# File lib/old_plaid/models/user.rb, line 101 def get(auth_level, options = {}) return false unless self.permit? auth_level case auth_level when 'auth' update(Connection.post('auth/get', access_token: self.access_token)) when 'connect' payload = { access_token: self.access_token }.merge(options) update(Connection.post('connect/get', payload)) when 'info' update(Connection.secure_get('info', self.access_token)) else raise "Invalid auth level: #{auth_level}" end end
API: semi-private
# File lib/old_plaid/models/user.rb, line 117 def get_auth get('auth') end
API: semi-private
# File lib/old_plaid/models/user.rb, line 122 def get_connect(options={}) get('connect', options) end
API: semi-private
# File lib/old_plaid/models/user.rb, line 127 def get_info get('info') end
API: public Use this method to send back the MFA code or answer
# File lib/old_plaid/models/user.rb, line 21 def mfa_authentication(auth, type = nil) type = self.type if type.nil? auth_path = self.permissions.last + '/step' res = Connection.post(auth_path, { mfa: auth, access_token: self.access_token, type: type }) self.accounts = [] self.transactions = [] update(res) end
API: semi-private Internal helper method to set the available API levels
# File lib/old_plaid/models/user.rb, line 93 def permit!(api_level) return if api_level.nil? || self.permit?(api_level) self.permissions << api_level end
API: public Use this method to find out API levels available for this user
# File lib/old_plaid/models/user.rb, line 32 def permit?(auth_level) self.permissions.include? auth_level end
API: public Use this method to select the MFA method
# File lib/old_plaid/models/user.rb, line 12 def select_mfa_method(selection, type=nil) type = self.type if type.nil? auth_path = self.permissions.last + '/step' res = Connection.post(auth_path, { options: { send_method: selection }.to_json, access_token: self.access_token, type: type }) update(res, self.permissions.last) end
API: semi-private This method updates Account
with the results returned from the API
# File lib/old_plaid/models/user.rb, line 76 def update(res, api_level = nil) self.permit! api_level if res[:msg].nil? populate_user!(res) clean_up_user! else set_mfa_request!(res) end self end
API: semi-private Helper method to update user balance
# File lib/old_plaid/models/user.rb, line 144 def update_balance update(Connection.post('balance', { access_token: self.access_token })) end
API: semi-private Helper method to update user information Requires 'info' api level
# File lib/old_plaid/models/user.rb, line 134 def update_info(username,pass,pin=nil) return false unless self.permit? 'info' payload = { username: username, password: pass, access_token: self.access_token } payload.merge!(pin: pin) if pin update(OldPlaid.patch('info', payload)) end
API: public Use this method to upgrade a user to another api level
# File lib/old_plaid/models/user.rb, line 38 def upgrade(api_level=nil) if api_level.nil? api_level = 'auth' unless self.permit? 'auth' api_level = 'connect' unless self.permit? 'connect' end res = Connection.post('upgrade', { access_token: self.access_token, upgrade_to: api_level }) # Reset accounts and transaction self.accounts = [] self.transactions = [] update(res) end
Private Instance Methods
# File lib/old_plaid/models/user.rb, line 150 def clean_up_user! self.accounts.select! { |c| c.instance_of? Account } end
# File lib/old_plaid/models/user.rb, line 160 def populate_user!(res) res['accounts'].each do |account| if self.accounts.none? { |h| h.account_id == account['_id'] } self.accounts << Account.new(account) end end if res['accounts'] res['transactions'].each do |transaction| if self.transactions.any? { |t| t == transaction['_id'] } owned_transaction = self.transactions.find { |h| h == transaction['_id'] } owned_transaction.new(transaction) else self.transactions << Transaction.new(transaction) end end if res['transactions'] self.pending_mfa_questions = {} self.information = Information.new(res['info']) if res['info'] self.api_res = 'success' # TODO: Remove the following line when upgrading to V-2 self.info.merge!(res['info']) if res['info'] # End TODO self.access_token = res['access_token'].split[0] self.type = res['access_token'].split[1] end
# File lib/old_plaid/models/user.rb, line 154 def set_mfa_request!(res) self.access_token = res[:body]['access_token'] self.pending_mfa_questions = res[:body] self.api_res = res[:msg] end