class Lurker::Json::Schema

Constants

EXTENSIONS
REQUEST_PARAMETERS
RESPONSE_CODES
RESPONSE_PARAMETERS

Attributes

parent_property[R]
parent_schema[R]
root_schema[R]
uri[R]

Public Class Methods

new(schema, options = {}) click to toggle source
# File lib/lurker/json/schema.rb, line 13
def initialize(schema, options = {})
  @root_schema = options[:root_schema]
  @parent_schema = options[:parent_schema]
  @parent_property = options[:parent_property]
  @uri = parse_uri(options[:uri])

  @parser = Lurker::Json::Parser.new(subschema_options)

  parse_schema(schema)
end

Public Instance Methods

documentation() click to toggle source
# File lib/lurker/json/schema.rb, line 28
def documentation
  open(documentation_uri).read
rescue
  @schema['description']
end
documentation_uri(extension = 'md') click to toggle source
# File lib/lurker/json/schema.rb, line 24
def documentation_uri(extension = 'md')
  @uri.to_s.sub(%r{^file:(//)?}, '').sub(/(\.json)?(\.yml)?(\.erb)?$/, ".#{extension}")
end
merge!(schema) click to toggle source
# File lib/lurker/json/schema.rb, line 38
def merge!(schema)
  return if schema.blank?

  schema.each do |property, property_schema|
    if @schema[property].is_a?(Lurker::Json::Schema)
      @schema[property].merge!(property_schema)

      next
    end

    replace!(property, property_schema)
  end
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/lurker/json/schema.rb, line 77
def method_missing(method, *args, &block)
  if @schema.is_a?(Lurker::Json::Schema) || @schema.respond_to?(method)
    @schema.send(method, *args, &block)
  else
    super
  end
end
reorder!() click to toggle source
# File lib/lurker/json/schema.rb, line 56
def reorder!
  @schema = Hash[@schema.sort]
  self
end
replace!(property, property_schema) click to toggle source
# File lib/lurker/json/schema.rb, line 52
def replace!(property, property_schema)
  @schema[property] = @parser.plain.parse_property(property, property_schema)
end
respond_to_missing?(method, include_private=false) click to toggle source
# File lib/lurker/json/schema.rb, line 73
def respond_to_missing?(method, include_private=false)
  @schema.respond_to?(method, include_private)
end
root?() click to toggle source
# File lib/lurker/json/schema.rb, line 34
def root?
  root_schema.blank?
end
to_hash(options = {}) click to toggle source
# File lib/lurker/json/schema.rb, line 61
def to_hash(options = {})
  hashify(@schema, options)
end
to_json(options = {}) click to toggle source
# File lib/lurker/json/schema.rb, line 65
def to_json(options = {})
  to_hash(options).to_json
end
to_yaml(options = {}) click to toggle source
# File lib/lurker/json/schema.rb, line 69
def to_yaml(options = {})
  YAML.dump(to_hash(options))
end

Private Instance Methods

hashify(object, options = {}) click to toggle source
# File lib/lurker/json/schema.rb, line 87
def hashify(object, options = {})
  case object
  when Lurker::Json::Reference
    options[:reference] == :original ? object.to_original_hash(options)
      : object.to_hash(options)
  when Lurker::Json::Schema then object.to_hash(options)
  when Array then object.map { |x| hashify(x, options) }
  when Hash
    object.each_with_object({}) do |(property, property_schema), memo|
      memo[property] = hashify(property_schema, options)
    end
  else object
  end
end
parse_schema(schema) click to toggle source
# File lib/lurker/json/schema.rb, line 109
def parse_schema(schema)
  @schema = {}

  unless schema.is_a?(Hash)
    return @schema = @parser.plain.parse(schema)
  end

  schema.each do |property, property_schema|
    @schema[property] = case property
    when EXTENSIONS
      Lurker::Json::Extensions.new(property_schema, subschema_options)
    when RESPONSE_CODES
      Lurker::Json::ResponseCodes.new(property_schema, subschema_options)
    when REQUEST_PARAMETERS, RESPONSE_PARAMETERS
      @parser.typed(polymorph_if_empty: true).parse_property(property, property_schema)
    else
      @parser.plain.parse_property(property, property_schema)
    end
  end
end
parse_uri(uri) click to toggle source
# File lib/lurker/json/schema.rb, line 102
def parse_uri(uri)
  return if uri.blank?

  uri = uri.respond_to?(:scheme) ? uri : URI.parse(uri)
  uri.relative? ? URI.parse("file://#{uri}") : uri
end
subschema_options() click to toggle source
# File lib/lurker/json/schema.rb, line 130
def subschema_options
  {uri: uri, root_schema: (root? ? self : root_schema), parent_schema: self}
end