-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.rb
57 lines (47 loc) · 1.08 KB
/
account.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
require 'sinatra'
require 'json'
set members: [
{id: 1, name: 'Patrick', card: '6014355731786694', points: 154},
{id: 2, name: 'Sam', card: '6014355731755590', points: 597},
{id: 3, name: 'Evie', card: '6014355731725686', points: 4512},
]
def members
settings.members
end
def id
params[:id].to_i
end
def amount
params[:amount].to_i
end
def member
members.select { |m| m[:id] == id }.first
end
post '/members/validate' do
# The correct card number has to be entered
candidates = members.select { |m| m[:name].downcase == params[:name].downcase && m[:card] == params[:card] }
if candidates.one?
candidates.first.reject { |k, v| k == :card }.to_json
else
status 401
nil
end
end
# Increase a member's account (amount must be over 0)
post '/members/:id/credit' do
if amount > 0
member[:points] += amount
else
status 400
end
member.reject { |k, v| k == :card }.to_json
end
# Decrease account
post '/members/:id/debit' do
if amount > 0
member[:points] -= amount
else
status 400
end
member.reject { |k, v| k == :card }.to_json
end