json_inspector 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
+ SHA1:
3
+ metadata.gz: 7e13d32d29cb18d7b34fc7b7aed41808aeb9fb73
4
+ data.tar.gz: 83a0584f3105420ce392705fa332b3e51ef068d2
5
+ SHA512:
6
+ metadata.gz: 81a91903947c976ffdf83e06cee3ec2524f623ddc7805ac93ba011632628fea81ff443d5a400fd5d5d24185d4e4a957c10e0e3f405250a1a7dd2941234dab153
7
+ data.tar.gz: 1018f9f2086dad1828ddfc9c3e6c37799a38037d12c1d0075fe2ca206cfeda1fbfb72f4c3ab992c51bef4953bcc0ac3ed301db1a9d8ae44aef191b185987c125
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_inspector.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 undr
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,43 @@
1
+ # Json Inspector
2
+
3
+ [![Build Status](https://travis-ci.org/undr/json_inspector.svg?branch=master)](https://travis-ci.org/undr/json_inspector)
4
+
5
+ Console tool for inspecting JSON.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'json_inspector', group: :development
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ ```
18
+ $ bundle
19
+ ```
20
+
21
+ Or install it yourself as:
22
+
23
+ ```
24
+ $ gem install json_inspector
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ Run `bundle exec inspect ./path/to/json/file` to load JSON file into inspector.
30
+
31
+ ## Development
32
+
33
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `bundle exec rake spec` to run the tests. Run `bundle exec inspect` to use the gem in this directory, ignoring other installed copies of this gem.
34
+
35
+ ## Contributing
36
+
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/undr/json_inspector.
38
+
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
43
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/exe/inspect ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json_inspector'
4
+
5
+ context = JsonInspector::Context.new(ARGV[0])
6
+ Pry.start(context, prompt: context._prompt_, output_prefix: '')
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'json_inspector'
4
+
5
+ context = JsonInspector::Context.new(ARGV[0])
6
+ Pry.start(context, prompt: context._prompt_, output_prefix: '')
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_inspector/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'json_inspector'
8
+ spec.version = JsonInspector::VERSION
9
+ spec.authors = ['undr']
10
+ spec.email = ["undr@yandex.ru"]
11
+
12
+ spec.summary = %q{Console tool for inspecting JSON.}
13
+ spec.description = %q{Console tool for inspecting JSON.}
14
+ spec.homepage = 'https://github.com/undr/json_inspector'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+
23
+ spec.add_dependency 'pry'
24
+ spec.add_dependency 'hashie'
25
+ spec.add_dependency 'multi_json'
26
+
27
+ spec.add_development_dependency 'bundler', "~> 1.11"
28
+ spec.add_development_dependency 'rake', "~> 10.0"
29
+ spec.add_development_dependency 'rspec', "~> 3.0"
30
+ end
@@ -0,0 +1,10 @@
1
+ require 'pry'
2
+ require 'hashie'
3
+ require 'multi_json'
4
+
5
+ require 'json_inspector/version'
6
+ require 'json_inspector/stack'
7
+ require 'json_inspector/context'
8
+
9
+ module JsonInspector
10
+ end
@@ -0,0 +1,129 @@
1
+ require 'pp'
2
+
3
+ module JsonInspector
4
+ class Context < BasicObject
5
+ attr_reader :filename, :options, :doc, :stack
6
+
7
+ OMITTED_VALUE = '< omitted >'.freeze
8
+
9
+ def initialize(filename, options = {})
10
+ @options = options
11
+ @filename = filename
12
+ @basename = ::File.basename(filename)
13
+
14
+ @doc = ::MultiJson.load(::File.read(filename))
15
+ @stack = Stack.new(doc)
16
+ end
17
+
18
+ def _prompt_
19
+ [
20
+ ->(obj, nest_level, _) { "inspect(#{@basename}:#{@stack.path})>" },
21
+ ->(obj, nest_level, _) { "inspect(#{@basename}:#{@stack.path})*" }
22
+ ]
23
+ end
24
+
25
+ def find_key(query)
26
+ enumerator('', ::Float::INFINITY).select { |key, _| key.split(?.).include?(query) }.map { |key, _| key }
27
+ end
28
+
29
+ alias :fk :find_key
30
+
31
+ def find(query)
32
+ enumerator('', ::Float::INFINITY).select { |key, value| value == query }.map { |key, _| key }
33
+ end
34
+
35
+ alias :f :find
36
+
37
+ def tree(*args)
38
+ raise ::ArgumentError.new("wrong number of arguments (given #{args.size}, expected 2)") if args.size > 2
39
+
40
+ initial_key = args.first.is_a?(::String) ? args.shift : ''
41
+ level = args.last.nil? ? ::Float::INFINITY : args.last.to_i
42
+
43
+ enumerator(initial_key, level).map { |key, value| key unless value.is_a?(::Enumerable) }.compact
44
+ end
45
+
46
+ alias :t :tree
47
+
48
+ def keys(selector = '')
49
+ value = @stack.current(selector)
50
+ get_keys(value)
51
+ end
52
+
53
+ alias :k :keys
54
+
55
+ def into(selector)
56
+ @stack.push(selector)
57
+ current
58
+ end
59
+
60
+ alias :i :into
61
+
62
+ def show(selector)
63
+ @stack.current(selector)
64
+ end
65
+
66
+ alias :s :show
67
+
68
+ def out
69
+ @stack.pop
70
+ current
71
+ end
72
+
73
+ alias :o :out
74
+
75
+ def reset
76
+ @stack.clear!
77
+ nil
78
+ end
79
+
80
+ alias :r :reset
81
+
82
+ def current
83
+ @stack.current
84
+ end
85
+
86
+ alias :c :current
87
+
88
+ private
89
+
90
+ def enumerator(initial_key, level)
91
+ ::Enumerator.new do |yielder|
92
+ recursive(initial_key, level, 0, yielder, @stack.current(initial_key))
93
+ end
94
+ end
95
+
96
+ def recursive(initial_key, level, current_level, yielder, value)
97
+ return unless value.is_a?(::Enumerable)
98
+
99
+ get_keys(value).each do |key|
100
+ new_value = value[key]
101
+ new_initial_key = [initial_key, key.to_s].reject(&:empty?).join('.')
102
+ new_current_level = current_level + 1
103
+
104
+ if new_current_level < level
105
+ yielder.yield(new_initial_key, new_value)
106
+ recursive(new_initial_key, level, new_current_level, yielder, new_value)
107
+ else
108
+ yielder.yield(*get_omitted_key_and_value(new_initial_key, new_value))
109
+ end
110
+ end
111
+ end
112
+
113
+ def get_keys(value)
114
+ case
115
+ when value.is_a?(::Array)
116
+ (0...value.size).to_a
117
+ when value.respond_to?(:keys)
118
+ value.keys
119
+ else
120
+ []
121
+ end
122
+ end
123
+
124
+ def get_omitted_key_and_value(initial_key, value)
125
+ return ["#{initial_key}...", OMITTED_VALUE] if value.is_a?(::Enumerable)
126
+ [initial_key, value]
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,34 @@
1
+ module JsonInspector
2
+ class Stack
3
+ attr_reader :doc
4
+
5
+ def initialize(doc)
6
+ @doc = doc
7
+ @keys = []
8
+ @doc.extend(Hashie::Extensions::DeepFind)
9
+ @doc.extend(Hashie::Extensions::DeepFetch)
10
+ end
11
+
12
+ def push(keys)
13
+ keys = keys.split(?.)
14
+ @keys += keys
15
+ end
16
+
17
+ def pop
18
+ @keys.pop
19
+ end
20
+
21
+ def path
22
+ @keys.join(?.)
23
+ end
24
+
25
+ def clear!
26
+ @keys = []
27
+ end
28
+
29
+ def current(keys = '')
30
+ keys = @keys + keys.split(?.)
31
+ @doc.deep_fetch(*keys) { nil }
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module JsonInspector
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - undr
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: multi_json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.11'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.11'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description: Console tool for inspecting JSON.
98
+ email:
99
+ - undr@yandex.ru
100
+ executables:
101
+ - inspect
102
+ - json_inspector
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - exe/inspect
114
+ - exe/json_inspector
115
+ - json_inspector.gemspec
116
+ - lib/json_inspector.rb
117
+ - lib/json_inspector/context.rb
118
+ - lib/json_inspector/stack.rb
119
+ - lib/json_inspector/version.rb
120
+ homepage: https://github.com/undr/json_inspector
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.5.1
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: Console tool for inspecting JSON.
144
+ test_files: []