jsonpath 0.5.1 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
data/bin/jsonpath CHANGED
@@ -16,9 +16,7 @@ usage unless ARGV[0]
16
16
  jsonpath = JsonPath.new(ARGV[0])
17
17
  case ARGV[1]
18
18
  when nil #stdin
19
- until STDIN.eof?
20
- puts MultiJson.encode(jsonpath.on(MultiJson.decode(STDIN.readline)))
21
- end
19
+ puts MultiJson.encode(jsonpath.on(MultiJson.decode(STDIN.read)))
22
20
  when String
23
21
  puts MultiJson.encode(jsonpath.on(MultiJson.decode(File.exist?(ARGV[1]) ? File.read(ARGV[1]) : ARGV[1])))
24
- end
22
+ end
data/lib/jsonpath.rb CHANGED
@@ -20,7 +20,7 @@ class JsonPath
20
20
  @path << token
21
21
  elsif token = scanner.scan(/@/)
22
22
  @path << token
23
- elsif token = scanner.scan(/[a-zA-Z0-9_]+/)
23
+ elsif token = scanner.scan(/[a-zA-Z0-9_-]+/)
24
24
  @path << "['#{token}']"
25
25
  elsif token = scanner.scan(/'(.*?)'/)
26
26
  @path << "[#{token}]"
@@ -99,10 +99,15 @@ class JsonPath
99
99
  if exp.nil?
100
100
  default
101
101
  elsif exp[0] == ?(
102
- #p @_current_node
103
- #p exp.gsub(/@/, '@_current_node')
104
- #p eval(exp.gsub(/@/, '@_current_node'))
105
- allow_eval? ? @_current_node && eval(exp.gsub(/@/, '@_current_node')) : nil
102
+ return nil unless allow_eval? && @_current_node
103
+ match_result = /@\.(\p{Word}+)/.match(exp) || []
104
+ identifier = match_result[1]
105
+ # if there's no such method - convert into hash subscript
106
+ if !identifier.nil? && !@_current_node.methods.include?(identifier.to_sym)
107
+ return eval(exp.gsub(/@/, '@_current_node').gsub(/.#{identifier}/,"['#{identifier}']"))
108
+ end
109
+ # otherwise eval as is
110
+ eval(exp.gsub(/@/, '@_current_node'))
106
111
  elsif exp.empty?
107
112
  default
108
113
  else
@@ -1,3 +1,3 @@
1
1
  class JsonPath
2
- VERSION = '0.5.1'
2
+ VERSION = '0.5.2'
3
3
  end
@@ -62,6 +62,10 @@ class TestJsonpath < MiniTest::Unit::TestCase
62
62
  assert_equal [@object['store']['bicycle']['catalogue_number']], JsonPath.new('$.store.bicycle.catalogue_number').on(@object)
63
63
  end
64
64
 
65
+ def test_path_with_hyphens
66
+ assert_equal [@object['store']['bicycle']['single-speed']], JsonPath.new('$.store.bicycle.single-speed').on(@object)
67
+ end
68
+
65
69
  def test_paths_with_numbers
66
70
  assert_equal [@object['store']['bicycle']['2seater']], JsonPath.new('$.store.bicycle.2seater').on(@object)
67
71
  end
@@ -75,7 +79,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
75
79
  end
76
80
 
77
81
  def test_counting
78
- assert_equal 30, JsonPath.new('$..*').on(@object).to_a.size
82
+ assert_equal 31, JsonPath.new('$..*').on(@object).to_a.size
79
83
  end
80
84
 
81
85
  def test_space_in_path
@@ -126,6 +130,10 @@ class TestJsonpath < MiniTest::Unit::TestCase
126
130
  assert_equal @object['store']['book'].collect{|e| e['price']}, JsonPath.on(@object, '$..book[*].price')
127
131
  end
128
132
 
133
+ def test_support_filter_by_childnode_value
134
+ assert_equal [@object['store']['book'][3]], JsonPath.new("$..book[?(@.price > 20)]").on(@object)
135
+ end
136
+
129
137
  def example_object
130
138
  { "store"=> {
131
139
  "book" => [
@@ -156,6 +164,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
156
164
  "color"=> "red",
157
165
  "price"=> 20,
158
166
  "catalogue_number" => 12345,
167
+ "single-speed" => "no",
159
168
  "2seater" => "yes"}
160
169
  } }
161
170
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.1
4
+ version: 0.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-03 00:00:00.000000000 Z
12
+ date: 2013-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
@@ -145,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
145
145
  version: '0'
146
146
  segments:
147
147
  - 0
148
- hash: -4414844720981211301
148
+ hash: 2086024960613292474
149
149
  required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  none: false
151
151
  requirements: