poller-json 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -8,7 +8,7 @@ module Matchers
|
|
8
8
|
end
|
9
9
|
|
10
10
|
# @param document_s [String] - the document given as String
|
11
|
-
# Exceptions caught by JSON
|
11
|
+
# Exceptions caught by JSON Parser will be thrown up the stack
|
12
12
|
# ::JSON::ParserError in the case of invalid JSON
|
13
13
|
def matches?(document_s)
|
14
14
|
json_hash = ::JSON.parse(document_s)
|
@@ -9,29 +9,57 @@
|
|
9
9
|
module Matchers
|
10
10
|
module JSON
|
11
11
|
module JSONPath
|
12
|
+
|
12
13
|
def value_on_path(json_hash, path)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
return nil
|
23
|
-
end
|
24
|
-
if index
|
25
|
-
index = index[0..-2].to_i
|
26
|
-
begin
|
27
|
-
child_node = child_node.send(:fetch, index)
|
28
|
-
rescue IndexError
|
29
|
-
return nil
|
30
|
-
end
|
31
|
-
end
|
14
|
+
raise ArgumentError, "Invalid json path: #{path}" unless json_path_valid?(path)
|
15
|
+
path = translate_to_dot_notation(path)
|
16
|
+
current_node = json_hash # start traversal at document root
|
17
|
+
path_items(path).each do |path_item|
|
18
|
+
element, index = decompose_path_item(path_item)
|
19
|
+
begin
|
20
|
+
current_node = fetch(current_node, element, index)
|
21
|
+
rescue IndexError
|
22
|
+
return nil
|
32
23
|
end
|
33
|
-
|
24
|
+
end
|
25
|
+
current_node
|
26
|
+
end
|
27
|
+
|
28
|
+
# TODO: the regular expressions below will catch very few errors
|
29
|
+
def json_path_valid?(path)
|
30
|
+
notation = path.include?('[\'') ? :bracket : :dot
|
31
|
+
path_regex = /^\$[a-zA-Z0-9_(\[\d\])\.]*[^\.]$/ if notation == :dot
|
32
|
+
path_regex = /^\$[a-zA-Z0-9_(\[\d\])\.\']*[^\.]$/ if notation == :bracket
|
33
|
+
path_regex.match(path)
|
34
|
+
end
|
35
|
+
|
36
|
+
def translate_to_dot_notation(path)
|
37
|
+
path = path.gsub('[\'', '').gsub('\']', '') if path.include?('[\'')
|
38
|
+
path
|
39
|
+
end
|
40
|
+
private :translate_to_dot_notation
|
41
|
+
|
42
|
+
def path_items(path)
|
43
|
+
path[1..-1].split('.') # removing the leading '$'
|
44
|
+
end
|
45
|
+
private :path_items
|
46
|
+
|
47
|
+
def fetch(current_node, element, index)
|
48
|
+
current_node = current_node.send(:fetch, element)
|
49
|
+
current_node = current_node.send(:fetch, index) if index
|
50
|
+
current_node
|
34
51
|
end
|
52
|
+
private :fetch
|
53
|
+
|
54
|
+
# if the path item contains an index (eg 'sample[3]') then the index will be returned
|
55
|
+
# if not, second return value will be nil [element,index]
|
56
|
+
def decompose_path_item(path_item)
|
57
|
+
element, index = path_item.split('[')
|
58
|
+
index = index[0..-2].to_i if index
|
59
|
+
[element, index]
|
60
|
+
end
|
61
|
+
private :decompose_path_item
|
62
|
+
|
35
63
|
end
|
36
64
|
end
|
37
65
|
end
|
data/lib/poller/json/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: poller-json
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: poller
|
@@ -18,7 +18,7 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version:
|
21
|
+
version: 0.4.1
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ! '>='
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version:
|
29
|
+
version: 0.4.1
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
31
|
name: multi_json
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|