### Home Classes Methods

### In Files

- lib/yard/server/router.rb

### Parent

Object

### Methods

- ::new
- #call
- #docs_prefix
- #final_options
- #list_prefix
- #parse_library_from_path
- #route
- #route_docs
- #route_index
- #route_list
- #route_search
- #search_prefix

### Included Modules

- YARD::Server::StaticCaching
- YARD::Server::Commands

### Files

- LICENSE.txt
- MIT.txt

### Class/Module Index

Quicksearch

- YARD
- YARD::CLI
- YARD::CLI::Command
- YARD::CLI::CommandParser
- YARD::CLI::Config
- YARD::CLI::Diff
- YARD::CLI::Gems
- YARD::CLI::Graph
- YARD::CLI::GraphOptions
- YARD::CLI::Help
- YARD::CLI::I18n
- YARD::CLI::List
- YARD::CLI::Server
- YARD::CLI::Stats
- YARD::CLI::YRI
- YARD::CLI::Yardoc
- YARD::CLI::YardocOptions
- YARD::CodeObjects
- YARD::CodeObjects::Base
- YARD::CodeObjects::ClassObject
- YARD::CodeObjects::ClassVariableObject
- YARD::CodeObjects::CodeObjectList
- YARD::CodeObjects::ConstantObject
- YARD::CodeObjects::ExtendedMethodObject
- YARD::CodeObjects::ExtraFileObject
- YARD::CodeObjects::MacroObject
- YARD::CodeObjects::MethodObject
- YARD::CodeObjects::ModuleObject
- YARD::CodeObjects::NamespaceObject
- YARD::CodeObjects::Proxy
- YARD::CodeObjects::ProxyMethodError
- YARD::CodeObjects::RootObject
- YARD::Config
- YARD::Docstring
- YARD::DocstringParser
- YARD::Handlers
- YARD::Handlers::Base
- YARD::Handlers::C
- YARD::Handlers::C::AliasHandler
- YARD::Handlers::C::AttributeHandler
- YARD::Handlers::C::Base
- YARD::Handlers::C::ClassHandler
- YARD::Handlers::C::ConstantHandler
- YARD::Handlers::C::HandlerMethods
- YARD::Handlers::C::InitHandler
- YARD::Handlers::C::MethodHandler
- YARD::Handlers::C::MixinHandler
- YARD::Handlers::C::ModuleHandler
- YARD::Handlers::C::OverrideCommentHandler
- YARD::Handlers::C::PathHandler
- YARD::Handlers::C::StructHandler
- YARD::Handlers::C::SymbolHandler
- YARD::Handlers::NamespaceMissingError
- YARD::Handlers::Processor
- YARD::Handlers::Ruby
- YARD::Handlers::Ruby::AliasHandler
- YARD::Handlers::Ruby::AttributeHandler
- YARD::Handlers::Ruby::Base
- YARD::Handlers::Ruby::ClassConditionHandler
- YARD::Handlers::Ruby::ClassHandler
- YARD::Handlers::Ruby::ClassVariableHandler
- YARD::Handlers::Ruby::CommentHandler
- YARD::Handlers::Ruby::ConstantHandler
- YARD::Handlers::Ruby::DSLHandler
- YARD::Handlers::Ruby::DSLHandlerMethods
- YARD::Handlers::Ruby::ExceptionHandler
- YARD::Handlers::Ruby::ExtendHandler
- YARD::Handlers::Ruby::HandlesExtension
- YARD::Handlers::Ruby::Legacy
- YARD::Handlers::Ruby::Legacy::AliasHandler
- YARD::Handlers::Ruby::Legacy::AttributeHandler
- YARD::Handlers::Ruby::Legacy::Base
- YARD::Handlers::Ruby::Legacy::ClassConditionHandler
- YARD::Handlers::Ruby::Legacy::ClassHandler
- YARD::Handlers::Ruby::Legacy::ClassVariableHandler
- YARD::Handlers::Ruby::Legacy::CommentHandler
- YARD::Handlers::Ruby::Legacy::ConstantHandler
- YARD::Handlers::Ruby::Legacy::DSLHandler
- YARD::Handlers::Ruby::Legacy::ExceptionHandler
- YARD::Handlers::Ruby::Legacy::ExtendHandler
- YARD::Handlers::Ruby::Legacy::MethodHandler
- YARD::Handlers::Ruby::Legacy::MixinHandler
- YARD::Handlers::Ruby::Legacy::ModuleFunctionHandler
- YARD::Handlers::Ruby::Legacy::ModuleHandler
- YARD::Handlers::Ruby::Legacy::PrivateConstantHandler
- YARD::Handlers::Ruby::Legacy::VisibilityHandler
- YARD::Handlers::Ruby::Legacy::YieldHandler
- YARD::Handlers::Ruby::MethodCallWrapper
- YARD::Handlers::Ruby::MethodConditionHandler
- YARD::Handlers::Ruby::MethodHandler
- YARD::Handlers::Ruby::MixinHandler
- YARD::Handlers::Ruby::ModuleFunctionHandler
- YARD::Handlers::Ruby::ModuleHandler
- YARD::Handlers::Ruby::PrivateConstantHandler
- YARD::Handlers::Ruby::StructHandlerMethods
- YARD::Handlers::Ruby::TestNodeWrapper
- YARD::Handlers::Ruby::VisibilityHandler
- YARD::Handlers::Ruby::YieldHandler
- YARD::I18n
- YARD::I18n::Locale
- YARD::I18n::Message
- YARD::I18n::Messages
- YARD::I18n::PotGenerator
- YARD::I18n::Text
- YARD::Logger
- YARD::Options
- YARD::Parser
- YARD::Parser::Base
- YARD::Parser::C
- YARD::Parser::C::BodyStatement
- YARD::Parser::C::CParser
- YARD::Parser::C::Comment
- YARD::Parser::C::CommentParser
- YARD::Parser::C::Statement
- YARD::Parser::C::ToplevelStatement
- YARD::Parser::OrderedParser
- YARD::Parser::ParserSyntaxError
- YARD::Parser::Ruby
- YARD::Parser::Ruby::AstNode
- YARD::Parser::Ruby::ClassNode
- YARD::Parser::Ruby::CommentNode
- YARD::Parser::Ruby::ConditionalNode
- YARD::Parser::Ruby::KeywordNode
- YARD::Parser::Ruby::Legacy
- YARD::Parser::Ruby::Legacy::RubyLex
- YARD::Parser::Ruby::Legacy::RubyLex::BufferedReader
- YARD::Parser::Ruby::Legacy::RubyParser
- YARD::Parser::Ruby::Legacy::RubyToken
- YARD::Parser::Ruby::Legacy::RubyToken::TkBlockContents
- YARD::Parser::Ruby::Legacy::RubyToken::TkError
- YARD::Parser::Ruby::Legacy::RubyToken::TkId
- YARD::Parser::Ruby::Legacy::RubyToken::TkKW
- YARD::Parser::Ruby::Legacy::RubyToken::TkNode
- YARD::Parser::Ruby::Legacy::RubyToken::TkOPASGN
- YARD::Parser::Ruby::Legacy::RubyToken::TkOp
- YARD::Parser::Ruby::Legacy::RubyToken::TkStatementEnd
- YARD::Parser::Ruby::Legacy::RubyToken::TkUnknownChar
- YARD::Parser::Ruby::Legacy::RubyToken::TkVal
- YARD::Parser::Ruby::Legacy::RubyToken::TkWhitespace
- YARD::Parser::Ruby::Legacy::RubyToken::Token
- YARD::Parser::Ruby::Legacy::Statement
- YARD::Parser::Ruby::Legacy::StatementList
- YARD::Parser::Ruby::Legacy::TokenList
- YARD::Parser::Ruby::LiteralNode
- YARD::Parser::Ruby::LoopNode
- YARD::Parser::Ruby::MethodCallNode
- YARD::Parser::Ruby::MethodDefinitionNode
- YARD::Parser::Ruby::ModuleNode
- YARD::Parser::Ruby::ParameterNode
- YARD::Parser::Ruby::ReferenceNode
- YARD::Parser::Ruby::RipperParser
- YARD::Parser::Ruby::RubyParser
- YARD::Parser::SourceParser
- YARD::Parser::UndocumentableError
- YARD::Rake
- YARD::Rake::YardocTask
- YARD::Registry
- YARD::RegistryStore
- YARD::Serializers
- YARD::Serializers::Base
- YARD::Serializers::FileSystemSerializer
- YARD::Serializers::ProcessSerializer
- YARD::Serializers::StdoutSerializer
- YARD::Serializers::YardocSerializer
- YARD::Server
- YARD::Server::Adapter
- YARD::Server::Commands
- YARD::Server::Commands::Base
- YARD::Server::Commands::DisplayFileCommand
- YARD::Server::Commands::DisplayObjectCommand
- YARD::Server::Commands::FramesCommand
- YARD::Server::Commands::LibraryCommand
- YARD::Server::Commands::LibraryIndexCommand
- YARD::Server::Commands::LibraryOptions
- YARD::Server::Commands::ListCommand
- YARD::Server::Commands::SearchCommand
- YARD::Server::Commands::StaticFileCommand
- YARD::Server::DocServerHelper
- YARD::Server::DocServerSerializer
- YARD::Server::FinishRequest
- YARD::Server::LibraryNotPreparedError
- YARD::Server::LibraryVersion
- YARD::Server::NotFoundError
- YARD::Server::RackAdapter
- YARD::Server::RackMiddleware
- YARD::Server::Router
- YARD::Server::StaticCaching
- YARD::Server::WebrickAdapter
- YARD::Server::WebrickServlet
- YARD::StubProxy
- YARD::Tags
- YARD::Tags::AttributeDirective
- YARD::Tags::DefaultFactory
- YARD::Tags::DefaultTag
- YARD::Tags::Directive
- YARD::Tags::EndGroupDirective
- YARD::Tags::GroupDirective
- YARD::Tags::Library
- YARD::Tags::MacroDirective
- YARD::Tags::MethodDirective
- YARD::Tags::OptionTag
- YARD::Tags::OverloadTag
- YARD::Tags::ParseDirective
- YARD::Tags::RefTag
- YARD::Tags::RefTagList
- YARD::Tags::ScopeDirective
- YARD::Tags::Tag
- YARD::Tags::TagFormatError
- YARD::Tags::VisibilityDirective
- YARD::Templates
- YARD::Templates::Engine
- YARD::Templates::ErbCache
- YARD::Templates::Helpers
- YARD::Templates::Helpers::BaseHelper
- YARD::Templates::Helpers::FilterHelper
- YARD::Templates::Helpers::HtmlHelper
- YARD::Templates::Helpers::HtmlSyntaxHighlightHelper
- YARD::Templates::Helpers::Markup
- YARD::Templates::Helpers::Markup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::MarkupHelper
- YARD::Templates::Helpers::MethodHelper
- YARD::Templates::Helpers::ModuleHelper
- YARD::Templates::Helpers::TextHelper
- YARD::Templates::Helpers::UMLHelper
- YARD::Templates::Section
- YARD::Templates::Template
- YARD::Templates::Template::ClassMethods
- YARD::Templates::TemplateOptions
- YARD::Verifier
- Gem
- Gem::DocManager
- Gem::SourceIndex
- Gem::Specification
- Rack
- Rack::Request
- WEBrick
- WEBrick::HTTPRequest
- Array
- File
- Hash
- Insertion
- Module
- Object
- String
- SymbolHash

