Class: Prmd::Schema

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

Overview

Schema object

Instance Method Summary collapse

Constructor Details

#initialize(new_data = {}) ⇒ Schema

Returns a new instance of Schema.

Parameters:

  • new_data (Hash<String, Object>) (defaults to: {})


12
13
14
15
# File 'lib/prmd/schema.rb', line 12

def initialize(new_data = {})
  @data = convert_type_to_array(new_data)
  @schemata_examples = {}
end

Instance Method Details

#[](key) ⇒ Object

Parameters:

  • key (String)

Returns:

  • (Object)


38
39
40
# File 'lib/prmd/schema.rb', line 38

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

#[]=(key, value) ⇒ Object

Parameters:

  • key (String)
  • value (Object)


44
45
46
# File 'lib/prmd/schema.rb', line 44

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

#dereference(reference) ⇒ Object

Parameters:

  • reference (Hash, String)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/prmd/schema.rb', line 62

def dereference(reference)
  if reference.is_a?(Hash)
    if reference.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(/[^#]*#\//, '').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({}, &:merge)
    ]
  rescue => error
    $stderr.puts("Failed to dereference `#{key}`")
    raise error
  end
end

#hrefString?

Retrieve this schema’s href

Returns:

  • (String, nil)


142
143
144
# File 'lib/prmd/schema.rb', line 142

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

#merge!(schema) ⇒ void

This method returns an undefined value.

Merge schema data with provided schema

Parameters:



52
53
54
55
56
57
58
# File 'lib/prmd/schema.rb', line 52

def merge!(schema)
  if schema.is_a?(Schema)
    @data.merge!(schema.data)
  else
    @data.merge!(schema)
  end
end

#schema_example(schema) ⇒ Object

Parameters:

  • schema (Hash, String)


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/prmd/schema.rb', line 114

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

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

#schema_value_example(value) ⇒ Object

Parameters:

  • value (Hash)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/prmd/schema.rb', line 92

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

#schemata_example(schemata_id) ⇒ Object

Parameters:

  • schemata_id (String)


132
133
134
135
136
137
# File 'lib/prmd/schema.rb', line 132

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

#to_jsonString

Convert Schema to JSON

Returns:

  • (String)


149
150
151
152
153
154
# File 'lib/prmd/schema.rb', line 149

def to_json
  new_json = JSON.pretty_generate(@data)
  # nuke empty lines
  new_json = new_json.split("\n").reject(&:empty?).join("\n") + "\n"
  new_json
end

#to_sString

Convert Schema to String

Returns:

  • (String)


166
167
168
# File 'lib/prmd/schema.rb', line 166

def to_s
  to_json
end

#to_yamlString

Convert Schema to YAML

Returns:

  • (String)


159
160
161
# File 'lib/prmd/schema.rb', line 159

def to_yaml
  YAML.dump(@data)
end