Class: SchemaTools::RefResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_tools/ref_resolver.rb

Class Method Summary collapse

Class Method Details

._retrieve_pointer_from_object(pointer, object) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/schema_tools/ref_resolver.rb', line 42

def self._retrieve_pointer_from_object pointer, object
  # assume path to be the JSONPointer expression:
  #  json/pointer/expression
  # and obj to be the ruby hash representation of the json
  path = pointer.is_a?(Array) ? pointer : pointer.split("/")

  while object != nil && component = path.shift
    prev   = object
    component = component.to_i if object.is_a?(Array) && component =~ /^\d+$/
    object = object[component]
  end

  return object
end

.load_json_pointer(json_pointer, schema = {}) ⇒ Object

super basic resolving of JSON Pointer stuff in order to be able to load $ref parameter.

$refs in JSON Schema are defined in JSON Pointer syntax: tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07

the hash to evaluate it against. If the pointer contains a uri to a referenced schema, an attempt is made to load

Parameters:

  • json_pointer (String)

    the JSON Pointer expression to evaluate

  • schema (Hash) (defaults to: {})

    if the pointer refers to a local schema, this is this



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/schema_tools/ref_resolver.rb', line 19

def self.load_json_pointer json_pointer, schema = {}
  # JSON Pointer is a big, abandoned WIP and we're going to
  # start by only implementing the part's we need ...
  if nil ==  (json_pointer =~ /^(.*)#(.*)/ )
    raise "invalid json pointer: #{json_pointer}"
  end

  uri     = $1.strip
  pointer = $2

  if ! uri.empty?
    uri = URI.parse(uri)
    raise "must currently be a relative uri: #{json_pointer}" if uri.absolute?
    path = SchemaTools.schema_path + "/" + uri.path
    open (path) {|f|
      schema = JSON.parse(f.read)
    }
  end

  return self._retrieve_pointer_from_object pointer, schema
end