jsonpath 0.8.12 → 0.9.0

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: 9338212edfe2ba8589ef826b3443fb7dcfdc3112
4
- data.tar.gz: b5e09641428a77416aa455ec0450f0730abe86b4
3
+ metadata.gz: 02b522c0db894285a626b8e1d6d44a95140d5a03
4
+ data.tar.gz: 1f4176316390136b0d229627bf3965f4b4ae1c44
5
5
  SHA512:
6
- metadata.gz: ce08bd8da4e3240d613d589400d32219563c76afb376e46967258afb6000e00a924e8480ee2f934da4696ef2b34eb52f1409a5b8ecc0b80ed523d6f44dc990b5
7
- data.tar.gz: ccbbce4a9e02ad37c2f70ffa16a40a6667d3df00a57be835493589b3c0903efd0d8105e9e56032b2d232235b6d59955dff6281e02f718ffd528abeb8871a8b1c
6
+ metadata.gz: 7f4e322af2377394c35ca7fa5fb03f3f3df63ed815eb064d33c6a4e96064ad872a3fc58a3462ecffa4f733d7444d484abb4a454e4b0bb75dbea1cce0f3ad57be
7
+ data.tar.gz: 72110c129177b94eb9572bc4dd288dfe6bd8025f00db6c71ef4b384b0122f9ba6d373c7305fce7c6e477b50a524fc2b921c33bd95eb01877c752758a074d7378
@@ -36,13 +36,14 @@ class JsonPath
36
36
  expr[1, expr.size - 2].split(',').each do |sub_path|
37
37
  case sub_path[0]
38
38
  when '\'', '"'
39
- next unless node.is_a?(Hash)
40
- k = sub_path[1, sub_path.size - 2]
41
- each(node, k, pos + 1, &blk) if node.key?(k)
39
+ if node.is_a?(Hash)
40
+ k = sub_path[1, sub_path.size - 2]
41
+ each(node, k, pos + 1, &blk) if node.key?(k)
42
+ end
42
43
  when '?'
43
44
  handle_question_mark(sub_path, node, pos, &blk)
44
45
  else
45
- next unless node.is_a?(Array) && !node.empty?
46
+ next if node.is_a?(Array) && node.empty?
46
47
  array_args = sub_path.split(':')
47
48
  if array_args[0] == '*'
48
49
  start_idx = 0
@@ -65,19 +66,13 @@ class JsonPath
65
66
 
66
67
  def handle_question_mark(sub_path, node, pos, &blk)
67
68
  case node
68
- when Array
69
- node.size.times do |index|
70
- @_current_node = node[index]
71
- # exps = sub_path[1, sub_path.size - 1]
72
- # if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]")
69
+ when Hash, Array
70
+ (node.is_a?(Hash) ? node.keys : (0..node.size)).each do |e|
71
+ @_current_node = node[e]
73
72
  if process_function_or_literal(sub_path[1, sub_path.size - 1])
74
73
  each(@_current_node, nil, pos + 1, &blk)
75
74
  end
76
75
  end
77
- when Hash
78
- if process_function_or_literal(sub_path[1, sub_path.size - 1])
79
- each(@_current_node, nil, pos + 1, &blk)
80
- end
81
76
  else
82
77
  yield node if process_function_or_literal(sub_path[1, sub_path.size - 1])
83
78
  end
@@ -111,7 +106,7 @@ class JsonPath
111
106
  return nil unless @_current_node
112
107
 
113
108
  identifiers = /@?((?<!\d)\.(?!\d)(\w+))+/.match(exp)
114
- unless identifiers.nil? || @_current_node.methods.include?(identifiers[2].to_sym)
109
+ if !identifiers.nil? && !@_current_node.methods.include?(identifiers[2].to_sym)
115
110
  exp_to_eval = exp.dup
116
111
  exp_to_eval[identifiers[0]] = identifiers[0].split('.').map do |el|
117
112
  el == '@' ? '@' : "['#{el}']"
@@ -46,14 +46,17 @@ class JsonPath
46
46
  end
47
47
  end
48
48
 
49
- el = dig(elements, @_current_node)
49
+ if elements.empty?
50
+ el = @_current_node
51
+ else
52
+ el = dig(elements, @_current_node)
53
+ end
50
54
  return false if el.nil?
51
55
  return true if operator.nil? && el
52
56
 
53
57
  el = Float(el) rescue el
54
58
  operand = Float(operand) rescue operand
55
59
  operand = false if operand == 'false' && el == false
56
-
57
60
  el.send(operator.strip, operand)
58
61
  end
59
62
 
@@ -61,7 +64,7 @@ class JsonPath
61
64
 
