jsonpath 0.5.7 → 0.5.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 127831bc4a7219000b806933626b26c2b9b80bc6
4
- data.tar.gz: bd1962f8d0359b92bef9d0cf907c313ff0178089
3
+ metadata.gz: 0605409a770fbb28dd1352fa92753e7707d0df96
4
+ data.tar.gz: ed5e1e0c2fc22721d9d9a71ca5ec97f15e07bfe5
5
5
  SHA512:
6
- metadata.gz: ff10144fa37514160843c0e62f4a0775795cf18705805cc0beff2ea6b24178d80e22cbd4b486deda1c6d65a4afa1027c1c501797a6bb3f8cd56e5ff34b547d3d
7
- data.tar.gz: d1fe3eda2c940d8f047322cec32dcb0a0777a2fe6c90257c6ff07b4fbeba89181cdbf39b2cd169dc9e6aa60c510884d04d4c0ef05b3629884db7d695494573a8
6
+ metadata.gz: d3471d1c6ffdd79d987edf7772c945b2c5c74cd614086cc49e57588d47c6135310fd9ca6660c0e5e711521c9863b189de1067c8656165d46bf266eb2522a9491
7
+ data.tar.gz: 091adbef8dc29e30172d0016cf67e6035af094426fd405a45fff52e0292b45340f1aab1804b5291f918b0916fe2f02b252cb8fa2d97e2ee13dcd330645ac9cf8
@@ -104,19 +104,27 @@ class JsonPath
104
104
  default
105
105
  elsif exp[0] == ?(
106
106
  return nil unless allow_eval? && @_current_node
107
- match_result = /@\.(\p{Word}+)/.match(exp) || []
108
- identifier = match_result[1]
109
- # if there's no such method - convert into hash subscript
110
- if !identifier.nil? && !@_current_node.methods.include?(identifier.to_sym)
111
- exp_to_eval = exp.gsub(/@/, '@_current_node').gsub(/@_current_node.#{identifier}/,"@_current_node['#{identifier}']")
107
+ identifiers = /@?(\.(\w+))+/.match(exp)
108
+
109
+ if !identifiers.nil? && !@_current_node.methods.include?(identifiers[2].to_sym)
110
+ exp_to_eval = exp.dup
111
+ exp_to_eval[identifiers[0]] = identifiers[0].split('.').map{|el| el == '@' ? '@_current_node' : "['#{el}']"}.join
112
112
  begin
113
113
  return eval(exp_to_eval)
114
114
  rescue StandardError # if eval failed because of bad arguments or missing methods
115
115
  return default
116
116
  end
117
117
  end
118
+
118
119
  # otherwise eval as is
119
- eval(exp.gsub(/@/, '@_current_node'))
120
+ # TODO: this eval is wrong, because hash accessor could be nil and nil cannot be compared with anything,
121
+ # for instance, @_current_node['price'] - we can't be sure that 'price' are in every node, but it's only in several nodes
122
+ # I wrapped this eval into rescue returning false when error, but this eval should be refactored.
123
+ begin
124
+ eval(exp.gsub(/@/, '@_current_node'))
125
+ rescue
126
+ false
127
+ end
120
128
  elsif exp.empty?
121
129
  default
122
130
  else
@@ -1,3 +1,3 @@
1
1
  class JsonPath
2
- VERSION = '0.5.7'
2
+ VERSION = '0.5.8'
3
3
  end
@@ -15,7 +15,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
15
15
  end
16
16
 
17
17
  def test_lookup_direct_path
18
- assert_equal 4, JsonPath.new('$.store.*').on(@object).first['book'].size
18
+ assert_equal 7, JsonPath.new('$.store.*').on(@object).first['book'].size
19
19
  end
20
20
 
21
21
  def test_lookup_missing_element
@@ -27,7 +27,10 @@ class TestJsonpath < MiniTest::Unit::TestCase
27
27
  @object['store']['book'][0]['author'],
28
28
  @object['store']['book'][1]['author'],
29
29
  @object['store']['book'][2]['author'],
30
- @object['store']['book'][3]['author']
30
+ @object['store']['book'][3]['author'],
31
+ @object['store']['book'][4]['author'],
32
+ @object['store']['book'][5]['author'],
33
+ @object['store']['book'][6]['author']
31
34
  ], JsonPath.new('$..author').on(@object)
32
35
  end
33
36
 
@@ -43,15 +46,15 @@ class TestJsonpath < MiniTest::Unit::TestCase
43
46
 
44
47
  def test_recognize_array_splices
45
48
  assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0:1:1]').on(@object)