No matching classes.

# YARD::Server::Router

A router class implements the logic used to recognize a request for a specific URL and run specific {Commands::Base commands}.

## Subclassing Notes

To create a custom router, subclass this class and pass it into the adapter options through {Adapter#initialize} or by directly modifying {Adapter#router}.

The most general customization is to change the URL prefixes recognized by routing, which can be done by overriding {docs_prefix}, {list_prefix} and {search_prefix}.

## Implementing Custom Caching

By default, the Router class performs static disk-based caching on all requests through the `#check_static_cache`. To override this behaviour, or create your own caching mechanism, mixin your own custom module with this method implemented as per {StaticCaching#check_static_cache}.

@example Creating a subclassed router

    # Adds 'my' to all routing prefixes
    class MyRouter < YARD::Server::Router
      def docs_prefix; 'mydocs' end
      def list_prefix; 'mylist' end
      def search_prefix; 'mysearch' end
    end

    # Using it:
    WebrickAdapter.new(libraries, :router => MyRouter).start

### Attributes

adapter[RW]

@return [Adapter] the adapter used by the router

request[RW]

@return [Adapter Dependent] the request data coming in with the routing

### Public Class Methods

new(adapter) click to toggle source

Creates a new router for a specific adapter

@param [Adapter] adapter the adapter to route requests to

    # File lib/yard/server/router.rb, line 43
    def initialize(adapter)
      self.adapter = adapter
    end

### Public Instance Methods

call(request) click to toggle source

Perform routing on a specific request, serving the request as a static file through {Commands::StaticFileCommand} if no route is found.

@param [Adapter Dependent] request the request object @return [Array(Numeric,Hash,Array)] the Rack-style server response data

    # File lib/yard/server/router.rb, line 52
    def call(request)
      self.request = request
      if result = (check_static_cache || route)
        result
      else
        StaticFileCommand.new(adapter.options).call(request)
      end
    end

docs_prefix() click to toggle source

@return [String] the URI prefix for all object documentation requests

    # File lib/yard/server/router.rb, line 64
    def docs_prefix; 'docs' end

list_prefix() click to toggle source

@return [String] the URI prefix for all class/method/file list requests

    # File lib/yard/server/router.rb, line 67
    def list_prefix; 'list' end

parse_library_from_path(paths) click to toggle source

@return [Array(LibraryVersion, Array<String>)] the library followed

    by the rest of the path components in the request path. LibraryVersion
    will be nil if no matching library was found.

    # File lib/yard/server/router.rb, line 77
    def parse_library_from_path(paths)
      return [adapter.libraries.values.first.first, paths] if adapter.options[:single_library]
      library, paths = nil, paths.dup
      if libs = adapter.libraries[paths.first]
        paths.shift
        if library = libs.find {|l| l.version == paths.first }
          request.version_supplied = true if request
          paths.shift
        else # use the last lib in the list
          request.version_supplied = false if request
          library = libs.last
        end
      end
      [library, paths]
    end

search_prefix() click to toggle source

@return [String] the URI prefix for all search requests

    # File lib/yard/server/router.rb, line 70
    def search_prefix; 'search' end

### Protected Instance Methods

final_options(library, paths) click to toggle source

Adds extra :library/:path option keys to the adapter options. Use this method when passing options to a command.

@param (see route_docs) @return [Hash] finalized options

    # File lib/yard/server/router.rb, line 171
    def final_options(library, paths)
      adapter.options.merge(:library => library, :path => paths.join('/'))
    end

route(path = request.path) click to toggle source

Performs routing algorithm to find which prefix is called, first parsing out library name/version information.

@return [Array(Numeric,Hash,Array<String>)] the Rack-style response @return [nil] if no route is matched

    # File lib/yard/server/router.rb, line 100
    def route(path = request.path)
      path = path.gsub(%{//+}, '/').gsub(%{^/|/$}, '')
      return route_index if path.empty? || path == docs_prefix
      case path
      when /^(#{docs_prefix}|#{list_prefix}|#{search_prefix})(\/.*|$)/
        prefix = $1
        paths = $2.gsub(%{^/|/$}, '').split('/')
        library, paths = *parse_library_from_path(paths)
        return unless library
        return case prefix
        when docs_prefix;   route_docs(library, paths)
        when list_prefix;   route_list(library, paths)
        when search_prefix; route_search(library, paths)
        end
      end
      nil
    end

route_docs(library, paths) click to toggle source

Routes requests from {docs_prefix} and calls the appropriate command @param [LibraryVersion] library the library to route for @param [Array<String>] paths path components (split by '/') @return (see route)

    # File lib/yard/server/router.rb, line 122
    def route_docs(library, paths)
      return route_index if library.nil?
      case paths.first
      when "frames"
        paths.shift
        cmd = FramesCommand
      when "file"
        paths.shift
        cmd = DisplayFileCommand
      else
        cmd = DisplayObjectCommand
      end
      cmd = cmd.new(final_options(library, paths))
      cmd.call(request)
    end

route_index() click to toggle source

Routes for the index of a library / multiple libraries @return (see route)

    # File lib/yard/server/router.rb, line 140
    def route_index
      if adapter.options[:single_library]
        route_docs(adapter.libraries.values.first.first, [])
      else
        LibraryIndexCommand.new(adapter.options.merge(:path => '')).call(request)
      end
    end

route_list(library, paths) click to toggle source

Routes requests from {list_prefix} and calls the appropriate command @param (see route_docs) @return (see route_docs)

    # File lib/yard/server/router.rb, line 151
    def route_list(library, paths)
      return if paths.empty?
      ListCommand.new(final_options(library, paths)).call(request)
    end

route_search(library, paths) click to toggle source

Routes requests from {search_prefix} and calls the appropriate command @param (see route_docs) @return (see route_docs)

    # File lib/yard/server/router.rb, line 159
    def route_search(library, paths)
      return unless paths.empty?
      SearchCommand.new(final_options(library, paths)).call(request)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.