62
65
  # @TODO: Remove this once JsonPath no longer supports ruby versions below 2.3
63
66
  def dig(keys, hash)
64
- return hash unless hash.is_a? Hash
67
+ return nil unless hash.is_a? Hash
65
68
  return nil unless hash.key?(keys.first)
66
69
  return hash.fetch(keys.first) if keys.size == 1
67
70
  prev = keys.shift
@@ -1,3 +1,3 @@
1
1
  class JsonPath
2
- VERSION = '0.8.12'.freeze
2
+ VERSION = '0.9.0'.freeze
3
3
  end
@@ -245,8 +245,8 @@ class TestJsonpath < MiniTest::Unit::TestCase
245
245
  'id' => '123'
246
246
  }
247
247
  }
248
- assert_equal [{ 'type' => 'users', 'id' => '123' }], JsonPath.new("$.data[?(@.type == 'users')]").on(data)
249
- assert_equal [], JsonPath.new("$.data[?(@.type == 'admins')]").on(data)
248
+ assert_equal [{ 'type' => 'users', 'id' => '123' }], JsonPath.new("$.[?(@.type == 'users')]").on(data)
249
+ assert_equal [], JsonPath.new("$.[?(@.type == 'admins')]").on(data)
250
250
  end
251
251
 
252
252
  def test_support_at_sign_in_member_names
@@ -270,9 +270,11 @@ class TestJsonpath < MiniTest::Unit::TestCase
270
270
 
271
271
  def test_slash_in_value
272
272
  data = {
273
- 'data' => {
274
- 'type' => 'mps/awesome'
275
- }
273
+ 'data' => [{
274
+ 'type' => 'mps/awesome',
275
+ },{
276
+ 'type' => 'not',
277
+ }]
276
278
  }
277
279
  assert_equal [{ 'type' => 'mps/awesome' }], JsonPath.new("$.data[?(@.type == \"mps/awesome\")]").on(data)
278
280
  end
@@ -283,7 +285,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
283
285
  'type' => 0.00001
284
286
  }
285
287
  }
286
- assert_equal [{"type"=>0.00001}], JsonPath.new("$.data[?(@.type == 0.00001)]").on(data)
288
+ assert_equal [{"type"=>0.00001}], JsonPath.new("$.[?(@.type == 0.00001)]").on(data)
287
289
  end
288
290
 
289
291
  def test_digits_only_string
@@ -293,6 +295,19 @@ class TestJsonpath < MiniTest::Unit::TestCase
293
295
  'id' => '123'
294
296
  }
295
297
  }
298
+ assert_equal([{"type"=>"users", "id"=>"123"}], JsonPath.new("$.[?(@.id == '123')]").on(data))
299
+ end
300
+
301
+ def test_digits_only_string_in_array
302
+ data = {
303
+ 'foo' => [{
304
+ 'type' => 'users',
305
+ 'id' => '123'
306
+ },{
307
+ 'type' => 'users',
308
+ 'id' => '321'
309
+ }]
310
+ }
296
311
  assert_equal([{"type"=>"users", "id"=>"123"}], JsonPath.new("$.foo[?(@.id == '123')]").on(data))
297
312
  end
298
313
 
@@ -366,7 +381,7 @@ class TestJsonpath < MiniTest::Unit::TestCase
366
381
  'number' => '(492) 080-3961'
367
382
  }
368
383
  }
369
- assert_equal [{'number'=>'(492) 080-3961'}], JsonPath.new("$.data[?(@.number == '(492) 080-3961')]").on(data)
384
+ assert_equal [{'number'=>'(492) 080-3961'}], JsonPath.new("$.[?(@.number == '(492) 080-3961')]").on(data)
370
385
  end
371
386
 
372
387
 
@@ -452,6 +467,26 @@ class TestJsonpath < MiniTest::Unit::TestCase
452
467
  assert_equal 'C09C598QL', JsonPath.on(json, "$..channels[?(@.is_archived)].id")[0]
453
468
  end
454
469
 
470
+ def test_regression_4
471
+ json = {
472
+ ok: true,
473
+ channels: [
474
+ {
475
+ id: 'C09C5GYHF',
476
+ name: 'general',
477
+ is_archived: false
478
+ },
479
+ {
480
+ id: 'C09C598QL',
481
+ name: 'random',
482
+ is_archived: true
483
+ }
484
+ ]
485
+ }.to_json
486
+
487
+ assert_equal ['C09C5GYHF'], JsonPath.on(json, "$..[?(@.name == 'general')].id")
488
+ end
489
+
455
490
  def test_regression_5
456
491
  json = {
457
492
  ok: true,
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.8.12
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua Hull
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-05-12 00:00:00.000000000 Z
12
+ date: 2018-05-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json