Skip to content

Commit

Permalink
Fixed #57. (#58)
Browse files Browse the repository at this point in the history
* Fixed #57.

* Bumped version.
  • Loading branch information
Skarlso committed Jun 10, 2017
1 parent 021c1bc commit 38c5c9c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lib/jsonpath/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def parse(exp)
end

def parse_exp(exp)
exp = exp.gsub(/@/, '').gsub(/[\(\)]/, '')
exp = exp.gsub(/@/, '').gsub(/[\(\)]/, '').gsub(/"/, '\'').strip
scanner = StringScanner.new(exp)
elements = []
until scanner.eos?
Expand All @@ -34,9 +34,9 @@ def parse_exp(exp)
end
if t = scanner.scan(/\['\w+'\]+/)
elements << t.gsub(/\[|\]|'|\s+/, '')
elsif t = scanner.scan(/\s+[<>=][<>=]?\s+?/)
elsif t = scanner.scan(/(\s+)?[<>=][<>=]?(\s+)?/)
operator = t
elsif t = scanner.scan(/(\s+)?'?(\w+)?[.,]?(\w+)?'?(\s+)?/) # @TODO: At this point I should trim somewhere...
elsif t = scanner.scan(/(\s+)?'?.*'?(\s+)?/)
operand = t.delete("'").strip
elsif t = scanner.scan(/.*/)
raise "Could not process symbol: #{t}"
Expand All @@ -45,6 +45,7 @@ def parse_exp(exp)
el = dig(elements, @_current_node)
return false unless el
return true if operator.nil? && el

operand = operand.to_f if operand.to_i.to_s == operand || operand.to_f.to_s == operand
el.send(operator.strip, operand)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jsonpath/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class JsonPath
VERSION = '0.8.3'.freeze
VERSION = '0.8.4'.freeze
end
10 changes: 10 additions & 0 deletions test/test_jsonpath.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,16 @@ def test_support_underscore_in_member_names

def test_dig_return_string
assert_equal ['asdf'], JsonPath.new("$.store.book..tags[?(@ == 'asdf')]").on(@object)
assert_equal [], JsonPath.new("$.store.book..tags[?(@ == 'not_asdf')]").on(@object)
end

def test_slash_in_value
data = {
'data' => {
'type' => 'mps/awesome'
}
}
assert_equal [{ 'type' => 'mps/awesome' }], JsonPath.new("$.data[?(@.type == \"mps/awesome\")]").on(data)
end

def example_object
Expand Down

0 comments on commit 38c5c9c

Please sign in to comment.