Class: Lti2Commons::JsonWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/lti2_commons/json_wrapper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_str_or_obj) ⇒ JsonWrapper

Returns a new instance of JsonWrapper.



10
11
12
# File 'lib/lti2_commons/json_wrapper.rb', line 10

def initialize(json_str_or_obj)
  @root = JsonPath.new('$').on(json_str_or_obj).first
end

Instance Attribute Details

#rootObject

Returns the value of attribute root.



8
9
10
# File 'lib/lti2_commons/json_wrapper.rb', line 8

def root
  @root
end

Instance Method Details

#at(path) ⇒ Object



14
15
16
# File 'lib/lti2_commons/json_wrapper.rb', line 14

def at(path)
  JsonPath.new(path).on(@root)
end

#deep_copyJsonObject

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

Returns:

  • (JsonObject)

    A full new version of self



21
22
23
# File 'lib/lti2_commons/json_wrapper.rb', line 21

def deep_copy
  JsonWrapper.new(@root.to_json)
end

#each_leaf(&block) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/lti2_commons/json_wrapper.rb', line 25

def each_leaf &block
  JsonPath.new("$..*").on(@root).each { |node|
    if node.is_a? String 
      yield node
    end
  }
end

#first_at(path) ⇒ Object



33
34
35
# File 'lib/lti2_commons/json_wrapper.rb', line 33

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

Returns:

  • (TreeNode)

    Either self or immediate child that matches constraint_hash



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lti2_commons/json_wrapper.rb', line 42

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
    for child in candidate
      # 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.

Returns:

  • (Object)

    result_path within matching node or nil



62
63
64
65
66
67
# File 'lib/lti2_commons/json_wrapper.rb', line 62

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.

Returns:

  • (Object)

    result_path within matching node or nil



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lti2_commons/json_wrapper.rb', line 77

def select(path, selector, value, return_path)
  candidates = JsonPath.new(path).on(@root)
  for candidate in candidates
    selector_node = JsonPath.new(selector).on(candidate.first)
    if selector_node.include? value
      break
    end
  end
  if candidate
    JsonPath.new(return_path).on(candidate.first).first
  else
    nil
  end
end

#substitute_text_in_all_nodes(token_prefix, token_suffix, hash) ⇒ Object



92
93
94
95
# File 'lib/lti2_commons/json_wrapper.rb', line 92

def substitute_text_in_all_nodes(token_prefix, token_suffix, hash)
  self.each_leaf { |v|
    substitute_template_values_from_hash v, token_prefix, token_suffix, hash }
end

#to_pretty_jsonObject



97
98
99
# File 'lib/lti2_commons/json_wrapper.rb', line 97

def to_pretty_json
  JSON.pretty_generate root
end