jsonwalk 1.1.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
+ SHA1:
3
+ metadata.gz: 29153e1f6efa3ff4ef9880bcf90fc670a7f6596a
4
+ data.tar.gz: bc69962029c478dc73c44bda282d3083a7171ccc
5
+ SHA512:
6
+ metadata.gz: c5b98deaeea6ab16196f27b0811ac89d67f1e2a45f89f5f0691af8e9fe8f24086cde448f479dde939aed940f03f4b695a4a4b0173eabd7172e29be249cf40b2b
7
+ data.tar.gz: 5d698e1f8012c9ca01eec641102593e431d91ac8c6fbcde54c21f80876ae018562339a67da437624cbd31aa1c9834fbe3cbb7b10408481305482e55d51e331bd
data/.gemspec ADDED
@@ -0,0 +1 @@
1
+ ./jsonwalk.gemspec
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/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'jsonwalk'
4
+
5
+ # Specify your gem's dependencies in jsonwalk.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 TODO: Write your name
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,41 @@
1
+ # Jsonwalk
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/jsonwalk`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'jsonwalk'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install jsonwalk
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/PJO2/jsonwalk.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jsonwalk"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
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
Binary file
data/jsonwalk.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jsonwalk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jsonwalk"
8
+ spec.version = JsonWalk::VERSION
9
+ spec.authors = ["Philippe Jounin"]
10
+ spec.email = ["rubygem@jounin.net"]
11
+
12
+ spec.summary = %q{Recursively walk into a json structure}
13
+ spec.description = %q{Provides function to retrieve leaves values into a json tree. Selection and backward moves are provided. }
14
+ spec.homepage = ""
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ end
@@ -0,0 +1,3 @@
1
+ module JsonWalk
2
+ VERSION = "1.1.1"
3
+ end
data/lib/jsonwalk.rb ADDED
@@ -0,0 +1,103 @@
1
+ require "jsonwalk/version"
2
+
3
+ # ---------------------------------------------
4
+ # Module JsonWalk
5
+ # written by Ph. Jounin Sept 2016
6
+ # Released under GPL version 2
7
+ # ---------------------------------------------
8
+ # jsonwalk.rb
9
+ #
10
+ # walk through a json tree.
11
+ # This is slighlty different from classical tree parsing
12
+ # since json nodes are either hashes, arrays or leaves
13
+ # ---------------------------------------------
14
+
15
+ module JsonWalk
16
+
17
+ VERBOSE = false # traces ?
18
+
19
+ # return true if one member of nodes is descendant of the tree beginning by root
20
+ # probably useless outside the module...
21
+ def JsonWalk.IsAncestor( root, nodes )
22
+ case root
23
+ when Hash
24
+ root.each do |k, v|
25
+ return true if (nodes.include? v or IsAncestor(v, nodes))
26
+ end
27
+ when Array
28
+ root.each do |v|
29
+ return true if (nodes.include? v or IsAncestor(v, nodes))
30
+ end
31
+ else
32
+ return true if (nodes.include? root)
33
+ end
34
+ return false
35
+ end
36
+
37
+
38
+ # move forward in the tree, if node is a hash return on child
39
+ # if it is an array, return the set of children
40
+ def JsonWalk.forward(node, branch)
41
+ # move forward : get the uniq child or all of them
42
+ return node.kind_of?(Array) ?
43
+ node.flatten.map {|child| child.key?(branch) ? child[branch] : nil} :
44
+ node.key?(branch) ? node[branch] : nil
45
+ end
46
+
47
+ # just select a set of children matching a condition
48
+ # this does not change the level in the tree
49
+ def JsonWalk.select(node, branch, condition, operator)
50
+ # check manually, seems less dangerous than using eval
51
+ case operator
52
+ when '=='
53
+ return node.flatten.select {|child| child[branch]==condition}
54
+ when '=~'
55
+ return node.flatten.select {|child| /#{condition}/.match(child[branch])}
56
+ when '!='
57
+ return node.flatten.select {|child| child[branch]!=condition}
58
+ when '!~'
59
+ return node.flatten.select {|child| not /#{condition}/.match(child[branch])}
60
+ end
61
+ end
62
+
63
+ # move one step backward (but keep current selection)
64
+ def JsonWalk.backward(parent,node)
65
+ return parent.select { |child| IsAncestor(child, node) }
66
+ end
67
+
68
+ # walk the json tree : root is the top of the tree returned by json.parse
69
+ # path is a list of keys to child nodes with either selection by value
70
+ # or backward steps
71
+ def JsonWalk.walk(root, path)
72
+
73
+ levels = [] # keep track of how do we get to the current node
74
+ nodes = root
75
+
76
+ path.each do |child_key|
77
+ case child_key
78
+ when Integer
79
+ # just in case you want a fixed row
80
+ print "moving forward to index #{child_key}\n" if VERBOSE
81
+ nodes = (nodes.class==Array and child_key<nodes.count) ? nodes[child_key] : nil
82
+ when '..'
83
+ # move backwards, but keep only items which include a member of nodes in the sub tree
84
+ print "moving one step backward\n" if VERBOSE
85
+ nodes = backward(levels.pop, nodes) # levels.pop return the previous node
86
+ when /(==|=~|!=|!~)/
87
+ # this is a selection, regexp returns before, after and matching substrings
88
+ print "selecting nodes with key #{$`}, val #{$'}, cond #{$&}\n" if VERBOSE
89
+ nodes = select( nodes, $`, $', $& )
90
+ else
91
+ # move deeper in the tree
92
+ print "moving forward to key #{child_key}\n" if VERBOSE
93
+ levels.push nodes # keep track of current level
94
+ nodes = nodes==nil ? nil : forward( nodes, child_key)
95
+ end
96
+
97
+ end
98
+ return nodes
99
+ end
100
+
101
+ # module end
102
+ end
103
+
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsonwalk
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Philippe Jounin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-04-17 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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
+ description: 'Provides function to retrieve leaves values into a json tree. Selection
42
+ and backward moves are provided. '
43
+ email:
44
+ - rubygem@jounin.net
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gemspec"
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - jsonwalk-1.1.0.gem
58
+ - jsonwalk.gemspec
59
+ - lib/jsonwalk.rb
60
+ - lib/jsonwalk/version.rb
61
+ homepage: ''
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.6.6
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Recursively walk into a json structure
85
+ test_files: []