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 Parserwill be thrown up the stack
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
- path = path[1..-1] if path.start_with?('$') # TODO: check jsonpath syntax and raise error
14
- path_items = path.split('.')
15
- child_node = json_hash
16
- path_items.each do |path_item|
17
- path_item = path_item.sub('[\'', '').sub('\']', '') if path_item.start_with?('[\'')
18
- path_item, index = path_item.split('[')
19
- begin
20
- child_node = child_node.send(:fetch, path_item)
21
- rescue IndexError
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
- child_node
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
@@ -1,5 +1,5 @@
1
1
  module Poller
2
2
  module JSON
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
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.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-19 00:00:00.000000000 Z
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: '0'
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: '0'
29
+ version: 0.4.1
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: multi_json
32
32
  requirement: !ruby/object:Gem::Requirement