Class: Lti2Commons::JsonWrapper
- Inherits:
-
Object
- Object
- Lti2Commons::JsonWrapper
- Defined in:
- lib/lti2_commons/lib/lti2_commons/json_wrapper.rb
Instance Attribute Summary collapse
-
#root ⇒ Object
Returns the value of attribute root.
Instance Method Summary collapse
- #at(path) ⇒ Object
-
#deep_copy ⇒ JsonObject
Deep copy through reserialization.
- #each_leaf ⇒ Object
- #first_at(path) ⇒ Object
-
#get_matching_node(candidate, constraint_hash) ⇒ TreeNode
Does this node, possibly repeating, match all elements of the constraint_hash.
-
#initialize(json_str_or_obj) ⇒ JsonWrapper
constructor
A new instance of JsonWrapper.
-
#search(path, constraint_hash, return_path) ⇒ Object
Convenience method to find a particular node with matching attributes to constraint_hash and then return specific element of that node.
-
#select(path, selector, value, return_path) ⇒ Object
Convenience method to find a particular node with matching attributes to constraint_hash and then return specific element of that node.
- #substitute_text_in_all_nodes(token_prefix, token_suffix, hash) ⇒ Object
- #to_pretty_json ⇒ Object
Constructor Details
#initialize(json_str_or_obj) ⇒ JsonWrapper
Returns a new instance of JsonWrapper.
9 10 11 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 9 def initialize(json_str_or_obj) @root = JsonPath.new('$').on(json_str_or_obj).first end |
Instance Attribute Details
#root ⇒ Object
Returns the value of attribute root.
7 8 9 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 7 def root @root end |
Instance Method Details
#at(path) ⇒ Object
13 14 15 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 13 def at(path) JsonPath.new(path).on(@root) end |
#deep_copy ⇒ JsonObject
Deep copy through reserialization. Only used to preserve immutability of source.
20 21 22 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 20 def deep_copy JsonWrapper.new(@root.to_json) end |
#each_leaf ⇒ Object
24 25 26 27 28 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 24 def each_leaf JsonPath.new('$..*').on(@root).each do |node| yield node if node.is_a? String end end |
#first_at(path) ⇒ Object
30 31 32 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 30 def first_at(path) at(path).first end |
#get_matching_node(candidate, constraint_hash) ⇒ TreeNode
Does this node, possibly repeating, match all elements of the constraint_hash
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 39 def get_matching_node(candidate, constraint_hash) if candidate.is_a? Hash return candidate if is_hash_intersect(candidate, constraint_hash) elsif candidate.is_a? Array candidate.each do |child| # are there extraneous keys in constraint_hash return child if is_hash_intersect(child, constraint_hash) end end nil end |
#search(path, constraint_hash, return_path) ⇒ Object
Convenience method to find a particular node with matching attributes to constraint_hash and then return specific element of that node. e.g., search for ‘path’ within the ‘resource_handler’ with constraint_hash attributes.
59 60 61 62 63 64 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 59 def search(path, constraint_hash, return_path) candidate = JsonPath.new(path).on(@root) candidate = get_matching_node(candidate.first, constraint_hash) return nil unless candidate JsonPath.new(return_path).on(candidate).first end |
#select(path, selector, value, return_path) ⇒ Object
Convenience method to find a particular node with matching attributes to constraint_hash and then return specific element of that node. e.g., search for ‘path’ within the ‘resource_handler’ with constraint_hash attributes.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 74 def select(path, selector, value, return_path) candidates = JsonPath.new(path).on(@root) now_candidate = nil candidates.each do |candidate| now_candidate = candidate selector_node = JsonPath.new(selector).on(candidate.first) break if selector_node.include? value end if now_candidate JsonPath.new(return_path).on(now_candidate.first).first else nil end end |
#substitute_text_in_all_nodes(token_prefix, token_suffix, hash) ⇒ Object
89 90 91 92 93 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 89 def substitute_text_in_all_nodes(token_prefix, token_suffix, hash) self.each_leaf do |v| substitute_template_values_from_hash(v, token_prefix, token_suffix, hash) end end |
#to_pretty_json ⇒ Object
95 96 97 |
# File 'lib/lti2_commons/lib/lti2_commons/json_wrapper.rb', line 95 def to_pretty_json JSON.pretty_generate(root) end |