class Jdoc::Schema
Constants
- DEFAULT_ENDPOINT
Public Class Methods
new(schema)
click to toggle source
@param schema [Hash] JSON Schema
# File lib/jdoc/schema.rb, line 6 def initialize(schema) @json_schema = JsonSchema.parse!(schema).tap(&:expand_references!) end
Public Instance Methods
description()
click to toggle source
@return [String, nil] Description property of this schema @example
schema.description #=> "A schema for a small example API."
# File lib/jdoc/schema.rb, line 27 def description if @json_schema.description "#{@json_schema.description}\n\n" end end
host_with_port()
click to toggle source
@return [String] @example
host_with_port #=> "api.example.com" host_with_port #=> "api.example.com:3000"
# File lib/jdoc/schema.rb, line 37 def host_with_port if [80, 443].include?(port) host else "#{host}:#{port}" end end
resources()
click to toggle source
@return [Array<Jdoc::Resource>] All top-level properties in its title order
# File lib/jdoc/schema.rb, line 11 def resources @resources ||= @json_schema.properties.map do |key, property| Resource.new(property) end.sort end
title()
click to toggle source
@return [String, nil] Title property of this schema @example
schema.title #=> "app"
# File lib/jdoc/schema.rb, line 20 def title @json_schema.title end
Private Instance Methods
host()
click to toggle source
@return [String] Host name of API, used at dummy Host header @example
schema.host #=> "api.example.com"
# File lib/jdoc/schema.rb, line 55 def host root_uri.host end
link_for_root_endpoint()
click to toggle source
@return [JsonSchema::Schema::Link]
# File lib/jdoc/schema.rb, line 73 def link_for_root_endpoint @json_schema.links.find do |link| link.rel == "self" && link.href end end
port()
click to toggle source
@return [Fixnum] Port number for this API endpoint
# File lib/jdoc/schema.rb, line 48 def port root_uri.port || 80 end
root_uri()
click to toggle source
@return [URI::Generic] Root endpoint for the API @example
root_uri #=> "https://api.example.com"
# File lib/jdoc/schema.rb, line 62 def root_uri @root_endpoint = begin if link = link_for_root_endpoint URI(link.href) else URI(DEFAULT_ENDPOINT) end end end