pretty_json 1.0.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
+ SHA1:
3
+ metadata.gz: 08dddbd2036c9a8980cd73f21b3267af004415bc
4
+ data.tar.gz: 90f57dce86cfd1b0ca71378da97653d662ea8028
5
+ SHA512:
6
+ metadata.gz: a8212c730426faadd1df51b34714df26d13fe06cf2837b3ddbce81b778e20627fc62a77ba98e23303f48781e99cc3c4f6a756ba53b0688d5a16bb1e674ab9aea
7
+ data.tar.gz: 753ba6a778d42b54a0a6c1c89db2a6373fe1fff9b71ac291d82a54de7f392cf5755d2d7ffd19e87944514ea94b8f8835bbe11b98bcc4f7153cec2491d1ed8a6f
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.3
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 2.2.3
6
+ - 2.3.0
7
+ before_install: gem install bundler -v 1.11.2
8
+ script: bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pretty_json.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yves Komenda
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,69 @@
1
+ # PrettyJson
2
+
3
+ <a href="https://codeclimate.com/github/Dervol03/pretty_json"><img src="https://codeclimate.com/github/Dervol03/pretty_json/badges/gpa.svg" /></a> <a href="https://travis-ci.org/Dervol03/pretty_json"><img src="https://travis-ci.org/Dervol03/pretty_json.svg?branch=master" /></a>
4
+
5
+ This gem provides a simple interface to get pretty formatted JSON. It either can be used in a project or via the command line. When included in code, it provides the possibility to return the formatted JSON as string or to directly store it in a file. The latter option is not provided in the command line tool as you can refer to your favourite shell's file redirection for that purpose.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'pretty_json'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install pretty_json
22
+
23
+ ## Usage
24
+
25
+ ### In your own code
26
+
27
+ Simply require it wherever you which to use it:
28
+ ```ruby
29
+ require 'pretty_json'
30
+ ```
31
+
32
+ Afterwards, you have the `PrettyJSON` class at your disposal. You may pass a ruby Hash, String or a file name to the constructor.
33
+
34
+ ```ruby
35
+ json = PrettyJSON.new('{"key": "value", "array":[1,2,3]}')
36
+ json.to_s #=> pretty formatted JSON
37
+ json.to_file('/tmp/example.json') #=> writes pretty JSON to /tmp.example.json
38
+ ```
39
+
40
+ ### On the command line
41
+
42
+ If you only need quick formatting for some JSON, there are three possibilities to do so using the command line tool:
43
+ 1. save your JSON in a file and pass the file name to the command line tool
44
+ ```shell
45
+ pretty_json example.json
46
+ ```
47
+ 2. pass the JSON as a single line to the command line tool:
48
+ ```shell
49
+ pretty_json '{"key": "value", "array":[1,2,3]}'
50
+ ```
51
+ Beware the single quotes around the JSON string. If they are omitted, the shell interpreter will split the string as it thinks correct, which will result in invalid JSON in most cases
52
+ 3. you may pass multiline via STDIN using the `-` option
53
+ ```shell
54
+ pretty_json - > /tmp/example.json
55
+ {"key": "value",
56
+ "array":[1,2,3]}
57
+ ```
58
+ Here, the single quotes may be omitted. The example also shows how to write the result into a file (`/tmp/example.json`)instead of being printed to STDOUT.
59
+
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Dervol03/pretty_json.
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
69
+
data/bin/pretty_json ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'pretty_json'
4
+
5
+ def help
6
+ puts 'Usage:'
7
+ puts ' pretty_json [file_path|json_string]'
8
+ puts
9
+ puts '[Options]'
10
+ puts ' - read multiple lines from standard in (no single quotes needed).'
11
+ puts
12
+ puts 'You may specify a file path or a JSON string to be parsed and formatted.'
13
+ puts 'IMPORTANT: make sure to surround your JSON with single quotes to prevent the shell from splitting it by its own.'
14
+ exit 0
15
+ end
16
+
17
+
18
+ if %w(-h --help).include?(ARGV[0]) || ARGV.empty?
19
+ help
20
+ end
21
+
22
+ input = if ARGV.length == 1
23
+ if ARGV[0] == '-'
24
+ STDIN.read
25
+ else
26
+ ARGV[0]
27
+ end
28
+ else
29
+ ARGV.join
30
+ end
31
+
32
+ Dir.chdir(Dir.pwd) do
33
+ puts
34
+ puts
35
+ puts PrettyJSON.new(input).to_s
36
+ puts
37
+ end
@@ -0,0 +1,3 @@
1
+ class PrettyJSON
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+
3
+ require 'pretty_json/version'
4
+
5
+ # Represents a pretty JSON string.
6
+ class PrettyJSON
7
+
8
+ # @param [String] input either a file path or a JSON string.
9
+ def initialize(input)
10
+ json_hash = send("#{input.class.name.downcase}_to_hash", input)
11
+ @json = JSON.pretty_unparse(json_hash)
12
+ end
13
+
14
+
15
+ # @return [String] pretty formatted JSON.
16
+ def to_s
17
+ @json
18
+ end
19
+
20
+ # Writes pretty formatted JSON to the specified file and returns the same
21
+ # content as a string.
22
+ #
23
+ # @param [String] out_file to be written.
24
+ # @return [String] pretty formatted JSON.
25
+ def to_file(out_file)
26
+ File.write(out_file, @json)
27
+ @json
28
+ end
29
+
30
+
31
+ private
32
+
33
+ def hash_to_hash(hash)
34
+ hash
35
+ end
36
+
37
+
38
+ def string_to_hash(string)
39
+ json = File.exist?(string) ? File.read(string) : string
40
+ JSON.parse(json)
41
+ end
42
+
43
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pretty_json/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'pretty_json'
8
+ spec.version = PrettyJSON::VERSION
9
+ spec.authors = ['Yves Komenda']
10
+ spec.email = ['b_d_v@web.de']
11
+
12
+ spec.summary = %q{Simple command line tool to get some pretty JSON.}
13
+ spec.homepage = 'https://github.com/Dervol03/pretty_json'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = 'bin'
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.11'
22
+ spec.add_development_dependency 'rake', '~> 10.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.0'
24
+ spec.add_development_dependency 'pry', '~> 0.10'
25
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pretty_json
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yves Komenda
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description:
70
+ email:
71
+ - b_d_v@web.de
72
+ executables:
73
+ - pretty_json
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".ruby-version"
80
+ - ".travis.yml"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - bin/pretty_json
85
+ - lib/pretty_json.rb
86
+ - lib/pretty_json/version.rb
87
+ - pretty_json.gemspec
88
+ homepage: https://github.com/Dervol03/pretty_json
89
+ licenses:
90
+ - MIT
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.5.1
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Simple command line tool to get some pretty JSON.
112
+ test_files: []