Class: Syrup::Institutions::InstitutionBase

Inherits:
Object
  • Object
show all
Defined in:
lib/syrup/institutions/institution_base.rb

Direct Known Subclasses

Uccu, ZionsBank

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeInstitutionBase

Returns a new instance of InstitutionBase.



41
42
43
# File 'lib/syrup/institutions/institution_base.rb', line 41

def initialize
  @accounts = []
end

Instance Attribute Details

#passwordObject

Returns the value of attribute password.



39
40
41
# File 'lib/syrup/institutions/institution_base.rb', line 39

def password
  @password
end

#secret_questionsObject

Returns the value of attribute secret_questions.



39
40
41
# File 'lib/syrup/institutions/institution_base.rb', line 39

def secret_questions
  @secret_questions
end

#usernameObject

Returns the value of attribute username.



39
40
41
# File 'lib/syrup/institutions/institution_base.rb', line 39

def username
  @username
end

Class Method Details

.inherited(subclass) ⇒ Object

This method is called whenever a class inherits from this class. We keep track of all of them because they should all be institutions. This way we can provide a list of supported institutions via code.



11
12
13
14
# File 'lib/syrup/institutions/institution_base.rb', line 11

def inherited(subclass)
  @subclasses ||= []
  @subclasses << subclass
end

.subclassesObject

Returns an array of all classes that inherit from this class. Or, in other words, an array of all supported institutions



18
19
20
# File 'lib/syrup/institutions/institution_base.rb', line 18

def subclasses
  @subclasses
end

Instance Method Details

#accountsObject



64
65
66
67
# File 'lib/syrup/institutions/institution_base.rb', line 64

def accounts
  populate_accounts
  @accounts
end

#find_account_by_id(account_id) ⇒ Object

Returns an account with the specified account_id. Always use this method to create a new ‘Account` object. If you do, it will get populated correctly whenever the population occurs.



72
73
74
75
76
77
78
79
# File 'lib/syrup/institutions/institution_base.rb', line 72

def ()
   = @accounts.find { |a| a.id ==  }
  unless  || populated?
     = Account.new(:id => , :institution => self)
    @accounts << 
  end
  
end

#populate_account(account_id) ⇒ Object

Populates an account given an ‘account_id`. The implementing institution may populate all accounts when this is called if there isn’t a way to only request one account’s information.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/syrup/institutions/institution_base.rb', line 84

def ()
  unless populated?
    result = ()
    return nil if result.nil?
    
    if result.respond_to?(:each)
      populate_accounts(result)
      ()
    else
      result.populated = true
       = ()
      .merge! result if 
    end
  end
end

#populate_accounts(populated_accounts = nil) ⇒ Object

Populates all of the user’s accounts at this institution.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/syrup/institutions/institution_base.rb', line 101

def populate_accounts(populated_accounts = nil)
  unless populated?
    all_accounts = populated_accounts || fetch_accounts
    
    # Remove any accounts that were added, that don't actually exist
    @accounts.delete_if do |a|
      if all_accounts.include?(a)
        false
      else
        a.valid = false
        true
      end
    end
    
    # Add any additional account information
    new_accounts = []
    all_accounts.each do ||
       = @accounts.find { |a| a.id == .id }
      
      .populated = true
      
      # If we already had an account with this id, fill it with data
      if 
        .merge! 
      else
        new_accounts << 
      end
    end
    @accounts |= new_accounts # Uses set union
    
    self.populated = true
  end
end

#populated=(value) ⇒ Object



60
61
62
# File 'lib/syrup/institutions/institution_base.rb', line 60

def populated=(value)
  @populated = value
end

#populated?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/syrup/institutions/institution_base.rb', line 56

def populated?
  @populated
end

#setup {|_self| ... } ⇒ Object

This method allows you to setup an institution with block syntax

InstitutionBase.setup do |config|
  config.username = 'my_user"
  ...
end

Yields:

  • (_self)

Yield Parameters:



51
52
53
54
# File 'lib/syrup/institutions/institution_base.rb', line 51

def setup
  yield self
  self
end