Skip to content

Commit

Permalink
A lot of refactoring and addresses pull #39 and #41. (#46)
Browse files Browse the repository at this point in the history
* A lot of refactoring and addresses pull #39 and #41.

* Closes #44 and Closes #30.

* Bumped version.

* Updated readme.
  • Loading branch information
Skarlso authored May 2, 2017
1 parent e4474e0 commit a605269
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 222 deletions.
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
rvm:
- 1.9.2
- 1.9.3
- 2.0.0
- 2.1.6
- 2.3.1
- jruby
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Joshua Lin & Gergely Brautigam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ enum.any?{ |c| c == 'red' }

You can optionally prevent eval from being called on sub-expressions by passing in :allow_eval => false to the constructor.

### More examples

For more usage examples and variations on paths, please visit the tests. There are some more complex ones as well.

### Manipulation

If you'd like to do substitution in a json object, you can use `#gsub` or `#gsub!` to modify the object in place.
Expand All @@ -130,3 +134,7 @@ o = JsonPath.for(json).
to_hash
# => {"candy" => "big turks"}
~~~~~

# Contributions

Please feel free to submit an Issue or a Pull Request any time you feel like you would like to contribute. Thank you!
7 changes: 2 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ require 'bundler'
Bundler::GemHelper.install_tasks

task :test do
$: << 'lib'
require 'minitest/autorun'
require 'phocus'
require 'jsonpath'
$LOAD_PATH << 'lib'
Dir['./test/**/test_*.rb'].each { |test| require test }
end

task :default => :test
task default: :test
2 changes: 1 addition & 1 deletion bin/jsonpath
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ usage unless ARGV[0]

jsonpath = JsonPath.new(ARGV[0])
case ARGV[1]
when nil #stdin
when nil # stdin
puts MultiJson.encode(jsonpath.on(MultiJson.decode(STDIN.read)))
when String
puts MultiJson.encode(jsonpath.on(MultiJson.decode(File.exist?(ARGV[1]) ? File.read(ARGV[1]) : ARGV[1])))
Expand Down
23 changes: 12 additions & 11 deletions jsonpath.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@ require File.join(File.dirname(__FILE__), 'lib', 'jsonpath', 'version')
Gem::Specification.new do |s|
s.name = 'jsonpath'
s.version = JsonPath::VERSION
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Joshua Hull"]
s.summary = "Ruby implementation of http://goessner.net/articles/JsonPath/"
s.description = "Ruby implementation of http://goessner.net/articles/JsonPath/."
s.email = %q{joshbuddy@gmail.com}
s.required_rubygems_version =
Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
s.authors = ['Joshua Hull', 'Gergely Brautigam']
s.summary = 'Ruby implementation of http://goessner.net/articles/JsonPath/'
s.description = 'Ruby implementation of http://goessner.net/articles/JsonPath/.'
s.email = ['joshbuddy@gmail.com', 'skarlso777@gmail.com']
s.extra_rdoc_files = ['README.md']
s.files = `git ls-files`.split("\n")
s.homepage = %q{http://github.com/joshbuddy/jsonpath}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.test_files = `git ls-files`.split("\n").select{|f| f =~ /^spec/}
s.homepage = 'https://github.com/joshbuddy/jsonpath'
s.rdoc_options = ['--charset=UTF-8']
s.require_paths = ['lib']
s.rubygems_version = '1.3.7'
s.test_files = `git ls-files`.split("\n").select { |f| f =~ /^spec/ }
s.rubyforge_project = 'jsonpath'
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.licenses = ['MIT']

# dependencies
Expand Down
57 changes: 29 additions & 28 deletions lib/jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,30 @@
require 'jsonpath/enumerable'
require 'jsonpath/version'

# JsonPath: initializes the class with a given JsonPath and parses that path
# into a token array.
class JsonPath

PATH_ALL = '$..*'
PATH_ALL = '$..*'.freeze

attr_accessor :path

def initialize(path, opts = nil)
@opts = opts
scanner = StringScanner.new(path)
@path = []
while not scanner.eos?
if token = scanner.scan(/\$/)
@path << token
elsif token = scanner.scan(/@/)
until scanner.eos?
if token = scanner.scan(/\$|@\B|\*|\.\./)
@path << token
elsif token = scanner.scan(/[a-zA-Z0-9_-]+/)
elsif token = scanner.scan(/[\$@a-zA-Z0-9:_-]+/)
@path << "['#{token}']"
elsif token = scanner.scan(/'(.*?)'/)
@path << "[#{token}]"
elsif token = scanner.scan(/\[/)
count = 1
while !count.zero?
if t = scanner.scan(/\[/)
token << t
count += 1
elsif t = scanner.scan(/\]/)
token << t
count -= 1
elsif t = scanner.scan(/[^\[\]]+/)
token << t
elsif scanner.eos?
raise ArgumentError, 'unclosed bracket'
end
end
@path << token
@path << find_matching_brackets(token, scanner)
elsif token = scanner.scan(/\]/)
raise ArgumentError, 'unmatched closing bracket'
elsif token = scanner.scan(/\.\./)
@path << token
elsif scanner.scan(/\./)
nil
elsif token = scanner.scan(/\*/)
@path << token
elsif token = scanner.scan(/[><=] \d+/)
@path.last << token
elsif token = scanner.scan(/./)
Expand All @@ -55,6 +36,24 @@ def initialize(path, opts = nil)
end
end

def find_matching_brackets(token, scanner)
count = 1
until count.zero?
if t = scanner.scan(/\[/)
token << t
count += 1
elsif t = scanner.scan(/\]/)
token << t
count -= 1
elsif t = scanner.scan(/[^\[\]]+/)
token << t
elsif scanner.eos?
raise ArgumentError, 'unclosed bracket'
end
end
token
end

def join(join_path)
res = deep_clone
res.path += JsonPath.new(join_path).path
Expand All @@ -70,19 +69,21 @@ def first(obj_or_str, *args)
end

def enum_on(obj_or_str, mode = nil)
JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode, @opts)
JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode,
@opts)
end
alias_method :[], :enum_on

def self.on(obj_or_str, path, opts = nil)
self.new(path, opts).on(process_object(obj_or_str))
new(path, opts).on(process_object(obj_or_str))
end

def self.for(obj_or_str)
Proxy.new(process_object(obj_or_str))
end

private

def self.process_object(obj_or_str)
obj_or_str.is_a?(String) ? MultiJson.decode(obj_or_str) : obj_or_str
end
Expand Down
Loading

0 comments on commit a605269

Please sign in to comment.