rule_engine_client 0.1.7 → 0.1.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/rule_engine_client.rb +43 -32
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1b95bd4b0bd8099878d8b57f32a032dea0053c8e83832f01a8e3342c7d56081
|
4
|
+
data.tar.gz: 3c0fc92094388561886b7a4c9c8a2715bf30fd6011ccd36dc194f7b753df3a4a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a013886767656956cc8dd7b40603289c9bfe70e300341b755212f93fd0fe3c8579c110c6615647085bb0b3bfcf3b4896bdb60b0f0658f4273f9db3237f7c5a8b
|
7
|
+
data.tar.gz: 19472829863cd6e47b91a3ba1d88c4e475778e0432b4979311bdc9e0b76d7ba71b8d9f5fbf4c1c4e1c90a2ad8faec50a8f4492ff1bd81950b2db80dd295210cb
|
data/lib/rule_engine_client.rb
CHANGED
@@ -5,16 +5,42 @@ require 'timeout'
|
|
5
5
|
|
6
6
|
# rule engine client class
|
7
7
|
class RuleEngineClient
|
8
|
-
def initialize(server = 'http://localhost:8088',
|
8
|
+
def initialize(server = 'http://localhost:8088', request_timeout_in_s = 5, verbose = false)
|
9
9
|
@server = server
|
10
10
|
@verbose = verbose
|
11
11
|
@logger = Logger.new($stdout)
|
12
|
-
@
|
12
|
+
@request_timeout_in_s = request_timeout_in_s
|
13
13
|
log("rule engine '#{server}' client is initialized")
|
14
14
|
end
|
15
15
|
|
16
|
+
def evaluate(engine, params, mode = 'production')
|
17
|
+
url = URI.parse("#{@server}/run?e=#{engine}&m=#{mode}")
|
18
|
+
log("Will send post request: #{url}")
|
19
|
+
@result = create_request(url, 'POST', params)
|
20
|
+
respone = response_builder
|
21
|
+
log("respone #{respone}")
|
22
|
+
respone
|
23
|
+
end
|
24
|
+
|
25
|
+
def engine_exists?(engine)
|
26
|
+
log("checking if engine (#{engine}) exists")
|
27
|
+
url = URI.parse("#{@server}/engine/export?e=#{engine}")
|
28
|
+
log("Will send get request: #{url}")
|
29
|
+
response = create_request(url, 'GET')
|
30
|
+
if @time_out
|
31
|
+
engine_exists = false
|
32
|
+
else
|
33
|
+
engine_exists = response.code == '200'
|
34
|
+
log_message = engine_exists ? "engine #{engine} exists" : "engine #{engine} not found"
|
35
|
+
log(log_message)
|
36
|
+
end
|
37
|
+
engine_exists
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
16
42
|
def log(message)
|
17
|
-
@logger.debug("
|
43
|
+
@logger.debug("rule engine: #{message}") if @verbose
|
18
44
|
end
|
19
45
|
|
20
46
|
def valid_json?(obj)
|
@@ -33,7 +59,7 @@ class RuleEngineClient
|
|
33
59
|
|
34
60
|
def response_builder
|
35
61
|
if @time_out
|
36
|
-
errors = ['HTTP
|
62
|
+
errors = ['HTTP Gateway timeout!']
|
37
63
|
body = {}
|
38
64
|
code = 504
|
39
65
|
elsif @result.code == '502'
|
@@ -53,38 +79,23 @@ class RuleEngineClient
|
|
53
79
|
response_hash(code, body, errors)
|
54
80
|
end
|
55
81
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
82
|
+
def create_request(url, request_type, params = {})
|
83
|
+
Timeout.timeout(@request_timeout_in_s) do
|
84
|
+
http = Net::HTTP.new(url.host, url.port)
|
85
|
+
http.use_ssl = true if url.instance_of? URI::HTTPS
|
86
|
+
if request_type == 'GET'
|
87
|
+
request = Net::HTTP::Get.new(url.request_uri)
|
88
|
+
else
|
63
89
|
request = Net::HTTP::Post.new(url.request_uri)
|
64
|
-
request['Content-Type'] = 'application/json'
|
65
90
|
request.body = params.to_json
|
66
91
|
log("request body created #{request.body} ")
|
67
|
-
@result = http.request(request)
|
68
|
-
respone = response_builder
|
69
|
-
log("respone #{respone}")
|
70
|
-
respone
|
71
92
|
end
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
response_builder
|
93
|
+
request['Content-Type'] = 'application/json'
|
94
|
+
request['Accept'] = 'application/json'
|
95
|
+
http.request(request)
|
76
96
|
end
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
log("checking if work space (#{work_space}) exists")
|
81
|
-
url = URI.parse("http://#{@server}/engine/export?e=#{work_space}")
|
82
|
-
req = Net::HTTP::Get.new(url.to_s)
|
83
|
-
response = Net::HTTP.new(url.host, url.port).start { |http| http.request(req) }
|
84
|
-
work_space_exists = response.code == 200
|
85
|
-
|
86
|
-
log_message = work_space_exists ? "work space #{work_space} exists" : "work space #{work_space} not found"
|
87
|
-
log(log_message)
|
88
|
-
work_space_exists
|
97
|
+
rescue Timeout::Error
|
98
|
+
log('HTTP Gateway timeout!')
|
99
|
+
@time_out = true
|
89
100
|
end
|
90
101
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rule_engine_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Areej Nour
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-08-
|
11
|
+
date: 2022-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: integration with rule server
|
14
14
|
email:
|