json_path 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 31dcfa618b1b86a157267dc2dcee90b5c4663b31
4
+ data.tar.gz: 7821248cd073a36d0ff5ac17d9dbeff1579c5834
5
+ SHA512:
6
+ metadata.gz: a2dab72a69895a27a1c2e46f52303a6ce46ffafd908bd7c506c44b6dacd762394008774e487f91087e1f6e732048a97dd048b07eaedf42304bab51a00126f6a2
7
+ data.tar.gz: 153b1add10dd2fe036d2bc51f0d0f574d2167d42b0fef5101ce25a161503a20b01075fa73504842a875b5a7dd89493589a5488e2a39307f5c25e61676e230c66
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,65 @@
1
+ {
2
+ // Use IntelliSense to learn about possible attributes.
3
+ // Hover to view descriptions of existing attributes.
4
+ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+ "version": "0.2.0",
6
+ "configurations": [
7
+
8
+ {
9
+ "name": "Debug Local File",
10
+ "type": "Ruby",
11
+ "request": "launch",
12
+ "cwd": "${workspaceRoot}",
13
+ "program": "${workspaceRoot}/src/json_path.rb"
14
+ },
15
+ {
16
+ "name": "Listen for rdebug-ide",
17
+ "type": "Ruby",
18
+ "request": "attach",
19
+ "cwd": "${workspaceRoot}",
20
+ "remoteHost": "127.0.0.1",
21
+ "remotePort": "1234",
22
+ "remoteWorkspaceRoot": "${workspaceRoot}"
23
+ },
24
+ {
25
+ "name": "Rails server",
26
+ "type": "Ruby",
27
+ "request": "launch",
28
+ "cwd": "${workspaceRoot}",
29
+ "program": "${workspaceRoot}/bin/rails",
30
+ "args": [
31
+ "server"
32
+ ]
33
+ },
34
+ {
35
+ "name": "RSpec - all",
36
+ "type": "Ruby",
37
+ "request": "launch",
38
+ "cwd": "${workspaceRoot}",
39
+ "program": "${workspaceRoot}/bin/rspec",
40
+ "args": [
41
+ "-I",
42
+ "${workspaceRoot}"
43
+ ]
44
+ },
45
+ {
46
+ "name": "RSpec - active spec file only",
47
+ "type": "Ruby",
48
+ "request": "launch",
49
+ "cwd": "${workspaceRoot}",
50
+ "program": "${workspaceRoot}/bin/rspec",
51
+ "args": [
52
+ "-I",
53
+ "${workspaceRoot}",
54
+ "${file}"
55
+ ]
56
+ },
57
+ {
58
+ "name": "Cucumber",
59
+ "type": "Ruby",
60
+ "request": "launch",
61
+ "cwd": "${workspaceRoot}",
62
+ "program": "${workspaceRoot}/bin/cucumber"
63
+ }
64
+ ]
65
+ }
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at ribeiro.luis@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+
4
+ # Specify your gem's dependencies in json_path.gemspec
5
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ json_path (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ json_path!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.1
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Luis Landeiro Ribeiro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,361 @@
1
+ # JsonPath
2
+ Ruby conversion of the Javascript JSONPath from http://goessner.net/articles/JsonPath/
3
+
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'json_path'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install json_path
20
+
21
+ ## Usage
22
+
23
+ ```json
24
+ { "store": {
25
+ "book": [
26
+ { "category": "reference",
27
+ "author": "Nigel Rees",
28
+ "title": "Sayings of the Century",
29
+ "price": 8.95
30
+ },
31
+ { "category": "fiction",
32
+ "author": "Evelyn Waugh",
33
+ "title": "Sword of Honour",
34
+ "price": 12.99
35
+ },
36
+ { "category": "fiction",
37
+ "author": "Herman Melville",
38
+ "title": "Moby Dick",
39
+ "isbn": "0-553-21311-3",
40
+ "price": 8.99
41
+ },
42
+ { "category": "fiction",
43
+ "author": "J. R. R. Tolkien",
44
+ "title": "The Lord of the Rings",
45
+ "isbn": "0-395-19395-8",
46
+ "price": 22.99
47
+ }
48
+ ],
49
+ "bicycle": {
50
+ "color": "red",
51
+ "price": 19.95
52
+ }
53
+ } ,
54
+ "expensive": 10
55
+ }
56
+ ```
57
+
58
+ `$.store.book[*].author` can get the authors of all books in the store
59
+
60
+ ```json
61
+ ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
62
+ ```
63
+
64
+ `$..author` can get all authors
65
+
66
+ ```json
67
+ ["Nigel Rees","Evelyn Waugh","Herman Melville","J. R. R. Tolkien"]
68
+ ```
69
+
70
+ `$.store.*` can get all things in store, which are some books and a red bicycle
71
+
72
+ ```json
73
+ [
74
+ {
75
+ "category" : "reference",
76
+ "author" : "Nigel Rees",
77
+ "title" : "Sayings of the Century",
78
+ "price" : 8.95
79
+ },
80
+ {
81
+ "category" : "fiction",
82
+ "author" : "Evelyn Waugh",
83
+ "title" : "Sword of Honour",
84
+ "price" : 12.99
85
+ },
86
+ {
87
+ "category" : "fiction",
88
+ "author" : "Herman Melville",
89
+ "title" : "Moby Dick",
90
+ "isbn" : "0-553-21311-3",
91
+ "price" : 8.99
92
+ },
93
+ {
94
+ "category" : "fiction",
95
+ "author" : "J. R. R. Tolkien",
96
+ "title" : "The Lord of the Rings",
97
+ "isbn" : "0-395-19395-8",
98
+ "price" : 22.99
99
+ }
100
+ ],
101
+ {
102
+ "color" : "red",
103
+ "price" : 19.95
104
+ }
105
+ ]
106
+ ```
107
+
108
+ `$.store..price` can get the price of everything in the store
109
+
110
+ ```json
111
+ [ 8.95, 12.99, 8.99, 22.99, 19.95 ]
112
+ ````
113
+
114
+ `$..book[2]` can get the third book
115
+
116
+ ```json
117
+ [
118
+ {
119
+ "category" : "fiction",
120
+ "author" : "Herman Melville",
121
+ "title" : "Moby Dick",
122
+ "isbn" : "0-553-21311-3",
123
+ "price" : 8.99
124
+ }
125
+ ]
126
+ ```
127
+
128
+ `$..book[(@.length-1)]` can get the last element
129
+
130
+ ```json
131
+ [{
132
+ "category" : "fiction",
133
+ "author" : "J. R. R. Tolkien",
134
+ "title" : "The Lord of the Rings",
135
+ "isbn" : "0-395-19395-8",
136
+ "price" : 22.99
137
+ }]
138
+ ```
139
+
140
+ `$..book[-1:]` can get the last book in order
141
+
142
+ ```json
143
+ [{
144
+ "category" : "fiction",
145
+ "author" : "J. R. R. Tolkien",
146
+ "title" : "The Lord of the Rings",
147
+ "isbn" : "0-395-19395-8",
148
+ "price" : 22.99
149
+ }]
150
+ ```
151
+
152
+ `$..book[0,1]` can get the first two books
153
+
154
+ ```json
155
+ [{
156
+ "category" : "reference",
157
+ "author" : "Nigel Rees",
158
+ "title" : "Sayings of the Century",
159
+ "price" : 8.95
160
+ },
161
+ {
162
+ "category" : "fiction",
163
+ "author" : "Evelyn Waugh",
164
+ "title" : "Sword of Honour",
165
+ "price" : 12.99
166
+ }]
167
+ ```
168
+
169
+ `$..book[:2]` can get the first two books
170
+
171
+ ```json
172
+ [{
173
+ "category" : "reference",
174
+ "author" : "Nigel Rees",
175
+ "title" : "Sayings of the Century",
176
+ "price" : 8.95
177
+ },
178
+ {
179
+ "category" : "fiction",
180
+ "author" : "Evelyn Waugh",
181
+ "title" : "Sword of Honour",
182
+ "price" : 12.99
183
+ }]
184
+ ```
185
+
186
+ `$..book[?(@.isbn)]` can filter all books with isbn number
187
+
188
+ ```json
189
+ [{
190
+ "category" : "fiction",
191
+ "author" : "Herman Melville",
192
+ "title" : "Moby Dick",
193
+ "isbn" : "0-553-21311-3",
194
+ "price" : 8.99
195
+ },
196
+ {
197
+ "category" : "fiction",
198
+ "author" : "J. R. R. Tolkien",
199
+ "title" : "The Lord of the Rings",
200
+ "isbn" : "0-395-19395-8",
201
+ "price" : 22.99
202
+ }]
203
+ ```
204
+
205
+ `$..book[?(@.price<10)]` can filter all books cheapier than 10
206
+
207
+ ```json
208
+ [
209
+ {
210
+ "category" : "reference",
211
+ "author" : "Nigel Rees",
212
+ "title" : "Sayings of the Century",
213
+ "price" : 8.95
214
+ },
215
+ {
216
+ "category" : "fiction",
217
+ "author" : "Herman Melville",
218
+ "title" : "Moby Dick",
219
+ "isbn" : "0-553-21311-3",
220
+ "price" : 8.99
221
+ }
222
+ ]
223
+ ```
224
+
225
+ `$..*` can get All members of JSON structure
226
+
227
+ ```json
228
+ [
229
+ {
230
+ "book" : [
231
+ {
232
+ "category" : "reference",
233
+ "author" : "Nigel Rees",
234
+ "title" : "Sayings of the Century",
235
+ "price" : 8.95
236
+ },
237
+ {
238
+ "category" : "fiction",
239
+ "author" : "Evelyn Waugh",
240
+ "title" : "Sword of Honour",
241
+ "price" : 12.99
242
+ },
243
+ {
244
+ "category" : "fiction",
245
+ "author" : "Herman Melville",
246
+ "title" : "Moby Dick",
247
+ "isbn" : "0-553-21311-3",
248
+ "price" : 8.99
249
+ },
250
+ {
251
+ "category" : "fiction",
252
+ "author" : "J. R. R. Tolkien",
253
+ "title" : "The Lord of the Rings",
254
+ "isbn" : "0-395-19395-8",
255
+ "price" : 22.99
256
+ }
257
+ ],
258
+ "bicycle" : {
259
+ "color" : "red",
260
+ "price" : 19.95
261
+ }
262
+ },
263
+ 10,
264
+ [
265
+ {
266
+ "category" : "reference",
267
+ "author" : "Nigel Rees",
268
+ "title" : "Sayings of the Century",
269
+ "price" : 8.95
270
+ },
271
+ {
272
+ "category" : "fiction",
273
+ "author" : "Evelyn Waugh",
274
+ "title" : "Sword of Honour",
275
+ "price" : 12.99
276
+ },
277
+ {
278
+ "category" : "fiction",
279
+ "author" : "Herman Melville",
280
+ "title" : "Moby Dick",
281
+ "isbn" : "0-553-21311-3",
282
+ "price" : 8.99
283
+ },
284
+ {
285
+ "category" : "fiction",
286
+ "author" : "J. R. R. Tolkien",
287
+ "title" : "The Lord of the Rings",
288
+ "isbn" : "0-395-19395-8",
289
+ "price" : 22.99
290
+ }
291
+ ],
292
+ {
293
+ "color" : "red",
294
+ "price" : 19.95
295
+ },
296
+ {
297
+ "category" : "reference",
298
+ "author" : "Nigel Rees",
299
+ "title" : "Sayings of the Century",
300
+ "price" : 8.95
301
+ },
302
+ {
303
+ "category" : "fiction",
304
+ "author" : "Evelyn Waugh",
305
+ "title" : "Sword of Honour",
306
+ "price" : 12.99
307
+ },
308
+ {
309
+ "category" : "fiction",
310
+ "author" : "Herman Melville",
311
+ "title" : "Moby Dick",
312
+ "isbn" : "0-553-21311-3",
313
+ "price" : 8.99
314
+ },
315
+ {
316
+ "category" : "fiction",
317
+ "author" : "J. R. R. Tolkien",
318
+ "title" : "The Lord of the Rings",
319
+ "isbn" : "0-395-19395-8",
320
+ "price" : 22.99
321
+ },
322
+ "reference",
323
+ "Nigel Rees",
324
+ "Sayings of the Century",
325
+ 8.95,
326
+ "fiction",
327
+ "Evelyn Waugh",
328
+ "Sword of Honour",
329
+ 12.99,
330
+ "fiction",
331
+ "Herman Melville",
332
+ "Moby Dick",
333
+ "0-553-21311-3",
334
+ 8.99,
335
+ "fiction",
336
+ "J. R. R. Tolkien",
337
+ "The Lord of the Rings",
338
+ "0-395-19395-8",
339
+ 22.99,
340
+ "red",
341
+ 19.95
342
+ ]
343
+ ```
344
+
345
+ ## Development
346
+
347
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
348
+
349
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
350
+
351
+ ## Contributing
352
+
353
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lribeiro/json_path. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
354
+
355
+ ## License
356
+
357
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
358
+
359
+ ## Code of Conduct
360
+
361
+ Everyone interacting in the JsonPath project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/lribeiro/json_path/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "json_path/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "json_path"
8
+ spec.version = JsonPath::VERSION
9
+ spec.authors = ["Luis Landeiro Ribeiro"]
10
+ spec.email = ["ribeiro.luis@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby conversion of the Javascript JsonPath from goessner}
13
+ spec.description = %q{Ruby conversion of the Javascript JsonPath from http://goessner.net/articles/JsonPath/}
14
+ spec.homepage = "https://github.com/lribeiro/json_path"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "bin"
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.16"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,190 @@
1
+ require "json_path/version"
2
+
3
+ class String
4
+ @@is_int_regex = /\A[-+]?\d+\z/
5
+ def is_i?
6
+ @@is_int_regex.match?(self)
7
+ end
8
+
9
+ def as_i(default)
10
+ self.is_i? ? self.to_i : default
11
+ end
12
+ end
13
+
14
+ class JsonPath
15
+ attr_accessor :resultType
16
+ attr_accessor :result
17
+ attr_accessor :expression
18
+
19
+ def initialize(arg)
20
+ @resultType = arg && arg.resultType rescue "VALUE"
21
+ @result = []
22
+ @expression = normalize(arg).sub(/^\$;/,"")
23
+ end
24
+
25
+ def normalize(expr)
26
+ subx = [];
27
+
28
+ ex = expr.gsub(/[\['](\??\(.*?\))[\]']/){ |x| subx << $1; "[##{subx.size - 1}]"}
29
+ ex = ex.gsub(/'?\.'?|\['?/, ";")
30
+ ex = ex.gsub(/;;;|;;/, ";..;")
31
+ ex = ex.gsub(/;$|'?\]|'$/, "")
32
+ ex = ex.gsub(/#([0-9]+)/){ |x| subx[$1.to_i]}
33
+
34
+ ex
35
+ end
36
+
37
+ def asPath(path)
38
+ x = path.split(";")
39
+ p = "$"
40
+
41
+ i = 1
42
+ n = x.size
43
+ while i<n do
44
+ p += /^[0-9*]+$/.match?(x[i]) ? ("["+x[i]+"]") : ("['"+x[i]+"']")
45
+ i +=1
46
+ end
47
+ return p
48
+ end
49
+
50
+ def store(p, v)
51
+ if p
52
+ @result << (@resultType == "PATH" ? asPath(p) : v)
53
+ end
54
+ return !!p
55
+ end
56
+
57
+ def trace(expr, val, path)
58
+ if expr && expr.size > 0
59
+ x = expr.split(";")
60
+ loc = x.shift
61
+ x = x.join(";")
62
+
63
+ if val && (Hash === val && val[loc])
64
+ trace(x, val[loc], path + ";" + loc)
65
+ elsif val && (Array === val && loc && loc.is_i? && val.size > loc.to_i)
66
+ trace(x, val[loc.to_i], path + ";" + loc)
67
+
68
+ elsif (loc == "*")
69
+ walk(loc, x, val, path) do |m,l,x,v,p|
70
+ trace("#{m};#{x}",v,p)
71
+ end
72
+
73
+ elsif (loc == "..")
74
+ trace(x, val, path)
75
+ walk(loc, x, val, path) do |m,l,x,v,p|
76
+ case object = v[m]
77
+ when Hash,Array
78
+ trace("..;"+x.to_s,v[m],p.to_s+";"+m.to_s)
79
+ end
80
+ end
81
+
82
+ elsif /,/.match?(loc) # [name1,name2,...]
83
+ s=loc.split(/'?,'?/)
84
+ i=0
85
+ n=s.size
86
+
87
+ while i<n do
88
+ trace(s[i].to_s+";"+x.to_s, val, path)
89
+ i+=1
90
+ end
91
+
92
+ elsif (/^\(.*?\)$/.match?(loc)) # [(expr)]
93
+ last_idx = path.rindex(";") + 1
94
+ p = path[last_idx..-1]
95
+ trace(jp_eval(loc, val, p).to_s+";"+x, val, path)
96
+
97
+ elsif (/^\?\(.*?\)$/.match?(loc)) # [?(expr)]
98
+ walk(loc, x, val, path) do |m,l,x,v,p|
99
+ if jp_eval(l.sub(/^\?\((.*?)\)$/,'\1'),v[m],m)
100
+ trace("#{m};#{x}",v,p)
101
+ end
102
+ end
103
+
104
+ elsif (/^(-?[0-9]*):(-?[0-9]*):?([0-9]*)$/.match?(loc)) # [start:end:step] python slice syntax
105
+ slice(loc, x, val, path)
106
+ end
107
+ else
108
+ store(path, val)
109
+ end
110
+ end
111
+
112
+ def walk(loc, expr, val, path)
113
+ case val
114
+ when Array
115
+ val.each_with_index do |object,i|
116
+ yield i,loc,expr,val,path
117
+ end
118
+ when Hash
119
+ val.each_pair do |key,value|
120
+ yield key,loc,expr,val,path
121
+ end
122
+ end
123
+ end
124
+
125
+ def slice(loc, expr, val, path)
126
+ if Array === val
127
+ len=val.size
128
+ start=0
129
+ size=len
130
+ step=1
131
+
132
+ loc.gsub(/^(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)$/) do |match|
133
+ start = $1.to_i if $1.size > 0
134
+ size = $2.to_i if $2.size > 0
135
+ step = $3.to_i if $3.size > 0
136
+ end
137
+
138
+ start = (start < 0) ? [0,start+len].max : [len,start].min
139
+ size = (size < 0) ? [0,size+len].max : [len,size].min
140
+
141
+ i = start
142
+ while i < size
143
+ trace(i.to_s+";"+expr.to_s, val, path)
144
+ i += step
145
+ end
146
+ end
147
+ end
148
+
149
+ def jp_eval(x, _v, _vname)
150
+ begin
151
+ if _v && _v.size > 0
152
+ case _v
153
+ when Hash
154
+ x.match(/@.(\w+)\s*([<=>!~]*)\s*(\d*)/)
155
+ case
156
+ when !$3 || $3.size == 0
157
+ _v[$1]
158
+ else
159
+ value = $1 == "length" ? _v.size : _v[$1].to_i
160
+ value.send($2,$3.to_i)
161
+ end
162
+ when Array
163
+ x.match(/@.(\w+)\s*([\<\=\>\!\~\-\+]+)\s*(\d*)/)
164
+ case
165
+ when (!$3 || $3.size == 0) && $1 && $1.is_i?
166
+ _v[$1.to_i]
167
+ else
168
+ value = case $1
169
+ when "length"
170
+ _v.size
171
+ when /\A[-+]?\d+\z/
172
+ $1.to_i
173
+ end
174
+ value && value.send($2,$3.to_i)
175
+ end
176
+ end
177
+
178
+ end
179
+ rescue Exception => e
180
+ raise "jsonPath: " + e.message + ": " + x.gsub(/@/, "_v").gsub(/\^/, "_a")
181
+ end
182
+ end
183
+
184
+ def on(object)
185
+ if @expression && object && ( @resultType == "VALUE" || @resultType == "PATH")
186
+ trace(@expression, object, "$")
187
+ return @result || false
188
+ end
189
+ end
190
+ end
@@ -0,0 +1,3 @@
1
+ class JsonPath
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_path
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Luis Landeiro Ribeiro
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-06-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: Ruby conversion of the Javascript JsonPath from http://goessner.net/articles/JsonPath/
56
+ email:
57
+ - ribeiro.luis@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".bundle/config"
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - ".vscode/launch.json"
67
+ - CODE_OF_CONDUCT.md
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.md
72
+ - Rakefile
73
+ - json_path.gemspec
74
+ - lib/json_path.rb
75
+ - lib/json_path/version.rb
76
+ homepage: https://github.com/lribeiro/json_path
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.6.12
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Ruby conversion of the Javascript JsonPath from goessner
100
+ test_files: []