Class: Prmd::Schema

Inherits:
Object
  • Object
show all
Defined in:
lib/prmd/schema.rb

Instance Method Summary collapse

Constructor Details

#initialize(new_data = {}) ⇒ Schema

Returns a new instance of Schema.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/prmd/schema.rb', line 12

def initialize(new_data = {})
  convert_type_to_array = lambda do |datum|
    case datum
    when Array
      datum.map { |element| convert_type_to_array.call(element) }
    when Hash
      if datum.has_key?('type') && datum['type'].is_a?(String)
        datum['type'] = [*datum['type']]
      end
      datum.each { |k,v| datum[k] = convert_type_to_array.call(v) }
    else
      datum
    end
  end
  @data = convert_type_to_array.call(new_data)
  @schemata_examples = {}
end

Instance Method Details

#[](key) ⇒ Object



4
5
6
# File 'lib/prmd/schema.rb', line 4

def [](key)
  @data[key]
end

#[]=(key, value) ⇒ Object



8
9
10
# File 'lib/prmd/schema.rb', line 8

def []=(key, value)
  @data[key] = value
end

#dereference(reference) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/prmd/schema.rb', line 30

def dereference(reference)
  if reference.is_a?(Hash)
    if reference.has_key?('$ref')
      value = reference.dup
      key = value.delete('$ref')
    else
      return [nil, reference] # no dereference needed
    end
  else
    key, value = reference, {}
  end
  begin
    datum = @data
    key.gsub(%r{[^#]*#/}, '').split('/').each do |fragment|
      datum = datum[fragment]
    end
    # last dereference will have nil key, so compact it out
    # [-2..-1] should be the final key reached before deref
    dereferenced_key, dereferenced_value = dereference(datum)
    [
      [key, dereferenced_key].compact.last,
      [dereferenced_value, value].inject({}) { |composite, element| composite.merge(element) }
    ]
  rescue => error
    $stderr.puts("Failed to dereference `#{key}`")
    raise(error)
  end
end

#hrefObject



101
102
103
# File 'lib/prmd/schema.rb', line 101

def href
  (@data['links'].detect { |link| link['rel'] == 'self' } || {})['href']
end

#schema_example(schema) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/prmd/schema.rb', line 77

def schema_example(schema)
  _, _schema = dereference(schema)

  if _schema.has_key?('example')
    _schema['example']
  elsif _schema.has_key?('properties')
    example = {}
    _schema['properties'].each do |key, value|
      _, value = dereference(value)
      example[key] = schema_value_example(value)
    end
    example
  elsif _schema.has_key?('items')
    schema_value_example(_schema)
  end
end

#schema_value_example(value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/prmd/schema.rb', line 59

def schema_value_example(value)
  if value.has_key?('example')
    value['example']
  elsif value.has_key?('anyOf')
    ref = value['anyOf'].detect {|ref| ref['$ref'].split('/').last == 'id'} || value['anyOf'].first
    schema_example(ref)
  elsif value.has_key?('properties') # nested properties
    schema_example(value)
  elsif value.has_key?('items') # array of objects
    _, items = dereference(value['items'])
    if value['items'].has_key?('example')
      [items['example']]
    else
      [schema_example(items)]
    end
  end
end

#schemata_example(schemata_id) ⇒ Object



94
95
96
97
98
99
# File 'lib/prmd/schema.rb', line 94

def schemata_example(schemata_id)
  _, schema = dereference("#/definitions/#{schemata_id}")
  @schemata_examples[schemata_id] ||= begin
    schema_example(schema)
  end
end

#to_jsonObject



105
106
107
108
109
110
# File 'lib/prmd/schema.rb', line 105

def to_json
  new_json = JSON.pretty_generate(@data)
  # nuke empty lines
  new_json = new_json.split("\n").delete_if {|line| line.empty?}.join("\n") + "\n"
  new_json
end

#to_sObject



116
117
118
# File 'lib/prmd/schema.rb', line 116

def to_s
  to_json
end

#to_yamlObject



112
113
114
# File 'lib/prmd/schema.rb', line 112

def to_yaml
  YAML.dump(@data)
end