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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4801f3c79cc372e71577b2a3b725c446b7c39021bd985be3ed6694bce8cdc888
4
- data.tar.gz: 860345a2846bb71d9da0e84b2c0caa3483236e113f16f27035d821b7fddd7fdc
3
+ metadata.gz: 12500570e62a718b69b1ba6fe10cfbdbca13979cc595746da329a18ea0f92894
4
+ data.tar.gz: 9a1e9dcecc32523bbe596f70a0582d369a06803a0233360fc9c03bd9ed644f63
5
5
  SHA512:
6
- metadata.gz: d082cfb79bbd83605f626c21c4d539b40c7494d37de2d0c364997fa79aaeef9bea98bbdec9de85332b79df7049c08735de27c3e84ab173c15c90a224f66facd8
7
- data.tar.gz: 605ee59e2cf8e772f6428f35b6108f6c735a16af8bfa3a9fd4daebce8a5005b7147d779836db50dccd0c7cc7bad7e62450834740fc10fc416d690e48b49b6917
6
+ metadata.gz: ea703da98688df194501db5ef50f1c25c6d374c21fdee4372d3cd260053fece80c911817144b892f3adb743b6c8919daeb110a38dc0c64f113cf8342dfa352c3
7
+ data.tar.gz: c3b12941618cbfc2f8f9ad74f7c767b03f9d3193c97ee74bc785b97172282ded21418d94de321dc2b92424e7072c2ebf42205eb18bd67547fd46b95a5d180ebe
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- json_reducer (0.1.0)
4
+ json_reducer (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # JsonReducer
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/json_reducer`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
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
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "json_reducer"
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 "irb"
13
+ require 'irb'
14
14
  IRB.start(__FILE__)
data/json_reducer.gemspec CHANGED
@@ -1,38 +1,38 @@
1
1
 
2
- lib = File.expand_path("../lib", __FILE__)
2
+ lib = File.expand_path('lib', __dir__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "json_reducer/version"
4
+ require 'json_reducer/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "json_reducer"
7
+ spec.name = 'json_reducer'
8
8
  spec.version = JsonReducer::VERSION
9
- spec.authors = ["Edis Veljacic"]
10
- spec.email = ["Veljacic@fidor.com"]
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/edis-veljacic'
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["allowed_push_host"] = "https://rubygems.org"
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
21
  else
22
- raise "RubyGems 2.0 or newer is required to protect against " \
23
- "public gem pushes."
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 = Dir.chdir(File.expand_path('..', __FILE__)) do
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 = "exe"
31
+ spec.bindir = 'exe'
32
32
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
- spec.require_paths = ["lib"]
33
+ spec.require_paths = ['lib']
34
34
 
35
- spec.add_development_dependency "bundler", "~> 1.16"
36
- spec.add_development_dependency "rake", "~> 10.0"
37
- spec.add_development_dependency "rspec", "~> 3.0"
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
@@ -1,7 +1,7 @@
1
1
  module JsonReducer
2
2
  class Mask
3
- def initialize(schema)
4
- @schema = parse_record(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
@@ -1,3 +1,3 @@
1
1
  module JsonReducer
2
- VERSION = "0.1.0"
2
+ VERSION = '1.1.0'.freeze
3
3
  end
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: 0.1.0
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-19 00:00:00.000000000 Z
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/edis-veljacic
78
+ homepage: https://github.com/veljacic/json_reducer
78
79
  licenses:
79
80
  - MIT
80
81
  metadata: