json_truncate 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: dc0246cf913cdef7f3bfd498765aa29d6dc5712fbcb259fd46cc44f93724d273
4
+ data.tar.gz: db42da9845727e231270614a8666d1860df573446e17586effff62fbfb2d68be
5
+ SHA512:
6
+ metadata.gz: 57db2f96a4abc66ae1283ec571c3320c3df50a8a78077145b3e89b8e777d4ed68b7a4f25222011ce9d5eecacf7eb44be1bbd7e5a6debae468ab4ed9d6306e0bd
7
+ data.tar.gz: 9b4bac28065b56d104622bde4b4481b5674afbbbb9a860ea9a839c2ff77902f517d48806ccca8f481f3d8a9ce133ba1022727b8e636f7b5d2615e4f4c2fa4793
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ .vscode
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+
4
+ group :test do
5
+ gem 'rspec'
6
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_truncate (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.4.4)
10
+ rspec (3.10.0)
11
+ rspec-core (~> 3.10.0)
12
+ rspec-expectations (~> 3.10.0)
13
+ rspec-mocks (~> 3.10.0)
14
+ rspec-core (3.10.1)
15
+ rspec-support (~> 3.10.0)
16
+ rspec-expectations (3.10.1)
17
+ diff-lcs (>= 1.2.0, < 2.0)
18
+ rspec-support (~> 3.10.0)
19
+ rspec-mocks (3.10.2)
20
+ diff-lcs (>= 1.2.0, < 2.0)
21
+ rspec-support (~> 3.10.0)
22
+ rspec-support (3.10.2)
23
+
24
+ PLATFORMS
25
+ x86_64-linux
26
+
27
+ DEPENDENCIES
28
+ json_truncate!
29
+ rspec
30
+
31
+ BUNDLED WITH
32
+ 2.2.21
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Sean West <sean@digitalshift.ca>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # `json_truncate`
2
+
3
+ Simplify JSON object structures for logging purposes. Limit the number of array elements, depth of the tree or length of strings.
4
+
5
+ ```ruby
6
+ gem 'json_truncate'
7
+ ```
8
+
9
+ ```ruby
10
+ require 'json_truncate'
11
+
12
+ person = {
13
+ name: 'John Doe',
14
+ address: {
15
+ street: '123 ABC St.',
16
+ city: 'Truncatia',
17
+ },
18
+ image: '123456789012345678901234567890',
19
+ emails: [ 'jdoe@example.com', 'johnd@example.com', 'john.doe@example.com' ]
20
+ }
21
+
22
+ JsonTruncate.truncate(person,
23
+ max_depth: 2,
24
+ max_string_length: 20,
25
+ max_array_length: 2,
26
+ )
27
+
28
+ # {
29
+ # name: 'John Doe',
30
+ # address: {
31
+ # street: '123 ABC St.',
32
+ # city: 'Truncatia',
33
+ # latlong: [],
34
+ # },
35
+ # image: '12345678901234567890...',
36
+ # emails: [ 'jdoe@example.com', 'johnd@example.com', '...' ]
37
+ # }
38
+ ```
39
+
40
+ # Options
41
+
42
+ - `max_depth`: restrict the depth of the tree by pruning arrays and hashes.
43
+ - `max_string_length`: restrict the length of strings.
44
+ - `max_array_length`: restrict the number of items an array can contain.
45
+
46
+ All options are optional and can be used together.
47
+
48
+ # Motivation
49
+
50
+ There have been occassions where I just want the basics of a JSON structure and
51
+ not all the detail. I couldn't find an existing gem to do this so I threw this one
52
+ together.
@@ -0,0 +1,15 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'json_truncate/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'json_truncate'
6
+ s.license = 'MIT'
7
+ s.version = JsonTruncate::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Sean West']
10
+ s.email = 'sean@digitalshift.ca'
11
+ s.homepage = 'http://github.com/seanzo/json_truncate'
12
+ s.summary = %q{Simplify a JSON object for logging purposes.}
13
+ s.description = %q{Truncate any JSON values to a certain length. Truncate to a maximum depth. Truncate to a maximum number of array elements.}
14
+ s.files = `git ls-files`.split("\n")
15
+ end
@@ -0,0 +1,2 @@
1
+ require 'json_truncate/truncate'
2
+ require 'json_truncate/version'
@@ -0,0 +1,35 @@
1
+ module JsonTruncate
2
+ def self.truncate(object, opts = {})
3
+ process(object.dup, opts)
4
+ end
5
+
6
+ def self.process(object, opts, depth = 0)
7
+ depth += 1
8
+
9
+ if opts[:max_array_length] && object.is_a?(Array) && object.length > opts[:max_array_length]
10
+ object.slice!(opts[:max_array_length]..-1)
11
+ object.push('...')
12
+ end
13
+
14
+ if opts[:max_string_length] && object.is_a?(String) && object.length > opts[:max_string_length]
15
+ object.slice!(opts[:max_string_length]..-1)
16
+ object << '...'
17
+ end
18
+
19
+ if opts[:max_depth] && depth > opts[:max_depth] && [ Array, Hash ].include?(object.class)
20
+ object.delete_if { |_| true }
21
+ end
22
+
23
+ if object.is_a?(Hash)
24
+ object.each do |k,v|
25
+ process(v, opts, depth)
26
+ end
27
+ elsif object.is_a?(Array)
28
+ object.each do |v|
29
+ process(v, opts, depth)
30
+ end
31
+ end
32
+
33
+ object
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module JsonTruncate
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe JsonTruncate do
4
+ it "should remove nested objects" do
5
+ o = { foo: 'bar', bar: { baz: 'foo' }}
6
+
7
+ expect(JsonTruncate.truncate(
8
+ o,
9
+ max_depth: 1
10
+ )).to eql({ foo: 'bar', bar: {}})
11
+ end
12
+
13
+ it "should remove array items" do
14
+ o = { foo: [ 'foo', 'bar', 'baz' ], bar: { baz: [ 'foo', 'bar' ]}}
15
+
16
+ expect(JsonTruncate.truncate(
17
+ o,
18
+ max_array_length: 1
19
+ )).to eql({ foo: [ 'foo', '...' ], bar: { baz: [ 'foo', '...' ]}})
20
+ end
21
+
22
+ it "should truncate strings" do
23
+ o = { foo: '1234567890', bar: { baz: '1234567890', bat: [ '1234567890' ]}}
24
+
25
+ expect(JsonTruncate.truncate(
26
+ o,
27
+ max_string_length: 5
28
+ )).to eql({ foo: '12345...', bar: { baz: '12345...', 'bat': [ '12345...' ]}})
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+
3
+ require 'rubygems'
4
+ require 'json_truncate'
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_truncate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sean West
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Truncate any JSON values to a certain length. Truncate to a maximum depth.
14
+ Truncate to a maximum number of array elements.
15
+ email: sean@digitalshift.ca
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - Gemfile
22
+ - Gemfile.lock
23
+ - LICENSE
24
+ - README.md
25
+ - json_truncate.gemspec
26
+ - lib/json_truncate.rb
27
+ - lib/json_truncate/truncate.rb
28
+ - lib/json_truncate/version.rb
29
+ - spec/json_truncate/truncate_spec.rb
30
+ - spec/spec_helper.rb
31
+ homepage: http://github.com/seanzo/json_truncate
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 2.7.6.2
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: Simplify a JSON object for logging purposes.
55
+ test_files: []