class Lti2Commons::JsonWrapper

Attributes

root[RW]

Public Class Methods

new(json_str_or_obj) click to toggle source
# 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

Public Instance Methods

at(path) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/json_wrapper.rb, line 13
def at(path)
  JsonPath.new(path).on(@root)
end
deep_copy() click to toggle source

Deep copy through reserialization. Only used to preserve immutability of source.

@return [JsonObject] A full new version of self

# File lib/lti2_commons/lib/lti2_commons/json_wrapper.rb, line 20
def deep_copy
  JsonWrapper.new(@root.to_json)
end
each_leaf() { |node| ... } click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Does this node, possibly repeating, match all elements of the constraint_hash

@params candidate [Hash/Array] node or Array of nodes to match @params constraint_hash [Hash] Hash of value to match @return [TreeNode] Either self or immediate child that matches constraint_hash

# 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
select(path, selector, value, return_path) click to toggle source

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.

@params path [JsonPath] path to parent of candidate array of nodes @params constraint_hash [Hash] Hash of values which must match target @params return_path [JsonPath] Path of element within matching target @return [Object] result_path within matching node or nil

# 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) click to toggle source
# 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() click to toggle source
# File lib/lti2_commons/lib/lti2_commons/json_wrapper.rb, line 95
def to_pretty_json
  JSON.pretty_generate(root)
end

Private Instance Methods

is_hash_intersect(target_hash, constraint_hash) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/json_wrapper.rb, line 101
def is_hash_intersect(target_hash, constraint_hash)
  # failed search if constraint_hash as invalid keys
  return nil if (constraint_hash.keys - target_hash.keys).length > 0
  target_hash.each_pair do |k, v|
    if constraint_hash.key? k
      return false unless v == constraint_hash[k]
    end
  end
  true
end
substitute_template_values_from_hash(source_string, prefix, suffix, hash) click to toggle source
# File lib/lti2_commons/lib/lti2_commons/json_wrapper.rb, line 112
def substitute_template_values_from_hash(source_string, prefix, suffix, hash)
  hash.each do |k, v|
    source_string.sub!(prefix + k + suffix, v)
  end
end