Class: JsonPath
- Inherits:
-
Object
show all
- Defined in:
- lib/jsonpath.rb,
lib/jsonpath/proxy.rb,
lib/jsonpath/parser.rb,
lib/jsonpath/version.rb,
lib/jsonpath/enumerable.rb
Overview
JsonPath: initializes the class with a given JsonPath and parses that path into a token array.
Defined Under Namespace
Classes: Enumerable, Parser, Proxy
Constant Summary
collapse
- PATH_ALL =
'$..*'
- VERSION =
'1.0.5'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path, opts = {}) ⇒ JsonPath
Returns a new instance of JsonPath.
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/jsonpath.rb', line 17
def initialize(path, opts = {})
@opts = opts
scanner = StringScanner.new(path.strip)
@path = []
until scanner.eos?
if (token = scanner.scan(/\$\B|@\B|\*|\.\./))
@path << token
elsif (token = scanner.scan(/[$@a-zA-Z0-9:{}_-]+/))
@path << "['#{token}']"
elsif (token = scanner.scan(/'(.*?)'/))
@path << "[#{token}]"
elsif (token = scanner.scan(/\[/))
@path << find_matching_brackets(token, scanner)
elsif (token = scanner.scan(/\]/))
raise ArgumentError, 'unmatched closing bracket'
elsif (token = scanner.scan(/\(.*\)/))
@path << token
elsif scanner.scan(/\./)
nil
elsif (token = scanner.scan(/[><=] \d+/))
@path.last << token
elsif (token = scanner.scan(/./))
begin
@path.last << token
rescue RuntimeError
raise ArgumentError, "character '#{token}' not supported in query"
end
end
end
end
|
Instance Attribute Details
#path ⇒ Object
Returns the value of attribute path.
15
16
17
|
# File 'lib/jsonpath.rb', line 15
def path
@path
end
|
Class Method Details
.for(obj_or_str) ⇒ Object
96
97
98
|
# File 'lib/jsonpath.rb', line 96
def self.for(obj_or_str)
Proxy.new(process_object(obj_or_str))
end
|
.on(obj_or_str, path, opts = {}) ⇒ Object
92
93
94
|
# File 'lib/jsonpath.rb', line 92
def self.on(obj_or_str, path, opts = {})
new(path, opts).on(process_object(obj_or_str))
end
|
Instance Method Details
#enum_on(obj_or_str, mode = nil) ⇒ Object
Also known as:
[]
86
87
88
89
|
# File 'lib/jsonpath.rb', line 86
def enum_on(obj_or_str, mode = nil)
JsonPath::Enumerable.new(self, self.class.process_object(obj_or_str), mode,
@opts)
end
|
#find_matching_brackets(token, scanner) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/jsonpath.rb', line 48
def find_matching_brackets(token, scanner)
count = 1
until count.zero?
if (t = scanner.scan(/\[/))
token << t
count += 1
elsif (t = scanner.scan(/\]/))
token << t
count -= 1
elsif (t = scanner.scan(/[^\[\]]+/))
token << t
elsif scanner.eos?
raise ArgumentError, 'unclosed bracket'
end
end
token
end
|
#first(obj_or_str, *args) ⇒ Object
82
83
84
|
# File 'lib/jsonpath.rb', line 82
def first(obj_or_str, *args)
enum_on(obj_or_str).first(*args)
end
|
#join(join_path) ⇒ Object
66
67
68
69
70
|
# File 'lib/jsonpath.rb', line 66
def join(join_path)
res = deep_clone
res.path += JsonPath.new(join_path).path
res
end
|
#on(obj_or_str, opts = {}) ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/jsonpath.rb', line 72
def on(obj_or_str, opts = {})
a = enum_on(obj_or_str).to_a
if opts[:symbolize_keys]
a.map! do |e|
e.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; }
end
end
a
end
|