Class: Finicity::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/finicity/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token = nil, mfa_session = nil) ⇒ Client

Constructor



11
12
13
14
# File 'lib/finicity/client.rb', line 11

def initialize(token = nil, mfa_session = nil)
  @token = token
  @mfa_session = mfa_session
end

Instance Attribute Details

#mfa_sessionObject

Attributes



6
7
8
# File 'lib/finicity/client.rb', line 6

def mfa_session
  @mfa_session
end

#tokenObject

Attributes



6
7
8
# File 'lib/finicity/client.rb', line 6

def token
  @token
end

Instance Method Details

#activate_accounts(customer_id, institution_id, accounts) ⇒ Object

Instance Methods

The accounts parameter is an array of Finicity::V1::Reponse::Account



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/finicity/client.rb', line 20

def activate_accounts(customer_id, institution_id, accounts)
  request = ::Finicity::V1::Request::ActivateAccounts.new(token, customer_id, institution_id, accounts)
  request.log_request
  response = request.activate_accounts
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#activate_accounts_with_mfa(customer_id, institution_id, accounts, mfa_credentials) ⇒ Object

The accounts parameter is an array of Finicity::V1::Reponse::Account The mfa_credentials parameter is an array of hashes with keys :text, :answer



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/finicity/client.rb', line 41

def activate_accounts_with_mfa(customer_id, institution_id, accounts, mfa_credentials)
  request = ::Finicity::V1::Request::ActivateAccountsWithMfa.new(token, mfa_session, customer_id, institution_id, accounts, mfa_credentials)
  request.log_request
  response = request.activate_accounts_with_mfa
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#add_customer(user_guid) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/finicity/client.rb', line 60

def add_customer(user_guid)
  request = ::Finicity::V1::Request::AddCustomer.new(token, user_guid)
  request.log_request
  response = request.add_customer
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Customer.parse(response.body)
    return parsed_response
  else
    raise_generic_error!(response)
  end
end

#authenticate!Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/finicity/client.rb', line 74

def authenticate!
  request = ::Finicity::V2::Request::PartnerAuthentication.new
  response = request.authenticate

  if response.ok?
    parsed_response = ::Finicity::V2::Response::PartnerAuthentication.parse(response.body)
    @token = parsed_response.token
    parsed_response
  else
    fail ::Finicity::AuthenticationError.new(response.body, response.status_code)
  end
end

#delete_account(customer_id, account_id) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/finicity/client.rb', line 87

def (customer_id, )
  request = ::Finicity::V1::Request::DeleteAccount.new(token, customer_id, )
  request.log_request
  response = request.
  log_response(response)

  if response.ok?
    # No data to parse, so return a hash for consistent return type
    return {}
  else
    raise_generic_error!(response)
  end
end

#delete_customer(customer_id) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/finicity/client.rb', line 101

def delete_customer(customer_id)
  request = ::Finicity::V1::Request::DeleteCustomer.new(token, customer_id)
  response = request.delete_customer

  if response.ok?
    # No data to parse, so return a hash for consistent return type
    return {}
  else
    raise_generic_error!(response)
  end
end

#discover_accounts(customer_id, institution_id, login_credentials) ⇒ Object

The login_credentials parameter is an array of hashes with the keys :id, :name, :value



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/finicity/client.rb', line 114

def discover_accounts(customer_id, institution_id, )
  request = ::Finicity::V1::Request::DiscoverAccounts.new(token, customer_id, institution_id, )
  request.log_request
  response = request.discover_accounts
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#discover_accounts_with_mfa(customer_id, institution_id, login_credentials, mfa_credentials) ⇒ Object

The login_credentials parameter is an array of hashes with the keys :id, :name, :value The mfa_credentials parameter is an array of hashes with keys :text, :answer



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/finicity/client.rb', line 135

def discover_accounts_with_mfa(customer_id, institution_id, , mfa_credentials)
  request = ::Finicity::V1::Request::DiscoverAccountsWithMfa.new(token, mfa_session, customer_id, institution_id, , mfa_credentials)
  request.log_request
  response = request.discover_accounts_with_mfa
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#get_accounts(customer_id, institution_id) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/finicity/client.rb', line 154

def get_accounts(customer_id, institution_id)
  request = ::Finicity::V1::Request::GetAccounts.new(token, customer_id, institution_id)
  request.log_request
  response = request.get_accounts
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 404
    return []
  else
    raise_generic_error!(response)
  end
end

#get_customer_by_username(username) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/finicity/client.rb', line 170

def get_customer_by_username(username)
  request = ::Finicity::V1::Request::GetCustomersByUsername.new(token, username, 1, 1)
  request.log_request
  response = request.get_customers_by_username
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Customers.parse(response.body)
    if parsed_response.found && parsed_response.found > 1
      raise ::Finicity::DuplicateCustomerError.new(username)
    else
      return parsed_response.customers.first
    end
  else
    raise_generic_error!(response)
  end
