class Circuitdata::Dereferencer

Attributes

base_path[R]

Public Class Methods

dereference(schema, base_path) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 5
def self.dereference(schema, base_path)
  d = new(base_path)
  d.start(schema)
end
new(base_path) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 10
def initialize(base_path)
  @base_path = base_path
end

Public Instance Methods

start(schema) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 14
def start(schema)
  hash_iterator(schema, schema)
end

Private Instance Methods

array_iterator(arr, original_schema) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 95
def array_iterator(arr, original_schema)
  arr.map do |v|
    if v.is_a?(Hash)
      hash_iterator(v, original_schema)
    elsif v.is_a?(Array)
      array_iterator(arr, original_schema)
    else
      v
    end
  end
end
dereferenced_read_file(file_path) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 34
def dereferenced_read_file(file_path)
  if file_path.start_with?("https://")
    full_path = file_path
  else
    full_path = File.expand_path(file_path, base_path)
  end
  file_data = read_file(full_path)
  self.class.dereference(file_data, File.dirname(full_path))
end
get_ref(ref, original_schema) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 44
def get_ref(ref, original_schema)
  file_path, pointer = ref.split("#")
  if file_path == ""
    data = original_schema
  else
    data = dereferenced_read_file(file_path)
  end
  pointer_parts = pointer.split("/").reject(&:blank?)
  result = data.dig(*pointer_parts.map(&:to_sym))
  if result.nil?
    fail "Unable to dereference ref=#{ref}"
  end
  result
end
get_remote_file(url_str) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 59
def get_remote_file(url_str)
  url = URI.parse(url_str)
  req = Net::HTTP::Get.new(url.to_s)
  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  res = http.request(req)
  if res.code != "200"
    raise StandardError.new("Expected 200 status got #{res.code.inspect} for #{url_str}")
  end
  res.body
end
hash_iterator(h, original_schema) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 71
def hash_iterator(h, original_schema)
  h = h.clone
  h.each_pair do |k, v|
    if v.is_a?(Hash)
      res = hash_iterator(v, original_schema)
      if res[:"$ref"]
        h[k] = res[:"$ref"]
      else
        h[k] = res
      end
    elsif v.is_a?(Array)
      h[k] = array_iterator(v, original_schema)
    else
      if k == :"$ref"
        ref_schema = get_ref(v, original_schema)
        return hash_iterator(ref_schema, original_schema)
      else
        h[k] = v
      end
    end
  end
  h
end
read_file(file_path) click to toggle source
# File lib/circuitdata/dereferencer.rb, line 22
def read_file(file_path)
  if file_path.start_with?("https://")
    file = get_remote_file(file_path)
  else
    file = File.read(file_path)
  end
  JSON.parse(file, symbolize_names: true)
rescue => e
  puts file_path
  raise e
end