json_path_rfc9535 1.0.0 → 1.1.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
  SHA256:
3
- metadata.gz: 341631a563a6872bcf0f04f58fdb547a5a17500f3f7fae04ba29ada044c17dbd
4
- data.tar.gz: '0669414a8c33cd4ddfd67f2e330d72783ec862ba1a26573053a5bbf372a6b26e'
3
+ metadata.gz: 69b07574707f71649d798ce1c776fd4130df2fa439f7d83289296029af07a5ff
4
+ data.tar.gz: 795c8a411c539e28e4698979d251cc87994d0b8a5ba33b6833049b3303b973a5
5
5
  SHA512:
6
- metadata.gz: c355da298d64c50f35f59a76bbff44b2511de1dafbf65181a078db74909433396dd86f803ecd1b821ce0bf4327264786298d7189e5c7ee37777bf35e53dacc8e
7
- data.tar.gz: f8e4f5e8a131adb0b1d88b9a820218687db2a146fea1a4ef91e33adb461b1d299285895c5fde88554f6ac14d87b7e86235ed97734253f397e06e741a5f270b13
6
+ metadata.gz: bb465b393405011742d074b069503872715c8eb31dbaf554b3795db58b897857ffe54a3866157ab4d85f7693e7f02af1b239f0c1b72c0379cf7e29a461f68552
7
+ data.tar.gz: c19ddf5f2d90ebc66bfc6d3b263a5a1375bdde3b8a6c8a75536fa6e687686b06d2f029311cf0a37d27791aef4019557879852a4742f62d1337ac7f4710e9ad8c
data/CHANGELOG.md CHANGED
@@ -8,6 +8,13 @@
8
8
  ### Bug fixes
9
9
  )-->
10
10
 
11
+ ## 1.1.0 2024-09-10
12
+
13
+ ### New features
14
+
15
+ - Made the return value of queries an `Enumerable`.
16
+ - Made individual nodes queryable.
17
+
11
18
  ## 1.0.0 2024-09-09
12
19
 
13
20
  First public release. Refer to [README.md](README.md) for the full documentation.
data/README.md CHANGED
@@ -75,12 +75,23 @@ JSON
75
75
  doc.query('$.store.bicycle.color')
76
76
  ```
77
77
 
78
- The returned object has methods to retrieve the values or the paths of all the retrieved nodes:
78
+ If you already parsed the Json, you can use that instead of the Json string:
79
79
 
80
80
  ```ruby
81
- doc.query('$.store.book.*.category').values
81
+ json = JSON.parse('...')
82
+ ...
83
+ doc = JsonPath::Doc(json)
84
+ ```
85
+
86
+ The query returns an `Enumerable`, which also has methods to retrieve the values or the paths of all the retrieved nodes:
87
+
88
+ ```ruby
89
+ results = doc.query('$.store.book.*.category')
90
+ results.count
91
+ # => 4
92
+ results.values
82
93
  # => ["reference", "fiction", "fiction", "fiction"]
83
- doc.query('$.store.book.*.category').paths
94
+ results.paths
84
95
  # => ["$['store']['book'][0]['category']", "$['store']['book'][1]['category']", "$['store']['book'][2]['category']", "$['store']['book'][3]['category']"]
85
96
  ```
86
97
 
@@ -94,6 +105,13 @@ results.query('$.author').values
94
105
  # => ["Evelyn Waugh", "J. R. R. Tolkien"]
95
106
  ```
96
107
 
108
+ Alternatively, you can query the single nodes:
109
+
110
+ ```ruby
111
+ results.flat_map { _1.query('$.author').values }.join(', ')
112
+ # => "Evelyn Waugh, J. R. R. Tolkien"
113
+ ```
114
+
97
115
  This gem implements most of RFC 9535, with the exception of [function extensions](https://datatracker.ietf.org/doc/html/rfc9535#name-function-extensions) and the related [type system](https://datatracker.ietf.org/doc/html/rfc9535#name-type-system-for-function-ex). It also relies on the underlying Ruby interpreter for string evaluation, meaning that characters don't need to be double-escaped.
98
116
 
99
117
  ## Plans for future development
data/lib/json_path/doc.rb CHANGED
@@ -1,7 +1,5 @@
1
1
  require 'json'
2
2
  require_relative 'nodes'
3
- require_relative 'path'
4
- require_relative 'node_list'
5
3
 
6
4
  module JsonPath
7
5
  class Doc
@@ -12,12 +10,8 @@ module JsonPath
12
10
  @root_node = Nodes.parse '$', json
13
11
  end
14
12
 
15
- def query json_path
16
- json_path = Path.new json_path
17
-
18
- json_path
19
- .apply(root_node)
20
- .then { NodeList.new _1 }
13
+ def query(...)
14
+ root_node.query(...)
21
15
  end
22
16
 
23
17
  def value
@@ -1,27 +1,29 @@
1
- require_relative 'path'
2
-
3
1
  module JsonPath
4
2
  class NodeList
3
+ include Enumerable
4
+
5
5
  attr_reader :nodes
6
6
 
7
7
  def initialize nodes
8
8
  @nodes = nodes
9
9
  end
10
10
 
11
- def query json_path
12
- json_path = Path.new json_path
13
-
11
+ def query(...)
14
12
  nodes
15
- .flat_map { json_path.apply _1 }
13
+ .flat_map { _1.query(...) }
16
14
  .then { self.class.new _1 }
17
15
  end
18
16
 
17
+ def each(&block)
18
+ nodes.each(&block)
19
+ end
20
+
19
21
  def values
20
- nodes.map(&:value)
22
+ map(&:value)
21
23
  end
22
24
 
23
25
  def paths
24
- nodes.map(&:path)
26
+ map(&:path)
25
27
  end
26
28
  end
27
29
  end
@@ -1,3 +1,6 @@
1
+ require_relative '../path'
2
+ require_relative '../node_list'
3
+
1
4
  module JsonPath
2
5
  module Nodes
3
6
  class Base
@@ -8,6 +11,14 @@ module JsonPath
8
11
  @path = path
9
12
  @children = []
10
13
  end
14
+
15
+ def query json_path
16
+ json_path = Path.new json_path
17
+
18
+ json_path
19
+ .apply(self)
20
+ .then { NodeList.new _1 }
21
+ end
11
22
  end
12
23
  end
13
24
  end
@@ -1,3 +1,3 @@
1
1
  module JsonPathRfc9535
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_path_rfc9535
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2024-09-09 00:00:00.000000000 Z
12
+ date: 2024-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: parslet