class JsonRefs::Dereferencer
Attributes
Public Class Methods
Source
# File lib/json_refs.rb, line 20 def initialize(doc_dir, doc, options = {}) @doc = doc @options = options @doc_dir = doc_dir end
Public Instance Methods
Source
# File lib/json_refs.rb, line 26 def call(doc = @doc, keys = []) if doc.is_a?(Array) doc.each_with_index do |value, idx| call(value, keys + [idx]) end elsif doc.is_a?(Hash) if doc.has_key?('$ref') dereference(keys, doc['$ref']) else doc.each do |key, value| call(value, keys + [key]) end end end doc end
Private Instance Methods
Source
# File lib/json_refs.rb, line 47 def dereference(paths, referenced_path) key = paths.pop target = paths.inject(@doc) do |obj, key| obj[key] end value = follow_referenced_value(referenced_path) target[key] = value end
Source
# File lib/json_refs.rb, line 89 def dereference_file(referenced_path) if @options[:resolve_file_ref] === false return { '$ref' => referenced_path } end klass = JsonRefs::DereferenceHandler::File # Checking for "://" in a URL like http://something.com so as to determine if it's a remote URL remote_uri = referenced_path =~ /:\/\// if remote_uri klass.new(path: referenced_path, doc: @doc).call else recursive_dereference(referenced_path, klass) end end
Source
# File lib/json_refs.rb, line 80 def dereference_local(referenced_path) if @options[:resolve_local_ref] === false return { '$ref' => referenced_path } end klass = JsonRefs::DereferenceHandler::Local klass.new(path: referenced_path, doc: @doc).call end
Source
# File lib/json_refs.rb, line 56 def follow_referenced_value(referenced_path) value = referenced_value(referenced_path) return referenced_value(value['$ref']) if value.is_a?(Hash) && value.has_key?('$ref') value end
Source
# File lib/json_refs.rb, line 106 def recursive_dereference(referenced_path, klass) unless File.absolute_path?(referenced_path) referenced_path = "#{doc_dir}/#{referenced_path}" end directory = File.dirname(referenced_path) referenced_doc = klass.new(path: referenced_path, doc: @doc).call Dereferencer.new(directory, referenced_doc, @options).call end
Source
# File lib/json_refs.rb, line 62 def referenced_value(referenced_path) if @options[:logging] == true puts "De-referencing #{referenced_path}" end filepath, pointer = referenced_path.split('#') pointer.prepend('#') if pointer return dereference_local(pointer) if filepath.empty? dereferenced_file = dereference_file(filepath) return dereferenced_file if pointer.nil? JsonRefs::DereferenceHandler::Local.new( doc: dereferenced_file, path: pointer ).call end