Class: YodleeWrap::YodleeApi

Inherits:
Object
  • Object
show all
Defined in:
lib/yodlee_wrap/yodlee_api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ YodleeApi

Returns a new instance of YodleeApi.



9
10
11
# File 'lib/yodlee_wrap/yodlee_api.rb', line 9

def initialize(config = {})
  configure(config)
end

Instance Attribute Details

#base_urlObject (readonly)

Returns the value of attribute base_url.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def base_url
  @base_url
end

#cobrand_nameObject (readonly)

Returns the value of attribute cobrand_name.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def cobrand_name
  @cobrand_name
end

#cobranded_authObject (readonly)

Returns the value of attribute cobranded_auth.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def cobranded_auth
  @cobranded_auth
end

#cobranded_passwordObject (readonly)

Returns the value of attribute cobranded_password.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def cobranded_password
  @cobranded_password
end

#cobranded_usernameObject (readonly)

Returns the value of attribute cobranded_username.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def cobranded_username
  @cobranded_username
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def logger
  @logger
end

#proxy_urlObject (readonly)

Returns the value of attribute proxy_url.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def proxy_url
  @proxy_url
end

#user_authObject (readonly)

Returns the value of attribute user_auth.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def user_auth
  @user_auth
end

#webhook_endpointObject (readonly)

Returns the value of attribute webhook_endpoint.



6
7
8
# File 'lib/yodlee_wrap/yodlee_api.rb', line 6

def webhook_endpoint
  @webhook_endpoint
end

Instance Method Details

#add_provider_account(provider_id, provider_params) ⇒ Object



122
123
124
# File 'lib/yodlee_wrap/yodlee_api.rb', line 122

def (provider_id, provider_params)
  user_session_execute_api(:post, "/v1/providers/providerAccounts?providerId=#{provider_id}", provider_params)
end

#cobranded_auth_headerObject



158
159
160
# File 'lib/yodlee_wrap/yodlee_api.rb', line 158

def cobranded_auth_header
  "cobSession=#{cobranded_session_token}"
end

#cobranded_loginObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/yodlee_wrap/yodlee_api.rb', line 31

def 
  params = {
    cobrand: {
      cobrandLogin: cobranded_username,
      cobrandPassword: cobranded_password,
      locale: "en_US"
    }
  }
  response = execute_api(:post, '/v1/cobrand/login', params)

  @cobranded_auth = response.success? ? response.body : nil

  response
end

#cobranded_session_execute_api(method, url, params = {}) ⇒ Object



150
151
152
# File 'lib/yodlee_wrap/yodlee_api.rb', line 150

def cobranded_session_execute_api(method, url, params = {})
  execute_api(method, url, params, cobranded_auth_header)
end

#cobranded_session_tokenObject



181
182
183
184
# File 'lib/yodlee_wrap/yodlee_api.rb', line 181

def cobranded_session_token
  return nil if cobranded_auth.nil?
  cobranded_auth.fetch('session', {}).fetch('cobSession', nil)
end

#configure(config = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/yodlee_wrap/yodlee_api.rb', line 13

def configure(config = {})
  validate(config)
  @cobrand_name = config[:cobrand_name] || YodleeWrap::Config.cobrand_name || 'restserver'
  @cobranded_username = config[:cobranded_username] || YodleeWrap::Config.cobranded_username
  @cobranded_password = config[:cobranded_password] || YodleeWrap::Config.cobranded_password
  @webhook_endpoint = config[:webhook_endpoint] || YodleeWrap::Config.webhook_endpoint
  @logger = config[:logger] || YodleeWrap::Config.logger
  info_log "YodleeApi configured with cobrand_name=#{cobrand_name} cobranded_username=#{cobranded_username} logger=#{logger}"
end

#debug_log(msg) ⇒ Object



191
192
193
# File 'lib/yodlee_wrap/yodlee_api.rb', line 191

def debug_log(msg)
  logger.debug(msg)
end

#delete_provider_account(provider_account_id) ⇒ Object



126
127
128
# File 'lib/yodlee_wrap/yodlee_api.rb', line 126

def ()
  user_session_execute_api(:delete, "/v1/providers/providerAccounts/#{}")
end

#execute_api(method, url, params, auth_header = '') ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/yodlee_wrap/yodlee_api.rb', line 166

def execute_api(method, url, params, auth_header = '')
  debug_log "calling #{url} with #{params}"
  ssl_opts = { verify: false }
  connection = Faraday.new(url: base_url, ssl: ssl_opts, request: { proxy: [] })
  response = connection.send(method) do |request|
    request.url "#{base_url}#{url}"
    request.headers['Authorization'] = auth_header
    request.body = params.to_json unless params.empty?
    request.headers['Content-Type'] = 'application/json' unless params.empty?
  end
  body = JSON.parse(response.body.blank? ? '{}' : response.body)
  debug_log "response=#{response.status} success?=#{response.success?} body=#{body}"
  Response.new(body, response.status)
end

#get_account(account_id) ⇒ Object



114
115
116
# File 'lib/yodlee_wrap/yodlee_api.rb', line 114

def ()
  user_session_execute_api(:get, "v1/accounts/#{}")
end

#get_accounts_for(provider_account_id, container_name) ⇒ Object



110
111
112
# File 'lib/yodlee_wrap/yodlee_api.rb', line 110

def get_accounts_for(, container_name)
  user_session_execute_api(:get, "/v1/accounts?providerAccountId=#{}&container=&#{container_name}")
