Class: Lurker::Json::Schema

Inherits:
Object
  • Object
show all
Includes:
Concerns::Validatable
Defined in:
lib/lurker/json/schema.rb

Constant Summary collapse

EXTENSIONS =
'extensions'.freeze
RESPONSE_CODES =
'responseCodes'.freeze
REQUEST_PARAMETERS =
'requestParameters'.freeze
RESPONSE_PARAMETERS =
'responseParameters'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Validatable

#to_validation_schema, #validate

Constructor Details

#initialize(schema, options = {}) ⇒ Schema

Returns a new instance of Schema.



13
14
15
16
17
18
19
20
21
22
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/lurker/json/schema.rb', line 73

def method_missing(method, *args, &block)
  if @schema.is_a?(Lurker::Json::Schema) || @schema.respond_to?(method)
    return @schema.send(method, *args, &block)
  end

  super
end

Instance Attribute Details

#parent_propertyObject (readonly)

Returns the value of attribute parent_property.



11
12
13
# File 'lib/lurker/json/schema.rb', line 11

def parent_property
  @parent_property
end

#parent_schemaObject (readonly)

Returns the value of attribute parent_schema.



11
12
13
# File 'lib/lurker/json/schema.rb', line 11

def parent_schema
  @parent_schema
end

#root_schemaObject (readonly)

Returns the value of attribute root_schema.



11
12
13
# File 'lib/lurker/json/schema.rb', line 11

def root_schema
  @root_schema
end

#uriObject (readonly)

Returns the value of attribute uri.



11
12
13
# File 'lib/lurker/json/schema.rb', line 11

def uri
  @uri
end

Instance Method Details

#documentationObject



28
29
30
31
32
# File 'lib/lurker/json/schema.rb', line 28

def documentation
  open(documentation_uri).read
rescue
  @schema['description']
end

#documentation_uri(extension = 'md') ⇒ Object



24
25
26
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#reorder!Object



56
57
58
59
# File 'lib/lurker/json/schema.rb', line 56

def reorder!
  @schema = Hash[@schema.sort]
  self
end

#replace!(property, property_schema) ⇒ Object



52
53
54
# File 'lib/lurker/json/schema.rb', line 52

def replace!(property, property_schema)
  @schema[property] = @parser.plain.parse_property(property, property_schema)
end

#root?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/lurker/json/schema.rb', line 34

def root?
  root_schema.blank?
end

#to_hash(options = {}) ⇒ Object



61
62
63
# File 'lib/lurker/json/schema.rb', line 61

def to_hash(options = {})
  hashify(@schema, options)
end

#to_json(options = {}) ⇒ Object



65
66
67
# File 'lib/lurker/json/schema.rb', line 65

def to_json(options = {})
  to_hash(options).to_json
end

#to_yaml(options = {}) ⇒ Object



69
70
71
# File 'lib/lurker/json/schema.rb', line 69

def to_yaml(options = {})
  YAML.dump(to_hash(options))
end