json_pretty_generate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 64bd62d114a4fa52979974f1321141d6fa75baecae332267d777fb0c5b5e5e4a
4
+ data.tar.gz: de25100652bc8884377f824c2ccd73f0b09746ec1407f0bfae73ff5400155e79
5
+ SHA512:
6
+ metadata.gz: a6d5cf4cd28d36c0f38ff8238a507bfa1da7304113fabb7da3eb1e73fdea075ebddac88ea4487b9b26bc8760eb18c0931289ba802e6e16f476ccc2c1f0c075b8
7
+ data.tar.gz: bd6044e841d2a638f2f2160d4de8e3034a54a9129bae18290d12842d46fbe9b13e5f7d97a3ce2e790629add67b93ee3259b440d684dd71091ce2bea9ff37b689
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+
4
+ Metrics:
5
+ Enabled: false
6
+
7
+ Layout/LineLength:
8
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.3
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rake'
6
+
7
+ group :development do
8
+ gem 'pry'
9
+ end
10
+
11
+ group :test do
12
+ gem 'rspec'
13
+ gem 'rubocop'
14
+ end
15
+
16
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,74 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_pretty_generate (0.1.0)
5
+ activesupport (~> 4.2.10)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (4.2.11.3)
11
+ i18n (~> 0.7)
12
+ minitest (~> 5.1)
13
+ thread_safe (~> 0.3, >= 0.3.4)
14
+ tzinfo (~> 1.1)
15
+ ast (2.4.2)
16
+ coderay (1.1.3)
17
+ concurrent-ruby (1.1.9)
18
+ diff-lcs (1.4.4)
19
+ i18n (0.9.5)
20
+ concurrent-ruby (~> 1.0)
21
+ method_source (1.0.0)
22
+ minitest (5.14.4)
23
+ parallel (1.20.1)
24
+ parser (3.0.1.1)
25
+ ast (~> 2.4.1)
26
+ pry (0.14.1)
27
+ coderay (~> 1.1)
28
+ method_source (~> 1.0)
29
+ rainbow (3.0.0)
30
+ rake (13.0.3)
31
+ regexp_parser (2.1.1)
32
+ rexml (3.2.5)
33
+ rspec (3.10.0)
34
+ rspec-core (~> 3.10.0)
35
+ rspec-expectations (~> 3.10.0)
36
+ rspec-mocks (~> 3.10.0)
37
+ rspec-core (3.10.1)
38
+ rspec-support (~> 3.10.0)
39
+ rspec-expectations (3.10.1)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.10.0)
42
+ rspec-mocks (3.10.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.10.0)
45
+ rspec-support (3.10.2)
46
+ rubocop (1.16.1)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.0.0.0)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml
52
+ rubocop-ast (>= 1.7.0, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 1.4.0, < 3.0)
55
+ rubocop-ast (1.7.0)
56
+ parser (>= 3.0.1.1)
57
+ ruby-progressbar (1.11.0)
58
+ thread_safe (0.3.6)
59
+ tzinfo (1.2.9)
60
+ thread_safe (~> 0.1)
61
+ unicode-display_width (2.0.0)
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ json_pretty_generate!
68
+ pry
69
+ rake
70
+ rspec
71
+ rubocop
72
+
73
+ BUNDLED WITH
74
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Nick Palaniuk
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # JSON Pretty Generate
2
+
3
+ Small utility method for pretty generating JSON
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'json_pretty_generate'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install json_pretty_generate
20
+
21
+ ## Usage
22
+
23
+ There is a single `::call` method.
24
+
25
+ ### Argument is a Hash
26
+
27
+ When the argument is a Hash JSON will be returned with line breaks
28
+ ```ruby
29
+ obj = { a: 1 }
30
+ JSONPrettyGenerate.(obj)
31
+ # => "{\n \"a\": 1\n}"
32
+ ```
33
+
34
+ ### Argument is a String
35
+
36
+ When the argument is JSON it returns JSON with line breaks
37
+ ```ruby
38
+ obj = "{\"a\":1}"
39
+ JSONPrettyGenerate.(obj)
40
+ # => "{\n \"a\": 1\n}"
41
+ ```
42
+
43
+ When the argument is a string that is not JSON it returns the argument
44
+ ```ruby
45
+ obj = "a=1"
46
+ JSONPrettyGenerate.(obj)
47
+ # => "a=1"
48
+ ```
49
+
50
+ ### Argument is not a String or a Hash
51
+
52
+ When the argument type is not a String or Hash it will return nil
53
+ ```ruby
54
+ obj = 1
55
+ JSONPrettyGenerate.(obj)
56
+ # => nil
57
+ ```
58
+
59
+ ### Filtering with a block
60
+
61
+ You can use a block to filter the obj to a subset of the data. Strings
62
+ or symbols can be passed to the block argument.
63
+ ```ruby
64
+ obj = { data: { body: { a: 1 } } }
65
+ JSONPrettyGenerate.(obj) { |o| o[:data][:body] }
66
+ # => "{\n \"a\": 1\n}"
67
+ ```
68
+
69
+ ## Development
70
+
71
+ To run the specs
72
+
73
+ ```bash
74
+ $ bundle exec rake
75
+ ```
76
+
77
+ ## Contributing
78
+
79
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nikkypx/json_pretty_generate.
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: %i[spec rubocop:auto_correct]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'json_pretty_generate'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/json_pretty_generate/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'json_pretty_generate'
7
+ spec.version = JSONPrettyGenerate::VERSION
8
+ spec.authors = ['Nicholas Palaniuk']
9
+ spec.email = ['npalaniuk+github@gmail.com']
10
+ spec.summary = 'Util for pretty printing JSON'
11
+ spec.description = 'Util for pretty printing JSON'
12
+ spec.homepage = 'https://github.com/nikkypx/json_pretty_generate'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+ spec.license = 'MIT'
15
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
16
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ end
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = spec.homepage
21
+
22
+ spec.require_paths = ['lib']
23
+ spec.add_dependency 'activesupport', '~> 4.2.10'
24
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json_pretty_generate/version'
4
+ require 'active_support/core_ext/hash'
5
+ require 'json'
6
+
7
+ module JSONPrettyGenerate
8
+ def self.call(obj)
9
+ return unless [Hash, String].include?(obj.class)
10
+
11
+ if obj.is_a? String
12
+ begin
13
+ obj = JSON.parse(obj)
14
+ rescue JSON::ParserError
15
+ return obj
16
+ end
17
+ end
18
+
19
+ obj = yield(obj.with_indifferent_access) if block_given?
20
+
21
+ JSON.pretty_generate(obj)
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module JSONPrettyGenerate
4
+ VERSION = '0.1.0'
5
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_pretty_generate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicholas Palaniuk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.10
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.10
27
+ description: Util for pretty printing JSON
28
+ email:
29
+ - npalaniuk+github@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".gitignore"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - ".travis.yml"
38
+ - Gemfile
39
+ - Gemfile.lock
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - bin/console
44
+ - bin/setup
45
+ - json_pretty_generate.gemspec
46
+ - lib/json_pretty_generate.rb
47
+ - lib/json_pretty_generate/version.rb
48
+ homepage: https://github.com/nikkypx/json_pretty_generate
49
+ licenses:
50
+ - MIT
51
+ metadata:
52
+ homepage_uri: https://github.com/nikkypx/json_pretty_generate
53
+ source_code_uri: https://github.com/nikkypx/json_pretty_generate
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 2.3.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.6
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Util for pretty printing JSON
73
+ test_files: []