json_reducer 0.1.0 → 1.1.0
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/Gemfile +2 -2
- data/Gemfile.lock +1 -1
- data/README.md +107 -4
- data/Rakefile +3 -3
- data/bin/console +3 -3
- data/json_reducer.gemspec +15 -15
- data/lib/json_reducer/mask.rb +2 -3
- data/lib/json_reducer/schemas.rb +24 -0
- data/lib/json_reducer/version.rb +1 -1
- data/lib/json_reducer.rb +10 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 12500570e62a718b69b1ba6fe10cfbdbca13979cc595746da329a18ea0f92894
|
4
|
+
data.tar.gz: 9a1e9dcecc32523bbe596f70a0582d369a06803a0233360fc9c03bd9ed644f63
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea703da98688df194501db5ef50f1c25c6d374c21fdee4372d3cd260053fece80c911817144b892f3adb743b6c8919daeb110a38dc0c64f113cf8342dfa352c3
|
7
|
+
data.tar.gz: c3b12941618cbfc2f8f9ad74f7c767b03f9d3193c97ee74bc785b97172282ded21418d94de321dc2b92424e7072c2ebf42205eb18bd67547fd46b95a5d180ebe
|
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
|
-
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
|
3
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
4
|
|
5
5
|
# Specify your gem's dependencies in json_reducer.gemspec
|
6
6
|
gemspec
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# JsonReducer
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Reduces hash based on given schema. If you want to render just one part of the hash this gem will make that easy. Just create JSON schema for desired response and gem will do the rest.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -22,7 +20,112 @@ Or install it yourself as:
|
|
22
20
|
|
23
21
|
## Usage
|
24
22
|
|
25
|
-
|
23
|
+
First create an initializer inside initializers folder. You can call it json_reducer but that is up to you.
|
24
|
+
Here you can define your base_path to schemas and register them.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
JsonReducer.base_path(Rails.root.join('lib', 'schemas')) # don't forget to create schemas folder
|
28
|
+
|
29
|
+
JsonReducer.register(:example1, 'example1.json')
|
30
|
+
JsonReducer.register(:example2, { foo: {bar: {title: 'BAR'} } }, file: false)
|
31
|
+
JsonReducer.register(:example3, {"foo": {"bar": {"title": "BAR"}}}, file: false)
|
32
|
+
```
|
33
|
+
|
34
|
+
You can pass filename, json or hash. Just set file option to false for hash or json.
|
35
|
+
When you want to register schema using path pass schema filename. In our example schema source is in: '/lib/schemas/example1.json'
|
36
|
+
|
37
|
+
JSON schema example:
|
38
|
+
|
39
|
+
```json
|
40
|
+
{
|
41
|
+
"type": "object",
|
42
|
+
"properties": {
|
43
|
+
"foo": {
|
44
|
+
"type": "object",
|
45
|
+
"properties": {
|
46
|
+
"bar": {"type": "object"}
|
47
|
+
}
|
48
|
+
},
|
49
|
+
"abc": {
|
50
|
+
"type": "array",
|
51
|
+
"properties": {
|
52
|
+
"id": { "type" : "string" }
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
In JSON schema you are just whitelisting the properties which you want to use. If you exclude field in properties it will be excluded in response
|
60
|
+
but if you completely omit properties in JSON schema all object fields for that property object will be parsed.
|
61
|
+
It is important to set type correctly, especially for arrays otherwise hash won't be parsed correctly.
|
62
|
+
|
63
|
+
## Example
|
64
|
+
|
65
|
+
```
|
66
|
+
payload = {
|
67
|
+
foo: {
|
68
|
+
bar: {
|
69
|
+
title: 'BAR',
|
70
|
+
body: 'Body of the BAR'
|
71
|
+
},
|
72
|
+
baz: 'BAZ'
|
73
|
+
},
|
74
|
+
abc: {
|
75
|
+
def: 'DEF'
|
76
|
+
},
|
77
|
+
dbc: {
|
78
|
+
fed: 'FED'
|
79
|
+
}
|
80
|
+
}
|
81
|
+
```
|
82
|
+
|
83
|
+
```json
|
84
|
+
schema = {
|
85
|
+
"type": "object",
|
86
|
+
"properties": {
|
87
|
+
"foo": {
|
88
|
+
"type": "object",
|
89
|
+
"properties": {
|
90
|
+
"bar": {
|
91
|
+
"type": "object",
|
92
|
+
"properties": {
|
93
|
+
"title": { "type": "string" }
|
94
|
+
/* omitting body to exclude it from response */
|
95
|
+
}
|
96
|
+
}
|
97
|
+
}
|
98
|
+
},
|
99
|
+
"dbc": {
|
100
|
+
"type": "object"
|
101
|
+
/* omitting properties to parse all */
|
102
|
+
}
|
103
|
+
}
|
104
|
+
}
|
105
|
+
```
|
106
|
+
|
107
|
+
Registering the schema and applying it on given payload.
|
108
|
+
|
109
|
+
```
|
110
|
+
JsonReducer.register(:test, schema, file: false)
|
111
|
+
JsonReducer.new(:test).apply(payload)
|
112
|
+
```
|
113
|
+
|
114
|
+
Will result with
|
115
|
+
|
116
|
+
```
|
117
|
+
payload = {
|
118
|
+
'foo' => {
|
119
|
+
'bar' => {
|
120
|
+
'title' => 'BAR'
|
121
|
+
}
|
122
|
+
},
|
123
|
+
'dbc' => {
|
124
|
+
'fed' => 'FED'
|
125
|
+
}
|
126
|
+
}
|
127
|
+
|
128
|
+
```
|
26
129
|
|
27
130
|
## Development
|
28
131
|
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
|
-
require
|
4
|
-
require
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'json_reducer'
|
5
5
|
|
6
6
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
7
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +10,5 @@ require "json_reducer"
|
|
10
10
|
# require "pry"
|
11
11
|
# Pry.start
|
12
12
|
|
13
|
-
require
|
13
|
+
require 'irb'
|
14
14
|
IRB.start(__FILE__)
|
data/json_reducer.gemspec
CHANGED
@@ -1,38 +1,38 @@
|
|
1
1
|
|
2
|
-
lib = File.expand_path(
|
2
|
+
lib = File.expand_path('lib', __dir__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
4
|
+
require 'json_reducer/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'json_reducer'
|
8
8
|
spec.version = JsonReducer::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Edis Veljacic']
|
10
|
+
spec.email = ['Veljacic@fidor.com']
|
11
11
|
|
12
12
|
spec.summary = 'Hash reducer'
|
13
13
|
spec.description = 'Reduces hash based on given JSON schema.'
|
14
|
-
spec.homepage = 'https://github.com/
|
14
|
+
spec.homepage = 'https://github.com/veljacic/json_reducer'
|
15
15
|
spec.license = 'MIT'
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
18
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
19
|
if spec.respond_to?(:metadata)
|
20
|
-
spec.metadata[
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
21
|
else
|
22
|
-
raise
|
23
|
-
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
23
|
+
'public gem pushes.'
|
24
24
|
end
|
25
25
|
|
26
26
|
# Specify which files should be added to the gem when it is released.
|
27
27
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
28
|
-
spec.files
|
28
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
29
29
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
30
30
|
end
|
31
|
-
spec.bindir =
|
31
|
+
spec.bindir = 'exe'
|
32
32
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
33
|
-
spec.require_paths = [
|
33
|
+
spec.require_paths = ['lib']
|
34
34
|
|
35
|
-
spec.add_development_dependency
|
36
|
-
spec.add_development_dependency
|
37
|
-
spec.add_development_dependency
|
35
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
36
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
37
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
38
38
|
end
|
data/lib/json_reducer/mask.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module JsonReducer
|
2
2
|
class Mask
|
3
|
-
def initialize(
|
4
|
-
@schema =
|
3
|
+
def initialize(key)
|
4
|
+
@schema = JsonReducer::Schemas.instance.get(key)
|
5
5
|
end
|
6
6
|
|
7
7
|
def apply(payload)
|
@@ -16,7 +16,6 @@ module JsonReducer
|
|
16
16
|
def apply!(payload, schema)
|
17
17
|
return if schema.dig('properties').nil?
|
18
18
|
|
19
|
-
# sliced = payload.slice!(*schema['properties'].keys)
|
20
19
|
sliced = slice!(payload, schema['properties'].keys)
|
21
20
|
handle(schema['properties'], sliced)
|
22
21
|
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
module JsonReducer
|
5
|
+
class Schemas
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
attr_accessor :base_path
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
@schemas = {}
|
12
|
+
@base_path = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def get(key)
|
16
|
+
@schemas[key]
|
17
|
+
end
|
18
|
+
|
19
|
+
def set(key, schema, file)
|
20
|
+
value = file ? File.read("#{@base_path}/#{schema}") : schema
|
21
|
+
@schemas[key] = value.is_a?(String) ? JSON.parse(value) : value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/json_reducer/version.rb
CHANGED
data/lib/json_reducer.rb
CHANGED
@@ -1,8 +1,18 @@
|
|
1
1
|
require 'json_reducer/version'
|
2
|
+
|
2
3
|
require 'json_reducer/mask'
|
4
|
+
require 'json_reducer/schemas'
|
3
5
|
|
4
6
|
module JsonReducer
|
5
7
|
def self.new(*args)
|
6
8
|
Mask.new(*args)
|
7
9
|
end
|
10
|
+
|
11
|
+
def self.register(key, schema, file: true)
|
12
|
+
JsonReducer::Schemas.instance.set(key, schema, file)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.base_path(path)
|
16
|
+
JsonReducer::Schemas.instance.base_path = path
|
17
|
+
end
|
8
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_reducer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edis Veljacic
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-09-
|
11
|
+
date: 2018-09-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -73,8 +73,9 @@ files:
|
|
73
73
|
- json_reducer.gemspec
|
74
74
|
- lib/json_reducer.rb
|
75
75
|
- lib/json_reducer/mask.rb
|
76
|
+
- lib/json_reducer/schemas.rb
|
76
77
|
- lib/json_reducer/version.rb
|
77
|
-
homepage: https://github.com/
|
78
|
+
homepage: https://github.com/veljacic/json_reducer
|
78
79
|
licenses:
|
79
80
|
- MIT
|
80
81
|
metadata:
|