json-func 0.0.2 → 0.0.3
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/json_func.rb +13 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 939b0be19a28747baad6c9f3be8f1fcb1a7a9317
|
4
|
+
data.tar.gz: ed7a97ee19c565d542308baf49e31db5b870b32e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5f3cad78c2d6646a6e535b29622aab462a261496dd2d59ffd9395af9d8d1e2c1c907f37e55fd0ef09ec1f6ebf468e1c1c9cbed4ab4cf86456a94a8c832312baa
|
7
|
+
data.tar.gz: 4737067ba99b146f7deda6e2febe72fc648a7a0856944803c35a632df49cb90d79353e8947d2370099ad563f67b0c7d9ea650f60282cc8afb7366e93de95a356
|
data/lib/json_func.rb
CHANGED
@@ -1,43 +1,42 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
3
|
class JsonFunc
|
4
|
-
class BadRootObject < RuntimeError; end;
|
5
|
-
class MultipleFunctionsError < RuntimeError; end;
|
6
4
|
class ZeroFunctionsError < RuntimeError; end;
|
7
5
|
class BadFunctionNameError < RuntimeError; end;
|
8
6
|
attr_reader(:handler)
|
9
7
|
|
8
|
+
class Function < Array; end;
|
9
|
+
|
10
10
|
def initialize(handler)
|
11
11
|
@handler = handler
|
12
12
|
end
|
13
13
|
|
14
14
|
def execute(json)
|
15
15
|
initial_argument = JSON.parse(json)
|
16
|
-
|
17
|
-
execute_hash(initial_argument)
|
16
|
+
parse_arg(initial_argument)
|
18
17
|
end
|
19
18
|
|
20
|
-
def
|
21
|
-
raise MultipleFunctionsError.new(hash.keys) if hash.size > 1
|
19
|
+
def execute_fuction(hash)
|
22
20
|
raise ZeroFunctionsError.new(hash.keys) if hash.size == 0
|
23
|
-
func = hash.
|
21
|
+
func = hash.first
|
24
22
|
raise BadFunctionNameError.new(func) unless valid_function?(func)
|
25
|
-
|
26
|
-
|
23
|
+
args = hash[1..-1].map do |arg|
|
24
|
+
parse_arg(arg)
|
25
|
+
end
|
26
|
+
return args if func == 'list'
|
27
|
+
handler.send(func, *args)
|
27
28
|
end
|
28
29
|
|
29
30
|
def valid_function?(func)
|
30
|
-
(handler.public_methods - Object.new.public_methods).map(&:to_s).include? func
|
31
|
+
(handler.public_methods - Object.new.public_methods + ['list']).map(&:to_s).include? func
|
31
32
|
end
|
32
33
|
|
33
34
|
def parse_arg(arg)
|
34
35
|
case arg
|
35
|
-
when Hash
|
36
|
-
execute_hash(arg)
|
37
36
|
when Array
|
38
|
-
arg
|
37
|
+
execute_fuction(arg)
|
39
38
|
else
|
40
|
-
|
39
|
+
arg
|
41
40
|
end
|
42
41
|
end
|
43
42
|
end
|