json-func 0.0.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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/json_func.rb +43 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 95a9fbda904f188f22f1b0f960a441af96271764
4
+ data.tar.gz: a7125b01b928a20cf3008cc358f970fe7162f34e
5
+ SHA512:
6
+ metadata.gz: 2a44d310104be5144911f48a1c81c2e5700886381d1aa6643518da807fa60fbddca9a7472d0c82dd7145854d5195db96d260752a05ed57714b97e73298324e68
7
+ data.tar.gz: 40d96c7ca0013ccf53a491a10352bc6417a5d370d2f2e2243e4cc23331590bb5eff8b0986d323ccaf2c23361628b9fc6636d0d67b7318691ec52aaec9431a115
data/lib/json_func.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+
3
+ class JsonFunc
4
+ class BadRootObject < RuntimeError; end;
5
+ class MultipleFunctionsError < RuntimeError; end;
6
+ class ZeroFunctionsError < RuntimeError; end;
7
+ class BadFunctionNameError < RuntimeError; end;
8
+ attr_reader(:handler)
9
+
10
+ def initialize(handler)
11
+ @handler = handler
12
+ end
13
+
14
+ def execute(json)
15
+ initial_argument = JSON.parse(json)
16
+ raise BadRootObject unless initial_argument.is_a? Hash
17
+ execute_hash(initial_argument)
18
+ end
19
+
20
+ def execute_hash(hash)
21
+ raise MultipleFunctionsError.new(hash.keys) if hash.size > 1
22
+ raise ZeroFunctionsError.new(hash.keys) if hash.size == 0
23
+ func = hash.keys.first
24
+ raise BadFunctionNameError.new(func) unless valid_function?(func)
25
+ arg = hash.values.first
26
+ handler.send(func, *parse_arg(arg))
27
+ end
28
+
29
+ def valid_function?(func)
30
+ (handler.public_methods - Object.new.public_methods).include? func
31
+ end
32
+
33
+ def parse_arg(arg)
34
+ case arg
35
+ when Hash
36
+ execute_hash(arg)
37
+ when Array
38
+ arg
39
+ else
40
+ [arg]
41
+ end
42
+ end
43
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json-func
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hayden Faulds
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-03 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Use to execute function chains persisted in json
14
+ email: fauldsh@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/json_func.rb
20
+ homepage: http://www.github.com/hfaulds/ruby-json-func
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.0
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Execute function chains stored in json
44
+ test_files: []