class Moonrope::DocServer

Constants

CONTENT_TYPES

Attributes

path_regex[W]
base[R]

Public Class Methods

new(app, base, options = {}) click to toggle source
# File lib/moonrope/doc_server.rb, line 23
def initialize(app, base, options = {})
  @app = app
  @base = base
  @options = options
end
path_regex() click to toggle source

Set the default path regex which should be matched for requests for API docmentation. By default, this is /api/docs/.

# File lib/moonrope/doc_server.rb, line 17
def path_regex
  @path_regex ||= /\A\/#{Moonrope::Request.path_prefix}docs\/([\w\.]+)\/?([\w\/\-\.]+)?/
end

Public Instance Methods

call(env) click to toggle source
# File lib/moonrope/doc_server.rb, line 64
def call(env)
  if env['PATH_INFO'] =~ self.class.path_regex
    version = $1
    doc_path = $2
    request = Rack::Request.new(env)
    generator = Generator.new(@base, :host => "#{request.scheme}://#{request.host_with_port}", :version => version, :prefix => env['PATH_INFO'].split('/')[1])

    if @options[:reload_on_each_request]
      @base.load
    end

    file = nil
    content_type = nil

    case doc_path
    when nil, ""
      return [302, {'Location' => "#{env['PATH_INFO']}/welcome"}, ['']]
    when /\Awelcome\z/, /\Aindex\.html\z/
      file = generator.generate_file(doc_path, 'index')
    when /\Acontrollers\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        file = generator.generate_file(doc_path, 'controller', :controller => controller)
      end
    when /\Acontrollers\/(\w+)\/(\w+)(\.html)?\z/
      if controller = @base.controller($1.to_sym)
        if action = controller.action($2.to_sym)
          file = generator.generate_file(doc_path, 'action', :controller => controller, :action => action)
        end
      end
    when /\Astructures\/(\w+)(\.html)?\z/
      if structure = @base.structure($1.to_sym)
        file = generator.generate_file(doc_path, 'structure', :structure => structure)
      end
    when /\Aauthenticators\/(\w+)(\.html)?\z/
      if authenticator = @base.authenticators[$1.to_sym]
        file = generator.generate_file(doc_path, 'authenticator', :authenticator => authenticator)
      end
    when /\Aassets\/([\w]+)\.([a-z]+)\z/
      path = File.join(generator.template_root_path, 'assets', "#{$1}.#{$2}")
      if File.exist?(path)
        file = File.read(path)
        content_type = CONTENT_TYPES[$2] || 'text/plain'
      end
    end

    if file
      [200, {
        'Content-Type' => content_type || 'text/html',
        'Content-Length' => file.bytesize.to_s},
      [file]]
    else
      [404, {}, ['Not found']]
    end
  else
    return @app.call(env)
  end
end