forked from jplewicke/rivulet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routes.rb
192 lines (154 loc) · 5.47 KB
/
routes.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
require "rubygems"
require "bundler"
Bundler.setup
require "sinatra"
require "json"
require "app_classes"
require "auth"
require "parse"
get '/' do
File.read(File.join('public', 'index.html'))
end
get '/accounts/:payor/?' do |payor|
Neo4j::Transaction.run do |t|
auth_list = [payor]
protected!(auth_list)
User.fromid(payor).to_json
end
end
# Accepts an option to= parameter for the
get '/credits/:lender/?' do |lender|
Neo4j::Transaction.run do |t|
lendee = params["to"]
if lendee != nil
auth_list = [lender,lendee]
else
auth_list = [lender]
end
protected!(auth_list)
requested_by = authed_user(auth_list)
if requested_by == lendee || params["to"] != nil
source, dest = get_neo_users(lender, lendee)
CreditRelationship.new(source,dest).to_json
else
source = User.fromid(lender)
puts source.trusts
end
end
end
post '/accounts/?' do
Neo4j::Transaction.run do |t|
if params["user"] != nil && params["secret"] != nil && User.fromid(params["user"]) == nil
depth = 8
if params["depth"] != nil && posinteger?(params["depth"])
depth = Integer(params["depth"])
end
#Create account for new user.
user = User.new :user_id => params["user"], :depth => depth, :encrypted_password => BCrypt::Password.create(params["secret"])
user.to_json
else
throw(:halt, [403, "Not authorized\n"])
end
end
end
post '/credits/:lender/?' do |lender|
Neo4j::Transaction.run do |t|
lendee = params["to"]
protected!([lender, lendee])
parses!(params)
source, dest = get_neo_users(lender, lendee)
requested_by = authed_user([lender, lendee])
#If they authenticate as #{lender}, then we parse their request to see to whom
#they'd like to extend credit, and how much they'd like to extend.
#If they authenticate as someone else, we make sure they match the user identified
#in the request. The lendee allowed to set the maximum amount they'd like to
#borrow from the lender.
rel = CreditRelationship.new(source, dest)
source_offer = rel.source_offer
if (lender == requested_by)
source_offer.max_offered = params["amount"]
elsif (lendee == requested_by)
source_offer.max_desired = params["amount"]
else
throw(:halt, [500, "Error finding user in Neo4j after authentication.\n"])
end
#TODO: have it try to reclaim any credit in excess of what is currently possible.
rel.save!()
{:from => lender, :to => lendee, :credit_offered => source_offer.max_offered, :credit_accepted => source_offer.max_desired}.to_json
#puts rel.to_json
#puts "\n"
rel.to_json
end
end
post '/transactions/:payor/?' do |payor|
Neo4j::Transaction.run do |t|
protected!([payor])
parses!(params)
payee = params["to"]
source, dest = get_neo_users(payee, payor)
#This is an attempt to pay someone other than #{payor} by crediting the payee
#through the credit network. Since this will place #{payor} in debt to someone who
#has extended credit to him/her or will debit an existing credit balance owed to
##{payor}, this must be authorized by #{payor}.
#After we parse the request and take care of authorization, we need to make sure
#that this operation can be completed in a single Neo4j transaction and that a
#sufficient number of credits can be transferred.
path = CreditPath.new(source, dest)
to_transfer = params["amount"]
amount, rel = path.transfer_rollback!(to_transfer,t)
#Since the default method for transferring credits accumulates the resulting credit
#to the payee as a reserved amount, we need to clear that hold and store this transaction
#before we can complete it.
rel.dest_offer.amount_held -= to_transfer
#puts rel.to_json
#puts "\n"
rel.to_json
end
end
post '/transactions/:payor/held/?' do |payor|
Neo4j::Transaction.run do |t|
payee = params["to"]
protected!([payor, payee])
parses!(params)
source, dest = get_neo_users(payee, payor)
requested_by = authed_user([payor, payee])
#If we can properly authenticate the issuer of this request as either payor or
#payee, we will accumulate the requested amount of credit from payor to payee
#by debiting payor's existing credit lines.
path = CreditPath.new(source, dest)
to_transfer = params["amount"]
amount, rel = path.transfer_rollback!(to_transfer,t)
#puts rel.to_json
#puts "\n"
rel.to_json
end
end
#Release part of a held credit balance as payment or a gift.
post '/transactions/:payor/held/:payee/?' do |payor, payee|
Neo4j::Transaction.run do |t|
protected!([payor])
parses!(params)
source, dest = get_neo_users(payee, payor)
rel = CreditRelationship.new(source, dest)
can_transfer = rel.dest_offer.amount_held
to_transfer = params["amount"]
if can_transfer < to_transfer
throw(:halt, [403, "Insufficient reserved balance."])
else
rel.dest_offer.amount_held -= to_transfer
rel.to_json
end
end
end
#Should have an option to DELETE a held credit balance.
# Accepts an option to= parameter for the
get '/transactions/:payor/?' do |payor|
throw(:halt, [500, "not implemented yet"])
end
get '/transactions/:payor/held/?' do |payor|
throw(:halt, [500, "not implemented yet"])
end
# Accepts an option to= parameter for the
get '/credits/:payor' do |payor|
throw(:halt, [500, "not implemented yet"])
end