metarpc 0.0.1 → 0.0.2.1
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/metarpc/callable.rb +50 -32
- metadata +15 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c39e7104662301dc68561a54ace87f5f6826f17f94773890e0d07beeab31aa2
|
4
|
+
data.tar.gz: d74160dd168e6064084094670e8e70db8615959e31dd7fdbfa395b517f97c00c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c8106b3e246dcad98ef390fc3ce0f2ea8ea3bb604611a6ac7a8e2b760a2ed557b3ad884392ab6d8c0022b9c7326eb4187b9f0b1f8d47a80e8555383fe603e80c
|
7
|
+
data.tar.gz: 4d4833dc9484449c7f91295f6b033994791a1587316d10b95564133fcbcc368fb02cb890d220b803d0076b93e9efd408ea5f0bc58bc7af97b539538ec613b145
|
data/lib/metarpc/callable.rb
CHANGED
@@ -6,8 +6,49 @@ module MetaRPC
|
|
6
6
|
klass.send(:extend, ClassMethods)
|
7
7
|
end
|
8
8
|
|
9
|
-
def
|
10
|
-
raise RPCError.new(code), "Failed to call JSON RPC method
|
9
|
+
def raise_json_rpc_error(code)
|
10
|
+
raise RPCError.new(code), "Failed to call JSON RPC method: #{code}"
|
11
|
+
end
|
12
|
+
|
13
|
+
DYNAMIC_TYPE_SUFFIXES = {
|
14
|
+
nil => :not_nullable_nor_spread,
|
15
|
+
'?*' => :nullable_spread,
|
16
|
+
'*?' => :spread_nullable,
|
17
|
+
'?' => :nullable,
|
18
|
+
'*' => :spread
|
19
|
+
}.freeze
|
20
|
+
DYNAMIC_TYPE_REGEXP = /^(\w+)(\?|\?\*|\*\?|\*)?$/.freeze
|
21
|
+
|
22
|
+
def validate_type(item, klass, nullable: false)
|
23
|
+
return raise_json_rpc_error(:invalid_params) if item.nil? && !nullable
|
24
|
+
|
25
|
+
raise_json_rpc_error(:invalid_params) unless (item.present? && item.is_a?(klass)) || item.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
def validate_spread(items, klass, items_nullable: false, spread_nullable: false)
|
29
|
+
raise_json_rpc_error(:invalid_params) if items.nil? && !spread_nullable
|
30
|
+
items&.each { |item| validate_type(item, klass, nullable: items_nullable) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_dynamic_type(item, contract_type)
|
34
|
+
matched_type = contract_type.match(DYNAMIC_TYPE_REGEXP)
|
35
|
+
klass = matched_type[1].constantize
|
36
|
+
type_suffix = DYNAMIC_TYPE_SUFFIXES[matched_type[2]]
|
37
|
+
|
38
|
+
case type_suffix
|
39
|
+
when :not_nullable_nor_spread
|
40
|
+
validate_type(item, klass)
|
41
|
+
when :nullable
|
42
|
+
validate_type(item, klass, nullable: true)
|
43
|
+
when :spread
|
44
|
+
validate_spread(item, klass)
|
45
|
+
when :nullable_spread
|
46
|
+
validate_spread(item, klass, items_nullable: true)
|
47
|
+
when :spread_nullable
|
48
|
+
validate_spread(item, klass, spread_nullable: true)
|
49
|
+
else
|
50
|
+
raise_json_rpc_error(:internal_error)
|
51
|
+
end
|
11
52
|
end
|
12
53
|
|
13
54
|
def validate_item(item, contract_type)
|
@@ -17,37 +58,14 @@ module MetaRPC
|
|
17
58
|
when Array
|
18
59
|
validate_array(item, contract_type)
|
19
60
|
when Class
|
20
|
-
|
61
|
+
raise_json_rpc_error(:invalid_params) unless item.is_a?(contract_type)
|
21
62
|
when String
|
22
|
-
|
23
|
-
[contract_type.delete('!').constantize, true, false]
|
24
|
-
|
25
|
-
elsif contract_type.end_with?('!...')
|
26
|
-
[contract_type.delete('!...').constantize, true, true]
|
27
|
-
|
28
|
-
elsif contract_type.end_with?('...')
|
29
|
-
[contract_type.delete('...').constantize, false, true]
|
30
|
-
|
31
|
-
else
|
32
|
-
[contract_type.constantize, false, false]
|
33
|
-
end
|
34
|
-
|
35
|
-
if spread
|
36
|
-
raise json_rpc_error(:invalid_params) unless item.is_a?(Array)
|
37
|
-
|
38
|
-
item.each do |sub_item|
|
39
|
-
next if sub_item.nil? && nullable
|
40
|
-
raise json_rpc_error(:invalid_params) unless sub_item.present? && sub_item.is_a?(klass)
|
41
|
-
end
|
42
|
-
else
|
43
|
-
return if item.nil? && nullable
|
44
|
-
raise json_rpc_error(:invalid_params) unless item.present? && item.is_a?(klass)
|
45
|
-
end
|
63
|
+
validate_dynamic_type(item, contract_type)
|
46
64
|
else
|
47
|
-
|
65
|
+
raise_json_rpc_error(:internal_error)
|
48
66
|
end
|
49
67
|
rescue NameError
|
50
|
-
|
68
|
+
raise_json_rpc_error(:internal_error)
|
51
69
|
end
|
52
70
|
|
53
71
|
def validate_array(args, contract)
|
@@ -58,7 +76,7 @@ module MetaRPC
|
|
58
76
|
|
59
77
|
def validate_hash(args, contract)
|
60
78
|
contract.each do |arg_name, contract_type|
|
61
|
-
|
79
|
+
raise_json_rpc_error(:invalid_params) unless args.key?(arg_name)
|
62
80
|
|
63
81
|
validate_item(args[arg_name], contract_type)
|
64
82
|
end
|
@@ -87,7 +105,7 @@ module MetaRPC
|
|
87
105
|
when Hash
|
88
106
|
validate_item(args[0], params_contract)
|
89
107
|
else
|
90
|
-
|
108
|
+
raise_json_rpc_error(:internal_error)
|
91
109
|
end
|
92
110
|
|
93
111
|
old_method.bind(self).call(*args, &block)
|
@@ -96,7 +114,7 @@ module MetaRPC
|
|
96
114
|
|
97
115
|
rescue StandardError => err
|
98
116
|
error_handler&.call(err)
|
99
|
-
|
117
|
+
raise_json_rpc_error(:server_error)
|
100
118
|
end
|
101
119
|
|
102
120
|
@__ignoring_added_methods = false
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metarpc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1
|
4
|
+
version: 0.0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thesaurio Team
|
@@ -58,6 +58,20 @@ dependencies:
|
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
60
|
version: 0.63.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 0.12.2
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.12.2
|
61
75
|
description: Build a RPC API with ease
|
62
76
|
email: hello@thesaur.io
|
63
77
|
executables: []
|