class Rack::JsonSchema::Docs::DocumentGenerator

Public Class Methods

call(*args) click to toggle source
# File lib/rack/json_schema/docs.rb, line 37
def self.call(*args)
  new(*args).call
end
new(app: nil, env: nil, html: nil, markdown: nil, path: nil) click to toggle source

@param app [Object] Rack application @param env [Hash] Rack env @param html [String] HTML rendered docs @param markdown [String] Markdown rendered docs @param path [String] Route for docs

# File lib/rack/json_schema/docs.rb, line 46
def initialize(app: nil, env: nil, html: nil, markdown: nil, path: nil)
  @app = app
  @env = env
  @html = html
  @markdown = markdown
  @path = path
end

Public Instance Methods

call() click to toggle source

Generates suited response body from given env & document to docs request @return [Array] Rack response

# File lib/rack/json_schema/docs.rb, line 56
def call
  if has_docs_request?
    if has_markdown_request?
      markdown_response
    else
      html_response
    end
  else
    delegate
  end
end

Private Instance Methods

delegate() click to toggle source

Delegates request to given rack app

# File lib/rack/json_schema/docs.rb, line 71
def delegate
  @app.call(@env)
end
extname() click to toggle source

@return [String] Extension name of request path @example

extname #=> ".md"
# File lib/rack/json_schema/docs.rb, line 88
def extname
  ::File.extname(path)
end
has_docs_request?() click to toggle source

@return [true, false] True if docs are requested

# File lib/rack/json_schema/docs.rb, line 76
def has_docs_request?
  request_method == "GET" && path_without_extname == @path
end
has_markdown_request?() click to toggle source

@return [true, false] True if raw markdown content are requested

# File lib/rack/json_schema/docs.rb, line 81
def has_markdown_request?
  extname == ".md"
end
html_response() click to toggle source

@return [Array] Rack response of human readable HTML document, rendered from given document

# File lib/rack/json_schema/docs.rb, line 117
def html_response
  [
    200,
    { "Content-Type" => "text/html; charset=utf-8" },
    [@html],
  ]
end
markdown_response() click to toggle source

@return [Array] Rack response of raw markdown text

# File lib/rack/json_schema/docs.rb, line 108
def markdown_response
  [
    200,
    { "Content-Type" => "text/plain; charset=utf-8" },
    [@markdown],
  ]
end
path() click to toggle source

@return [String] Request path

# File lib/rack/json_schema/docs.rb, line 93
def path
  @env["PATH_INFO"]
end
path_without_extname() click to toggle source

@return [String]

# File lib/rack/json_schema/docs.rb, line 103
def path_without_extname
  path.gsub(/\..+\z/, "")
end
request_method() click to toggle source

@return [String]

# File lib/rack/json_schema/docs.rb, line 98
def request_method
  @env["REQUEST_METHOD"]
end