jsonpath 0.5.8 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +5 -0
- data/.rubocop.yml +1 -0
- data/.rubocop_todo.yml +127 -0
- data/.travis.yml +7 -4
- data/Gemfile +6 -2
- data/LICENSE.md +21 -0
- data/README.md +175 -42
- data/Rakefile +16 -5
- data/bin/jsonpath +2 -1
- data/jsonpath.gemspec +11 -16
- data/lib/jsonpath/dig.rb +57 -0
- data/lib/jsonpath/enumerable.rb +120 -89
- data/lib/jsonpath/parser.rb +217 -0
- data/lib/jsonpath/proxy.rb +18 -5
- data/lib/jsonpath/version.rb +4 -2
- data/lib/jsonpath.rb +63 -39
- data/test/test_jsonpath.rb +1002 -78
- data/test/test_jsonpath_bin.rb +9 -7
- data/test/test_readme.rb +117 -0
- metadata +22 -15
data/test/test_jsonpath_bin.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'phocus'
|
5
|
+
require 'jsonpath'
|
6
|
+
|
1
7
|
class TestJsonpathBin < MiniTest::Unit::TestCase
|
2
8
|
def setup
|
3
|
-
@runner =
|
9
|
+
@runner = 'ruby -Ilib bin/jsonpath'
|
4
10
|
@original_dir = Dir.pwd
|
5
11
|
Dir.chdir(File.join(File.dirname(__FILE__), '..'))
|
6
12
|
end
|
@@ -11,11 +17,7 @@ class TestJsonpathBin < MiniTest::Unit::TestCase
|
|
11
17
|
end
|
12
18
|
|
13
19
|
def test_stdin
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_stdin
|
18
|
-
File.open('/tmp/test.json', 'w'){|f| f << '{"test": "time"}'}
|
20
|
+
File.open('/tmp/test.json', 'w') { |f| f << '{"test": "time"}' }
|
19
21
|
assert_equal '["time"]', `#{@runner} '$.test' /tmp/test.json`.strip
|
20
22
|
end
|
21
|
-
end
|
23
|
+
end
|
data/test/test_readme.rb
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'minitest/autorun'
|
4
|
+
require 'phocus'
|
5
|
+
require 'jsonpath'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
class TestJsonpathReadme < MiniTest::Unit::TestCase
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@json = <<-HERE_DOC
|
12
|
+
{"store":
|
13
|
+
{"bicycle":
|
14
|
+
{"price":19.95, "color":"red"},
|
15
|
+
"book":[
|
16
|
+
{"price":8.95, "category":"reference", "title":"Sayings of the Century", "author":"Nigel Rees"},
|
17
|
+
{"price":12.99, "category":"fiction", "title":"Sword of Honour", "author":"Evelyn Waugh"},
|
18
|
+
{"price":8.99, "category":"fiction", "isbn":"0-553-21311-3", "title":"Moby Dick", "author":"Herman Melville","color":"blue"},
|
19
|
+
{"price":22.99, "category":"fiction", "isbn":"0-395-19395-8", "title":"The Lord of the Rings", "author":"Tolkien"}
|
20
|
+
]
|
21
|
+
}
|
22
|
+
}
|
23
|
+
HERE_DOC
|
24
|
+
end
|
25
|
+
attr_reader :json
|
26
|
+
|
27
|
+
def test_library_section
|
28
|
+
path = JsonPath.new('$..price')
|
29
|
+
assert_equal [19.95, 8.95, 12.99, 8.99, 22.99], path.on(json)
|
30
|
+
assert_equal [18.88], path.on('{"books":[{"title":"A Tale of Two Somethings","price":18.88}]}')
|
31
|
+
assert_equal ["Nigel Rees", "Evelyn Waugh", "Herman Melville", "Tolkien"], JsonPath.on(json, '$..author')
|
32
|
+
assert_equal [
|
33
|
+
{"price" => 8.95, "category" => "reference", "title" => "Sayings of the Century", "author" => "Nigel Rees"},
|
34
|
+
{"price" => 8.99, "category" => "fiction", "isbn" => "0-553-21311-3", "title" => "Moby Dick", "author" => "Herman Melville","color" => "blue"},
|
35
|
+
], JsonPath.new('$..book[::2]').on(json)
|
36
|
+
assert_equal [8.95, 8.99], JsonPath.new("$..price[?(@ < 10)]").on(json)
|
37
|
+
assert_equal ["Sayings of the Century", "Moby Dick"], JsonPath.new("$..book[?(@['price'] == 8.95 || @['price'] == 8.99)].title").on(json)
|
38
|
+
assert_equal [], JsonPath.new("$..book[?(@['price'] == 8.95 && @['price'] == 8.99)].title").on(json)
|
39
|
+
assert_equal "red", JsonPath.new('$..color').first(json)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_library_section_enumerable
|
43
|
+
enum = JsonPath.new('$..color')[json]
|
44
|
+
assert_equal "red", enum.first
|
45
|
+
assert enum.any?{ |c| c == 'red' }
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_ruby_structures_section
|
49
|
+
book = { title: "Sayings of the Century" }
|
50
|
+
assert_equal [], JsonPath.new('$.title').on(book)
|
51
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title', use_symbols: true).on(book)
|
52
|
+
|
53
|
+
book_class = Struct.new(:title)
|
54
|
+
book = book_class.new("Sayings of the Century")
|
55
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title').on(book)
|
56
|
+
|
57
|
+
book_class = Class.new{ attr_accessor :title }
|
58
|
+
book = book_class.new
|
59
|
+
book.title = "Sayings of the Century"
|
60
|
+
assert_equal ["Sayings of the Century"], JsonPath.new('$.title', allow_send: true).on(book)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_options_section
|
64
|
+
assert_equal ["0-553-21311-3", "0-395-19395-8"], JsonPath.new('$..book[*].isbn').on(json)
|
65
|
+
assert_equal [nil, nil, "0-553-21311-3", "0-395-19395-8"], JsonPath.new('$..book[*].isbn', default_path_leaf_to_null: true).on(json)
|
66
|
+
|
67
|
+
assert_equal ["price", "category", "title", "author"], JsonPath.new('$..book[0]').on(json).map(&:keys).flatten.uniq
|
68
|
+
assert_equal [:price, :category, :title, :author], JsonPath.new('$..book[0]').on(json, symbolize_keys: true).map(&:keys).flatten.uniq
|
69
|
+
end
|
70
|
+
|
71
|
+
def selecting_value_section
|
72
|
+
json = <<-HERE_DOC
|
73
|
+
{
|
74
|
+
"store": {
|
75
|
+
"book": [
|
76
|
+
{
|
77
|
+
"category": "reference",
|
78
|
+
"author": "Nigel Rees",
|
79
|
+
"title": "Sayings of the Century",
|
80
|
+
"price": 8.95
|
81
|
+
},
|
82
|
+
{
|
83
|
+
"category": "fiction",
|
84
|
+
"author": "Evelyn Waugh",
|
85
|
+
"title": "Sword of Honour",
|
86
|
+
"price": 12.99
|
87
|
+
}
|
88
|
+
]
|
89
|
+
}
|
90
|
+
HERE_DOC
|
91
|
+
got = JsonPath.on(json, "$.store.book[*](category,author)")
|
92
|
+
expected = [
|
93
|
+
{
|
94
|
+
"category" => "reference",
|
95
|
+
"author" => "Nigel Rees"
|
96
|
+
},
|
97
|
+
{
|
98
|
+
"category" => "fiction",
|
99
|
+
"author" => "Evelyn Waugh"
|
100
|
+
}
|
101
|
+
]
|
102
|
+
assert_equal expected, got
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_manipulation_section
|
106
|
+
assert_equal({"candy" => "big turks"}, JsonPath.for('{"candy":"lollipop"}').gsub('$..candy') {|v| "big turks" }.to_hash)
|
107
|
+
|
108
|
+
json = '{"candy":"lollipop","noncandy":null,"other":"things"}'
|
109
|
+
o = JsonPath.for(json).
|
110
|
+
gsub('$..candy') {|v| "big turks" }.
|
111
|
+
compact.
|
112
|
+
delete('$..other').
|
113
|
+
to_hash
|
114
|
+
assert_equal({"candy" => "big turks"}, o)
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonpath
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Hull
|
8
|
-
|
8
|
+
- Gergely Brautigam
|
9
|
+
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2020-12-28 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
15
|
name: multi_json
|
@@ -25,7 +26,7 @@ dependencies:
|
|
25
26
|
- !ruby/object:Gem::Version
|
26
27
|
version: '0'
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
29
|
+
name: bundler
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
32
|
- - ">="
|
@@ -39,7 +40,7 @@ dependencies:
|
|
39
40
|
- !ruby/object:Gem::Version
|
40
41
|
version: '0'
|
41
42
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
43
|
+
name: code_stats
|
43
44
|
requirement: !ruby/object:Gem::Requirement
|
44
45
|
requirements:
|
45
46
|
- - ">="
|
@@ -81,7 +82,7 @@ dependencies:
|
|
81
82
|
- !ruby/object:Gem::Version
|
82
83
|
version: '0'
|
83
84
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
85
|
+
name: rake
|
85
86
|
requirement: !ruby/object:Gem::Requirement
|
86
87
|
requirements:
|
87
88
|
- - ">="
|
@@ -95,7 +96,9 @@ dependencies:
|
|
95
96
|
- !ruby/object:Gem::Version
|
96
97
|
version: '0'
|
97
98
|
description: Ruby implementation of http://goessner.net/articles/JsonPath/.
|
98
|
-
email:
|
99
|
+
email:
|
100
|
+
- joshbuddy@gmail.com
|
101
|
+
- skarlso777@gmail.com
|
99
102
|
executables:
|
100
103
|
- jsonpath
|
101
104
|
extensions: []
|
@@ -105,41 +108,45 @@ files:
|
|
105
108
|
- ".gemtest"
|
106
109
|
- ".gitignore"
|
107
110
|
- ".rspec"
|
111
|
+
- ".rubocop.yml"
|
112
|
+
- ".rubocop_todo.yml"
|
108
113
|
- ".travis.yml"
|
109
114
|
- Gemfile
|
115
|
+
- LICENSE.md
|
110
116
|
- README.md
|
111
117
|
- Rakefile
|
112
118
|
- bin/jsonpath
|
113
119
|
- jsonpath.gemspec
|
114
120
|
- lib/jsonpath.rb
|
121
|
+
- lib/jsonpath/dig.rb
|
115
122
|
- lib/jsonpath/enumerable.rb
|
123
|
+
- lib/jsonpath/parser.rb
|
116
124
|
- lib/jsonpath/proxy.rb
|
117
125
|
- lib/jsonpath/version.rb
|
118
126
|
- test/test_jsonpath.rb
|
119
127
|
- test/test_jsonpath_bin.rb
|
120
|
-
|
128
|
+
- test/test_readme.rb
|
129
|
+
homepage: https://github.com/joshbuddy/jsonpath
|
121
130
|
licenses:
|
122
131
|
- MIT
|
123
132
|
metadata: {}
|
124
|
-
post_install_message:
|
125
|
-
rdoc_options:
|
126
|
-
- "--charset=UTF-8"
|
133
|
+
post_install_message:
|
134
|
+
rdoc_options: []
|
127
135
|
require_paths:
|
128
136
|
- lib
|
129
137
|
required_ruby_version: !ruby/object:Gem::Requirement
|
130
138
|
requirements:
|
131
139
|
- - ">="
|
132
140
|
- !ruby/object:Gem::Version
|
133
|
-
version: '
|
141
|
+
version: '2.5'
|
134
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
143
|
requirements:
|
136
144
|
- - ">="
|
137
145
|
- !ruby/object:Gem::Version
|
138
146
|
version: '0'
|
139
147
|
requirements: []
|
140
|
-
|
141
|
-
|
142
|
-
signing_key:
|
148
|
+
rubygems_version: 3.1.2
|
149
|
+
signing_key:
|
143
150
|
specification_version: 4
|
144
151
|
summary: Ruby implementation of http://goessner.net/articles/JsonPath/
|
145
152
|
test_files: []
|