end

#get_customersObject



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/finicity/client.rb', line 202

def get_customers
  request = ::Finicity::V1::Request::GetCustomers.new(token)
  start = 1
  limit = 25
  customers = []

  loop do
    response = request.get_customers(start, limit)

    if response.ok?
      parsed_response = ::Finicity::V1::Response::Customers.parse(response.body)
      customers << parsed_response.customers

      if parsed_response.more_available?
        start += limit
      else
        return customers.flatten
      end
    else
      raise_generic_error!(response)
    end
  end
end

#get_customers_by_username(username, start, limit) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/finicity/client.rb', line 188

def get_customers_by_username(username, start, limit)
  request = ::Finicity::V1::Request::GetCustomersByUsername.new(token, username, start, limit)
  request.log_request
  response = request.get_customers_by_username
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Customers.parse(response.body)
    return parsed_response.customers
  else
    raise_generic_error!(response)
  end
end

#get_institution(institution_id) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/finicity/client.rb', line 226

def get_institution(institution_id)
  request = ::Finicity::V1::Request::GetInstitution.new(token, institution_id)
  response = request.get_institution

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Institution.parse(response.body)
    return parsed_response
  else
    raise_generic_error!(response)
  end
end

#get_institutions(institution_name) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/finicity/client.rb', line 238

def get_institutions(institution_name)
  request = ::Finicity::V1::Request::GetInstitutions.new(token, institution_name)
  start = 1
  limit = 25
  institutions = []

  loop do
    response = request.get_institutions(start, limit)

    if response.ok?
      parsed_response = ::Finicity::V1::Response::Institutions.parse(response.body)
      institutions << parsed_response.institutions

      if parsed_response.more_available?
        start += limit
      else
        return institutions.flatten
      end
    else
      raise_generic_error!(response)
    end
  end
end

#get_login_form(institution_id) ⇒ Object



262
263
264
265
266
267
268
269
270
271
272
# File 'lib/finicity/client.rb', line 262

def (institution_id)
  request = ::Finicity::V1::Request::GetLoginForm.new(token, institution_id)
  response = request.

  if response.ok?
    parsed_response = ::Finicity::V1::Response::LoginForm.parse(response.body)
    return parsed_response.
  else
    raise_generic_error!(response)
  end
end

#get_transactions(customer_id, account_id, from_date, to_date) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/finicity/client.rb', line 274

def get_transactions(customer_id, , from_date, to_date)
  request = ::Finicity::V1::Request::GetTransactions.new(token, customer_id, , from_date, to_date)
  request.log_request
  response = request.get_transactions
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Transactions.parse(response.body)
    return parsed_response.transactions
  else
    raise_generic_error!(response)
  end
end

#interactive_refresh_account(customer_id, account_id) ⇒ Object



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/finicity/client.rb', line 288

def (customer_id, )
  request = ::Finicity::V1::Request::InteractiveRefreshAccount.new(token, customer_id, )
  request.log_request
  response = request.
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#interactive_refresh_account_with_mfa(customer_id, account_id, mfa_credentials) ⇒ Object

The mfa_credentials parameter is an array of hashes with keys :text, :answer



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/finicity/client.rb', line 308

def (customer_id, , mfa_credentials)
  request = ::Finicity::V1::Request::InteractiveRefreshAccountWithMfa.new(token, mfa_session, customer_id, , mfa_credentials)
  request.log_request
  response = request.
  log_response(response)

  if response.status_code == 200
    @mfa_session = nil
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  elsif response.status_code == 203
    @mfa_session = response.headers["MFA-Session"]
    parsed_response = ::Finicity::V1::Response::Mfa.parse(response.body)
    return parsed_response.questions
  else
    raise_generic_error!(response)
  end
end

#refresh_accounts(customer_id) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/finicity/client.rb', line 327

def refresh_accounts(customer_id)
  request = ::Finicity::V1::Request::RefreshAccounts.new(token, customer_id)
  request.log_request
  response = request.refresh_accounts
  log_response(response)

  if response.ok?
    parsed_response = ::Finicity::V1::Response::Accounts.parse(response.body)
    return parsed_response.accounts
  else
    raise_generic_error!(response)
  end
end

#update_credentials(customer_id, account_id, login_credentials) ⇒ Object

The login_credentials parameter is an array of hashes with the keys :id, :name, :value



342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/finicity/client.rb', line 342

def update_credentials(customer_id, , )
  request = ::Finicity::V1::Request::UpdateCredentials.new(token, customer_id, , )
  request.log_request
  response = request.update_credentials
  log_response(response)

  if response.ok?
    # No data to parse, so return a hash for consistent return type
    return {}
  else
    raise_generic_error!(response)
  end
end