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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/json_func.rb +13 -14
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0a63dc195f6c22a136762361dc556017eb6f8418
4
- data.tar.gz: 8e3078690d5d0510f650984017ec1524ced4b5b8
3
+ metadata.gz: 939b0be19a28747baad6c9f3be8f1fcb1a7a9317
4
+ data.tar.gz: ed7a97ee19c565d542308baf49e31db5b870b32e
5
5
  SHA512:
6
- metadata.gz: b0ec03c7a553f2e5094760a1f9c7baa8ddea0682d623a62b106db99eb5c00267ce9ee2300186b7a9859ce5129f76b6df2336621557eab9ef484916eb9f907261
7
- data.tar.gz: 2f60fdcfd076d5eeea4e9f02e68124e06d459caa5872c459a35246bab7bf42cf0f3dd75beb7eb7052c7fb3853eb1cc761a0666f558142ddff9c78eb570509c12
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
- raise BadRootObject unless initial_argument.is_a? Hash
17
- execute_hash(initial_argument)
16
+ parse_arg(initial_argument)
18
17
  end
19
18
 
20
- def execute_hash(hash)
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.keys.first
21
+ func = hash.first
24
22
  raise BadFunctionNameError.new(func) unless valid_function?(func)
25
- arg = hash.values.first
26
- handler.send(func, *parse_arg(arg))
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
- [arg]
39
+ arg
41
40
  end
42
41
  end
43
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-func
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hayden Faulds