-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.rb
More file actions
98 lines (75 loc) · 1.65 KB
/
proxy.rb
File metadata and controls
98 lines (75 loc) · 1.65 KB
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
class TeamAccount
attr_reader :balance
def initialize(balance = 0)
@balance = balance
end
def increase(amount)
@balance += amount
end
def decrease(amount)
@balance -= amount
end
end
class VirtualAccountProxy
def initialize(balance = 0)
@balance = balance
end
def increase(amount)
subject.increase(amount)
end
def decrease(amount)
subject.decrease(amount)
end
def balance
subject.balance
end
private
def subject
@subject ||= TeamAccount.new(@balance)
end
end
class RemoteAccountProxy
def initialize
@base_uri = "localhost:3000/team_account"
end
def balance
rest_service.get("/balance")
end
def increase(amount)
rest_service.post("/increase", {amount: amount})
end
def decrease(amount)
rest_service.delete("/decrease", {amount: amount})
end
private
attr_reader :rest_service
def rest_service
@rest_client ||= RestClient.new(@base_uri, :json)
end
end
class ProtectionAccountProxy
attr_reader :user_credentials
def initialize(user_credentials)
@subject = TeamAccount.new
@user_credentials = user_credentials
end
def balance
check_permissions(:read)
@subject.balance
end
def increase(amount)
check_permissions(:write)
@subject.increase(amount)
end
def decrease(amount)
check_permissions(:write)
@subject.decrease(amount)
end
private
def check_permissions(permission_type)
# CredentialValidator isn't implemented in this example
unless CredentialValidator.validate(@user_credentials, permission_type)
raise "Account #{@user_credentials} #{permission_type} action denied."
end
end
end