123456789_123456789_123456789_123456789_123456789_

Class: YARD::Server::Router

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/yard/server/router.rb

Overview

A router class implements the logic used to recognize a request for a specific URL and run specific 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, #static_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.

Examples:

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 static_prefix; 'mystatic' end
  def search_prefix; 'mysearch' end
end

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

Since:

  • 0.6.0

Route Prefixes

Routing Methods

Class Method Summary

Instance Attribute Summary

Instance Method Summary

StaticCaching - Included

#check_static_cache

Called by a router to return the cached object.

Constructor Details

.new(adapter) ⇒ Router

Creates a new router for a specific adapter

Parameters:

  • adapter (Adapter)

    the adapter to route requests to

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 45

def initialize(adapter)
  self.adapter = adapter
end

Instance Attribute Details

#adapterAdapter (rw)

Returns:

  • (Adapter)

    the adapter used by the router

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 40

attr_accessor :adapter

#requestAdapter Dependent (rw)

Returns:

  • (Adapter Dependent)

    the request data coming in with the routing

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 37

attr_accessor :request

Instance Method Details

#call(request) ⇒ Array(Numeric,Hash,Array)

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

Parameters:

  • request (Adapter Dependent)

    the request object

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 54

def call(request)
  self.request = request
  result = check_static_cache || route
  result ? result : RootRequestCommand.new(adapter.options).call(request)
end

#docs_prefixString

Returns:

  • (String)

    the URI prefix for all object documentation requests

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 63

def docs_prefix; 'docs' end

#list_prefixString

Returns:

  • (String)

    the URI prefix for all class/method/file list requests

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 66

def list_prefix; 'list' end

#parse_library_from_path(paths) ⇒ Array(LibraryVersion, Array<String>)

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 79

def parse_library_from_path(paths)
  return [adapter.libraries.values.first.first, paths] if adapter.options[:single_library]
  library = nil
  paths = paths.dup
  libs = adapter.libraries[paths.first]
  if libs
    paths.shift
    library = libs.find {|l| l.version == paths.first }
    if library
      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_prefixString

Returns:

  • (String)

    the URI prefix for all search requests

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 69

def search_prefix; 'search' end

#static_prefixString

Returns:

  • (String)

    the URI prefix for all static assets (templates)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/router.rb', line 72

def static_prefix; 'static' end