Sha256: 7e94b39852643bbdfe947be3d7dd31b261ce80a3c355d0f01135d2d6d639627f

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

module Lurker
  class JsonSchemaHash
    attr_accessor :schema_hash
    include JamlDescriptor::Rescue

    def initialize(schema_hash, uri)
      @to_s = uri
      @uri = URI.parse(uri)
      @uri = URI.parse("file://#{uri}") if @uri.relative?

      @schema_hash = Hash[schema_hash.map do |k, v|
        if k == '$ref' && v.is_a?(String)
          uri = @uri.merge(v)
          schema_hash = JSON.parse(open(uri.to_s).read)
          [k, JsonSchemaHash.new(schema_hash, uri.to_s)]
        elsif v.is_a?(Hash)
          [k, JsonSchemaHash.new(v, @to_s)]
        else
          [k, v]
        end
      end]
    end

    def respond_to_missing?(method, include_private = false)
      @schema_hash.send(:respond_to_missing?, method, include_private)
    end

    def method_missing(method, *args, &block)
      @schema_hash.send method, *args, &block
    end

    def is_a?(*args)
      @schema_hash.is_a?(*args)
    end

    def to_h
      Hash[@schema_hash.map do |k, v|
        if JsonSchemaHash === v
          [k, v.to_h]
        elsif v.is_a?(Array)
          [k, v.map { |i| JsonSchemaHash === i ? i.to_h : i }]
        else
          [k, v]
        end
      end]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lurker-0.6.3 lib/lurker/json_schema_hash.rb
lurker-0.6.2 lib/lurker/json_schema_hash.rb