json_paths 0.0.1

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: 55d10e0128711f471a1b1c7216c20e8127cfe229
4
+ data.tar.gz: 8947019433897eb003b6a3f2493d0952acd7005d
5
+ SHA512:
6
+ metadata.gz: e3a1dec3d6081a13a6cde313a85e972726f43700287346e2affa199ef23932368ffd34909ab34b5e2f5f47cef5f2b0fa3e6eb9a6bc7b08871b330851c1550a95
7
+ data.tar.gz: 9836130b466f1927c227764228097d149b327203b73d08b08f2d78ec633f674786ab560553c414dd53240e200925bb666273a78939dcb9370965198d5a0e0a27
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in json_paths.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Matt Pruitt
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # JsonPaths
2
+
3
+ Generate a list of all [JSON paths](http://goessner.net/articles/JsonPath/) in a JSON document.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'json_paths'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install json_paths
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ json = '{"a": {"b": [1]}, "c": 2}'
23
+
24
+ JsonPaths.new(json).map { |path| path }
25
+ #=> ['$.a.b[0]', '$.a.c']
26
+ ```
27
+
28
+ ## Contributing
29
+
30
+ 1. Fork it ( http://github.com/<my-github-username>/json_paths/fork )
31
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
32
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
33
+ 4. Push to the branch (`git push origin my-new-feature`)
34
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'json_paths/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_paths"
8
+ spec.version = JsonPaths::VERSION
9
+ spec.authors = ["Matt Pruitt"]
10
+ spec.email = ["matt@guitsaru.com"]
11
+ spec.summary = %q{Gets all json paths from a json document.}
12
+ spec.description = %q{Gets all json paths from a json document.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ end
@@ -0,0 +1,3 @@
1
+ class JsonPaths
2
+ VERSION = "0.0.1"
3
+ end
data/lib/json_paths.rb ADDED
@@ -0,0 +1,51 @@
1
+ require "json_paths/version"
2
+ require 'json'
3
+
4
+ class JsonPaths
5
+ include Enumerable
6
+
7
+ attr_reader :json
8
+ attr_reader :paths
9
+
10
+ def initialize(json)
11
+ @json = json
12
+
13
+ @paths = json_to_paths
14
+ end
15
+
16
+ def each
17
+ paths.each
18
+ end
19
+
20
+ def json_to_paths
21
+ parsed_json = JSON.parse(json)
22
+ paths = []
23
+
24
+ parsed_json.each_key do |key|
25
+ paths << key_to_path(parsed_json, key).map { |k| "$." + k.to_s}
26
+ end
27
+
28
+ paths.flatten
29
+ end
30
+
31
+ def key_to_path(hash, key)
32
+ paths_for_key = []
33
+ value = hash[key]
34
+
35
+ if value.respond_to?(:keys)
36
+ value.each_key do |k|
37
+ paths_for_key << key_to_path(value, k).map { |ktp| [key.to_s, ktp].flatten }
38
+ end
39
+ elsif value.respond_to?(:each_with_index)
40
+ value.each_with_index do |v,i|
41
+ paths_for_key << JsonPaths.new(v.to_json).paths.map do |j|
42
+ "#{key}[#{i}].#{j.gsub('$.', '')}"
43
+ end
44
+ end
45
+ else
46
+ paths_for_key = [key.to_s]
47
+ end
48
+
49
+ Array(paths_for_key.map { |p| Array(p).join('.') })
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe JsonPaths do
4
+ let(:json) do
5
+ <<-EOF
6
+ {
7
+ "a": {
8
+ "b": {
9
+ "c": 1
10
+ },
11
+ "d": 2,
12
+ "e": [{"f": 3}]
13
+ }
14
+ }
15
+ EOF
16
+ end
17
+
18
+ subject { JsonPaths.new(json) }
19
+
20
+ specify { expect(subject.paths).to include("$.a.b.c") }
21
+ specify { expect(subject.paths).to include("$.a.d") }
22
+ specify { expect(subject.paths).to include("$.a.e[0].f") }
23
+ end
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+
3
+ Bundler.require :default, :test
4
+
5
+ $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'json_paths'
7
+
8
+ RSpec.configure do |config|
9
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_paths
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Matt Pruitt
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-01 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Gets all json paths from a json document.
56
+ email:
57
+ - matt@guitsaru.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - json_paths.gemspec
68
+ - lib/json_paths.rb
69
+ - lib/json_paths/version.rb
70
+ - spec/json_paths_spec.rb
71
+ - spec/spec_helper.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.2.0
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Gets all json paths from a json document.
96
+ test_files:
97
+ - spec/json_paths_spec.rb
98
+ - spec/spec_helper.rb