Class: OpenStax::Api::RepresentableSchemaPrinter
- Inherits:
-
Object
- Object
- OpenStax::Api::RepresentableSchemaPrinter
- Defined in:
- lib/openstax/api/representable_schema_printer.rb
Class Method Summary collapse
- .definition_name(name) ⇒ Object
- .json(representer, options = {}) ⇒ Object
- .json_object(representer, definitions, options = {}) ⇒ Object
- .json_schema(representer, options = {}) ⇒ Object
- .representer_name(representer) ⇒ Object
Class Method Details
.definition_name(name) ⇒ Object
31 32 33 |
# File 'lib/openstax/api/representable_schema_printer.rb', line 31 def self.definition_name(name) "#/definitions/#{name}" end |
.json(representer, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/openstax/api/representable_schema_printer.rb', line 12 def self.json(representer, ={}) [:include] ||= [:readable, :writeable] schema = json_schema(representer, ) json_string = JSON.pretty_generate(schema) "\n## Schema {##{SecureRandom.hex(4)} .schema}\n" + "\n<pre class='code'>\n#{json_string}\n</pre>\n" end |
.json_object(representer, definitions, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/openstax/api/representable_schema_printer.rb', line 35 def self.json_object(representer, definitions, = {}) schema = { type: :object, required: [], properties: {}, additionalProperties: false } representer.representable_attrs.each do |attr| name = attr[:as].evaluate(self) schema_info = attr[:schema_info] || {} schema[:required].push(name.to_sym) if schema_info[:required] # Skip unless attr includes the specified key or is required next unless [[:include]].flatten.any?{ |inc| m = inc.to_s + "?" attr.respond_to?(m) ? attr.send(m) : attr[inc] } || schema_info[:required] # Guess a default type based on the attribute name type = attr[:type].to_s.downcase type = type.blank? ? \ (name.end_with?('id') ? :integer : :string) : type attr_info ||= { type: type } schema_info.each do |key, value| next if key == :required if key == :definitions definitions.merge!(value) next end value = value.to_s.downcase if key == :type attr_info[key] = value end # Overwrite type for collections attr_info[:type] = 'array' if attr[:collection] if attr[:extend] # Nested representer decorator = attr[:extend].evaluate(self) rname = representer_name(decorator) if rname dname = definition_name(rname) if attr[:collection] attr_info[:items] = { :$ref => dname } else # Type is included in ref attr_info.delete(:type) attr_info[:$ref] = dname end if definitions[rname].nil? definitions[rname] = {} definitions[rname] = json_object(decorator, definitions, ) end else attr_info.merge!(json_object(decorator, definitions, )) end end schema[:properties][name.to_sym] = attr_info end # Cleanup unused fields [:required, :properties].each do |field| schema.delete(field) if schema[field].blank? end schema end |
.json_schema(representer, options = {}) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/openstax/api/representable_schema_printer.rb', line 5 def self.json_schema(representer, = {}) definitions = {} schema = json_object(representer, definitions, ) schema[:definitions] = definitions unless definitions.blank? schema end |
.representer_name(representer) ⇒ Object
25 26 27 28 29 |
# File 'lib/openstax/api/representable_schema_printer.rb', line 25 def self.representer_name(representer) name = representer.name return nil if name.nil? name.chomp('Representer').demodulize.camelize(:lower) end |