46
- assert_equal [@object['store']['book'][1], @object['store']['book'][3]], JsonPath.new('$..book[1::2]').on(@object)
47
- assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[::2]').on(@object)
48
- assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[:-2:2]').on(@object)
49
- assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2::]').on(@object)
49
+ assert_equal [@object['store']['book'][1], @object['store']['book'][3], @object['store']['book'][5]], JsonPath.new('$..book[1::2]').on(@object)
50
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2], @object['store']['book'][4], @object['store']['book'][6]], JsonPath.new('$..book[::2]').on(@object)
51
+ assert_equal [@object['store']['book'][0], @object['store']['book'][2]], JsonPath.new('$..book[:-5:2]').on(@object)
52
+ assert_equal [@object['store']['book'][5], @object['store']['book'][6]], JsonPath.new('$..book[5::]').on(@object)
50
53
  end
51
54
 
52
55
  def test_recognize_array_comma
53
56
  assert_equal [@object['store']['book'][0], @object['store']['book'][1]], JsonPath.new('$..book[0,1]').on(@object)
54
- assert_equal [@object['store']['book'][2], @object['store']['book'][3]], JsonPath.new('$..book[2,-1::]').on(@object)
57
+ assert_equal [@object['store']['book'][2], @object['store']['book'][6]], JsonPath.new('$..book[2,-1::]').on(@object)
55
58
  end
56
59
 
57
60
  def test_recognize_filters
@@ -84,15 +87,15 @@ class TestJsonpath < MiniTest::Unit::TestCase
84
87
  end
85
88
 
86
89
  def test_recognize_array_with_evald_index
87
- assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-2)]').on(@object)
90
+ assert_equal [@object['store']['book'][2]], JsonPath.new('$..book[(@.length-5)]').on(@object)
88
91
  end
89
92
 
90
93
  def test_use_first
91
- assert_equal @object['store']['book'][2], JsonPath.new('$..book[(@.length-2)]').first(@object)
94
+ assert_equal @object['store']['book'][2], JsonPath.new('$..book[(@.length-5)]').first(@object)
92
95
  end
93
96
 
94
97
  def test_counting
95
- assert_equal 31, JsonPath.new('$..*').on(@object).to_a.size
98
+ assert_equal 49, JsonPath.new('$..*').on(@object).to_a.size
96
99
  end
97
100
 
98
101
  def test_space_in_path
@@ -144,7 +147,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
144
147
  end
145
148
 
146
149
  def test_wildcard
147
- assert_equal @object['store']['book'].collect{|e| e['price']}, JsonPath.on(@object, '$..book[*].price')
150
+ assert_equal @object['store']['book'].collect{|e| e['price']}.compact, JsonPath.on(@object, '$..book[*].price')
148
151
  end
149
152
 
150
153
  def test_wildcard_empty_array
@@ -165,6 +168,10 @@ class TestJsonpath < MiniTest::Unit::TestCase
165
168
  assert_equal [23], JsonPath.new("$..book[?(@.price > 20)].price").on(@object)
166
169
  end
167
170
 
171
+ def test_support_filter_by_childnode_value_over_childnode_and_select_child_key
172
+ assert_equal ["Osennie Vizity"], JsonPath.new("$..book[?(@.written.year == 1996)].title").on(@object)
173
+ end
174
+
168
175
  def example_object
169
176
  { "store"=> {
170
177
  "book" => [
@@ -189,6 +196,27 @@ class TestJsonpath < MiniTest::Unit::TestCase
189
196
  "title"=> "The Lord of the Rings",
190
197
  "isbn"=> "0-395-19395-8",
191
198
  "price"=> 23
199
+ },
200
+ { "category"=> "russian_fiction",
201
+ "author"=> "Lukyanenko",
202
+ "title"=> "Imperatory Illuziy",
203
+ "written" => {
204
+ "year" => 1995
205
+ }
206
+ },
207
+ { "category"=> "russian_fiction",
208
+ "author"=> "Lukyanenko",
209
+ "title"=> "Osennie Vizity",
210
+ "written" => {
211
+ "year" => 1996
212
+ }
213
+ },
214
+ { "category"=> "russian_fiction",
215
+ "author"=> "Lukyanenko",
216
+ "title"=> "Ne vremya dlya drakonov",
217
+ "written" => {
218
+ "year" => 1997
219
+ }
192
220
  }
193
221
  ],
194
222
  "bicycle"=> {
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonpath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.7
4
+ version: 0.5.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-10 00:00:00.000000000 Z
11
+ date: 2015-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json