forked from jruizgit/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterface.rb
126 lines (113 loc) · 2.96 KB
/
interface.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
require "sinatra"
require "json"
module Interface
class Application < Sinatra::Base
set :bind, "0.0.0.0"
@@host = nil
def self.set_host(value)
@@host = value
end
get "/:ruleset_name/state/:sid" do
begin
JSON.generate @@host.get_state(params["ruleset_name"], params["sid"])
rescue Exception => e
status 404
e.to_s
end
end
post "/:ruleset_name/state/:sid" do
begin
request.body.rewind
state = JSON.parse request.body.read
state["id"] = params["sid"]
result = @@host.patch_state(params["ruleset_name"], state)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
get "/:ruleset_name/state" do
begin
JSON.generate @@host.get_state(params["ruleset_name"], "0")
rescue Exception => e
status 404
e.to_s
end
end
post "/:ruleset_name/state" do
begin
request.body.rewind
state = JSON.parse request.body.read
result = @@host.patch_state(params["ruleset_name"], state)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
post "/:ruleset_name/events/:sid" do
begin
request.body.rewind
message = JSON.parse request.body.read
message["sid"] = params["sid"]
result = @@host.post(params["ruleset_name"], message)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
post "/:ruleset_name/events" do
begin
request.body.rewind
message = JSON.parse request.body.read
result = @@host.post(params["ruleset_name"], message)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
post "/:ruleset_name/facts/:sid" do
begin
request.body.rewind
message = JSON.parse request.body.read
message["sid"] = params["sid"]
result = @@host.assert(params["ruleset_name"], message)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
post "/:ruleset_name/facts" do
begin
request.body.rewind
message = JSON.parse request.body.read
result = @@host.assert(params["ruleset_name"], message)
JSON.generate({:outcome => result})
rescue Exception => e
status 500
e.to_s
end
end
get "/:ruleset_name/definition" do
begin
@@host.get_ruleset(params["ruleset_name"]).to_json
rescue Exception => e
status 404
e.to_s
end
end
post "/:ruleset_name/definition" do
begin
request.body.rewind
@@host.set_ruleset params["ruleset_name"], JSON.parse(request.body.read)
rescue Exception => e
status 500
e.to_s
end
end
end
end