-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
76 lines (65 loc) · 1.86 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Sinatra app to demo Bank transactions to AI advice
require "bundler"
Bundler.require
Dotenv.load
require 'erb'
require 'securerandom'
require "sinatra/cookies"
get '/' do
erb :index
end
get '/institutions' do
@institutions = nordigen_client.institution.get_institutions("BE")
erb :institutions
end
post '/init_requisition' do
ref = SecureRandom.uuid # unique reference
init = nordigen_client.init_session(
# redirect url after successful authentication
redirect_url: "http://127.0.0.1:4567/after_requisition",
institution_id: params[:institution_id],
reference_id: ref,
)
cookies[:gc_ref] = ref
cookies[:requisition_id] = init["id"]
redirect(init["link"])
end
get '/accounts' do
@accounts = get_accounts(cookies[:requisition_id]).map do |account|
# details = account.get_details()["account"] # rate limit = 4/day
# balances = account.get_balances()["balances"] # rate limit = 4/day
{
id: account.account_id,
# iban: details["iban"],
# currency: details["currency"],
# balance: balances&.first&.dig("balanceAmount", "acount")
}
end
erb :accounts
end
get '/account' do
@account_id = params[:account_id]
erb :account
# todo: fetch last 30d transactions
# todo: ask Claude Sonet to give advice
end
get '/after_requisition' do
redirect to('/')
end
def get_accounts(req_id)
client = nordigen_client
requisition_data = client.requisition.get_requisition_by_id(req_id)
requisition_data["accounts"].map { |account_id| client.account(account_id) }
end
def nordigen_client
client = Nordigen::NordigenClient.new(
secret_id: ENV.fetch("GOCARDLESS_SECRET_ID"),
secret_key: ENV.fetch("GOCARDLESS_SECRET_KEY"),
)
token_data = client.generate_token()
client.set_token(token_data["access"])
client
end
def anthropic_client
Anthropic::Client.new(access_token: ENV.fetch("ANTHROPIC_API_KEY"))
end