Class: YARD::Server::Router
- Inherits:
-
Object
- Object
- YARD::Server::Router
- Includes:
- Commands, StaticCaching
- 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.
Instance Attribute Summary collapse
-
#adapter ⇒ Adapter
The adapter used by the router.
-
#request ⇒ Adapter Dependent
The request data coming in with the routing.
Route Prefixes collapse
-
#docs_prefix ⇒ String
The URI prefix for all object documentation requests.
-
#list_prefix ⇒ String
The URI prefix for all class/method/file list requests.
-
#search_prefix ⇒ String
The URI prefix for all search requests.
-
#static_prefix ⇒ String
The URI prefix for all static assets (templates).
Routing Methods collapse
-
#parse_library_from_path(paths) ⇒ Array(LibraryVersion, Array<String>)
The library followed by the rest of the path components in the request path.
Instance Method Summary collapse
-
#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.
-
#initialize(adapter) ⇒ Router
constructor
Creates a new router for a specific adapter.
Methods included from StaticCaching
Constructor Details
#initialize(adapter) ⇒ Router
Creates a new router for a specific adapter
45 46 47 |
# File 'lib/yard/server/router.rb', line 45 def initialize(adapter) self.adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Adapter
Returns the adapter used by the router.
40 41 42 |
# File 'lib/yard/server/router.rb', line 40 def adapter @adapter end |
#request ⇒ Adapter Dependent
Returns the request data coming in with the routing.
37 38 39 |
# File 'lib/yard/server/router.rb', line 37 def request @request end |
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.
54 55 56 57 58 |
# File 'lib/yard/server/router.rb', line 54 def call(request) self.request = request result = check_static_cache || route result ? result : RootRequestCommand.new(adapter.).call(request) end |
#docs_prefix ⇒ String
Returns the URI prefix for all object documentation requests.
63 |
# File 'lib/yard/server/router.rb', line 63 def docs_prefix; 'docs' end |
#list_prefix ⇒ String
Returns the URI prefix for all class/method/file list requests.
66 |
# File 'lib/yard/server/router.rb', line 66 def list_prefix; 'list' end |
#parse_library_from_path(paths) ⇒ Array(LibraryVersion, Array<String>)
Returns the library followed by the rest of the path components in the request path. LibraryVersion will be nil if no matching library was found.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/yard/server/router.rb', line 79 def parse_library_from_path(paths) return [adapter.libraries.values.first.first, paths] if adapter.[: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_prefix ⇒ String
Returns the URI prefix for all search requests.
69 |
# File 'lib/yard/server/router.rb', line 69 def search_prefix; 'search' end |
#static_prefix ⇒ String
Returns the URI prefix for all static assets (templates).
72 |
# File 'lib/yard/server/router.rb', line 72 def static_prefix; 'static' end |