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.
@@ -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