end

#get_all_provider_accountsObject

Get all provider accounts for the currently logged in user.



138
139
140
# File 'lib/yodlee_wrap/yodlee_api.rb', line 138

def get_all_provider_accounts
  user_session_execute_api(:get, '/v1/providers/providerAccounts')
end

#get_provider_account_status(provider_account_id) ⇒ Object

After an account has been added, use the returned provider_account_id to get updates about the provider account. default to getting mfa questions if they are available.



133
134
135
# File 'lib/yodlee_wrap/yodlee_api.rb', line 133

def ()
  user_session_execute_api(:get, "/v1/providers/providerAccounts/#{}?include=credentials")
end

#get_provider_details(provider_id) ⇒ Object



118
119
120
# File 'lib/yodlee_wrap/yodlee_api.rb', line 118

def get_provider_details(provider_id)
  user_session_execute_api(:get, "/v1/providers/#{provider_id}")
end

#get_statementsObject



142
143
144
# File 'lib/yodlee_wrap/yodlee_api.rb', line 142

def get_statements
  user_session_execute_api(:get, '/v1/statements')
end

#get_transactionsObject



106
107
108
# File 'lib/yodlee_wrap/yodlee_api.rb', line 106

def get_transactions
  user_session_execute_api(:get, "/v1/transactions")
end

#info_log(msg) ⇒ Object



195
196
197
# File 'lib/yodlee_wrap/yodlee_api.rb', line 195

def info_log(msg)
  logger.info(msg)
end

#login_or_register_user(username:, password:, email:, subscribe: true) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/yodlee_wrap/yodlee_api.rb', line 91

def (username:, password:, email:, subscribe: true)
  info_log "Attempting to log in #{username}"
  response = (username: username, password: password)
  # TODO: look into what other errors could occur here
  if response.fail? && response.error_code == 'Y002'
    info_log "Invalid credentials for #{username}. Attempting to register"
    response = register_user(username: username, password: password, email: email, subscribe: subscribe)
  else
    info_log response.error_message
  end
  @user_auth = response.success? ? response.body : nil

  response
end

#login_user(username:, password:) ⇒ Object



57
58
59
60
61
62
# File 'lib/yodlee_wrap/yodlee_api.rb', line 57

def (username:, password:)
  params = user_params(username, password, nil)
  response = cobranded_session_execute_api(:post, '/v1/user/login', params)
  @user_auth = response.success? ? response.body : nil
  response
end

#logout_userObject



87
88
89
# File 'lib/yodlee_wrap/yodlee_api.rb', line 87

def logout_user
  user_session_execute_api(:post, '/v1/user/logout')
end

#register_user(username:, password:, email:, options: {}, subscribe: true) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/yodlee_wrap/yodlee_api.rb', line 64

def register_user(username:, password:, email:, options: {}, subscribe: true)
  params = user_params(username, password, email).merge(options)
  response = cobranded_session_execute_api(:post, '/v1/user/register', params)
  @user_auth = response.success? ? response.body : nil
  subscribe_user_to_refresh if response.success? && subscribe
  response
end

#subscribe_user_to_refreshObject

subscribe user to webhook refresh notifications



73
74
75
76
77
78
79
80
# File 'lib/yodlee_wrap/yodlee_api.rb', line 73

def subscribe_user_to_refresh
  params = {
    event: {
      callbackUrl: webhookEndpoint
    }
  }
  user_session_execute_api(:post, 'v1/cobrand/config/notifications/events/REFRESH', params)
end

#unregister_userObject



82
83
84
85
# File 'lib/yodlee_wrap/yodlee_api.rb', line 82

def unregister_user
  response = user_session_execute_api(:delete, 'v1/user/unregister')
  @user_auth = nil if response.success?
end

#update_provider_account(provider_account_id, provider_params) ⇒ Object



146
147
148
# File 'lib/yodlee_wrap/yodlee_api.rb', line 146

def (, provider_params)
  user_session_execute_api(:put, "/v1/providers/providerAccounts?providerAccountIds=#{}", provider_params)
end

#user_auth_headerObject



162
163
164
# File 'lib/yodlee_wrap/yodlee_api.rb', line 162

def user_auth_header
  cobranded_auth_header + ",userSession=#{user_session_token}"
end

#user_params(username, password, email) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/yodlee_wrap/yodlee_api.rb', line 46

def user_params(username, password, email)
  {
    user: {
      loginName: username,
      email: email,
      password: password,
      locale: 'en_US'
    }
  }
end

#user_session_execute_api(method, url, params = {}) ⇒ Object



154
155
156
# File 'lib/yodlee_wrap/yodlee_api.rb', line 154

def user_session_execute_api(method, url, params = {})
  execute_api(method, url, params, user_auth_header)
end

#user_session_tokenObject



186
187
188
189
# File 'lib/yodlee_wrap/yodlee_api.rb', line 186

def user_session_token
  return nil if user_auth.nil?
  user_auth.fetch('user', {}).fetch('session', {}).fetch('userSession')
end

#validate(config) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/yodlee_wrap/yodlee_api.rb', line 23

def validate(config)
  [:cobrand_name, :cobranded_username, :cobranded_password, :logger].each do |key|
    if config.key?(key) && config[key].nil?
      fail 'Invalid config provided to YodleeApi. Values may not be nil/blank.'
    end
  end
end