lazy_graph 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +282 -47
- data/Rakefile +12 -1
- data/examples/converter.rb +41 -0
- data/examples/performance_tests.rb +9 -9
- data/examples/shopping_cart_totals.rb +56 -0
- data/lib/lazy_graph/builder/dsl.rb +67 -30
- data/lib/lazy_graph/builder.rb +52 -25
- data/lib/lazy_graph/builder_group.rb +31 -18
- data/lib/lazy_graph/cli.rb +47 -0
- data/lib/lazy_graph/context.rb +37 -12
- data/lib/lazy_graph/environment.rb +6 -0
- data/lib/lazy_graph/graph.rb +28 -12
- data/lib/lazy_graph/hash_utils.rb +10 -5
- data/lib/lazy_graph/logger.rb +87 -0
- data/lib/lazy_graph/missing_value.rb +9 -2
- data/lib/lazy_graph/node/array_node.rb +14 -18
- data/lib/lazy_graph/node/derived_rules.rb +72 -17
- data/lib/lazy_graph/node/node_properties.rb +34 -9
- data/lib/lazy_graph/node/object_node.rb +41 -56
- data/lib/lazy_graph/node/symbol_hash.rb +15 -2
- data/lib/lazy_graph/node.rb +183 -94
- data/lib/lazy_graph/path_parser/path.rb +15 -7
- data/lib/lazy_graph/path_parser/path_group.rb +6 -0
- data/lib/lazy_graph/path_parser/path_part.rb +8 -0
- data/lib/lazy_graph/path_parser.rb +1 -1
- data/lib/lazy_graph/rack_app.rb +138 -0
- data/lib/lazy_graph/rack_server.rb +199 -0
- data/lib/lazy_graph/stack_pointer.rb +22 -14
- data/lib/lazy_graph/version.rb +1 -1
- data/lib/lazy_graph.rb +6 -8
- metadata +115 -11
- data/lib/lazy_graph/server.rb +0 -91
data/lib/lazy_graph/server.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module LazyGraph
|
4
|
-
class Server
|
5
|
-
ALLOWED_VALUES_VALIDATE = [true, false, nil, 'input', 'context'].to_set.freeze
|
6
|
-
ALLOWED_VALUES_DEBUG = [true, false, nil].to_set.freeze
|
7
|
-
|
8
|
-
def initialize(routes: {})
|
9
|
-
@routes = routes.transform_keys(&:to_sym).compare_by_identity
|
10
|
-
end
|
11
|
-
|
12
|
-
def call(env)
|
13
|
-
# Rack environment contains request details
|
14
|
-
env[:X_REQUEST_TIME_START] = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
15
|
-
request = Rack::Request.new(env)
|
16
|
-
|
17
|
-
unless (graph_module = @routes[request.path.to_sym])
|
18
|
-
return not_found!(request.path)
|
19
|
-
end
|
20
|
-
|
21
|
-
return success!(request, graph_module.usage) if request.get?
|
22
|
-
|
23
|
-
if request.post?
|
24
|
-
body = JSON.parse(request.body.read)
|
25
|
-
context, modules, validate, debug, query = body.values_at('context', 'modules', 'validate', 'debug', 'query')
|
26
|
-
unless context.is_a?(Hash) && !context.empty?
|
27
|
-
return not_acceptable!(request, "Invalid 'context' Parameter", 'Should be a non-empty object.')
|
28
|
-
end
|
29
|
-
|
30
|
-
unless modules.is_a?(Hash) && !modules.empty?
|
31
|
-
return not_acceptable!(request, "Invalid 'modules' Parameter", 'Should be a non-empty object.')
|
32
|
-
end
|
33
|
-
|
34
|
-
unless ALLOWED_VALUES_VALIDATE.include?(validate)
|
35
|
-
return not_acceptable!(
|
36
|
-
request, "Invalid 'validate' Parameter", "Should be nil, bool, or one of 'input', 'context'"
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
unless ALLOWED_VALUES_DEBUG.include?(debug)
|
41
|
-
return not_acceptable!(request, "Invalid 'debug' Parameter", 'Should be nil or bool')
|
42
|
-
end
|
43
|
-
|
44
|
-
unless query.nil? || query.is_a?(String) || (query.is_a?(Array) && query.all? do |q|
|
45
|
-
q.is_a?(String)
|
46
|
-
end)
|
47
|
-
return not_acceptable!(request, "Invalid 'query' Parameter", 'Should be nil, array or string array')
|
48
|
-
end
|
49
|
-
|
50
|
-
begin
|
51
|
-
result = graph_module.eval!(
|
52
|
-
modules: modules, context: context,
|
53
|
-
validate: validate, debug: debug,
|
54
|
-
query: query
|
55
|
-
)
|
56
|
-
return not_acceptable!(request, result[:message], result[:detail]) if result[:type] == :error
|
57
|
-
|
58
|
-
return success!(request, result)
|
59
|
-
rescue StandardError => e
|
60
|
-
LazyGraph.logger.error(e.message)
|
61
|
-
LazyGraph.logger.error(e.backtrace.join("\n"))
|
62
|
-
return error!(request, 500, 'Internal Server Error', e.message)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
not_found!("#{request.request_method} #{request.path}")
|
67
|
-
end
|
68
|
-
|
69
|
-
def not_acceptable!(request, message, details = '')
|
70
|
-
error!(request, 406, message, details)
|
71
|
-
end
|
72
|
-
|
73
|
-
def not_found!(request, details = '')
|
74
|
-
error!(request, 404, 'Not Found', details)
|
75
|
-
end
|
76
|
-
|
77
|
-
def request_ms(request)
|
78
|
-
((Process.clock_gettime(Process::CLOCK_MONOTONIC) - request.env[:X_REQUEST_TIME_START]) * 1000.0).round(3)
|
79
|
-
end
|
80
|
-
|
81
|
-
def success!(request, result, status: 200)
|
82
|
-
LazyGraph.logger.info("#{request.request_method}: #{request.path} => #{status} #{request_ms(request)}ms")
|
83
|
-
[status, { 'Content-Type' => 'text/json' }, [result.to_json]]
|
84
|
-
end
|
85
|
-
|
86
|
-
def error!(request, status, message, details = '')
|
87
|
-
LazyGraph.logger.info("#{request.request_method}: #{request.path} => #{status} #{request_ms(request)}ms")
|
88
|
-
[status, { 'Content-Type' => 'text/json' }, [{ 'error': message, 'details': details }.to_json]]
|
89
|
-
end
|
90
|
-
end
|
91
|
-
end
|