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 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.
-
- (Adapter Dependent) request
The request data coming in with the routing.
Route Prefixes (collapse)
-
- (String) docs_prefix
The URI prefix for all object documentation requests.
-
- (String) list_prefix
The URI prefix for all class/method/file list requests.
-
- (String) search_prefix
The URI prefix for all search requests.
Routing Methods (collapse)
-
- (Array(LibraryVersion, Array<String>)) parse_library_from_path(paths)
The library followed by the rest of the path components in the request path.
-
- (Array(Numeric,Hash,Array<String>)?) route
protected
Performs routing algorithm to find which prefix is called, first parsing out library name/version information.
-
- (Array(Numeric,Hash,Array<String>)?) route_docs(library, paths)
protected
Routes requests from #docs_prefix and calls the appropriate command.
-
- (Array(Numeric,Hash,Array<String>)?) route_index
protected
Routes for the index of a library / multiple libraries.
-
- (Array(Numeric,Hash,Array<String>)?) route_list(library, paths)
protected
Routes requests from #list_prefix and calls the appropriate command.
-
- (Array(Numeric,Hash,Array<String>)?) route_search(library, paths)
protected
Routes requests from #search_prefix and calls the appropriate command.
Utility Methods (collapse)
-
- (Hash) final_options(library, paths)
protected
Adds extra :library/:path option keys to the adapter options.
Instance Method Summary (collapse)
-
- (Array(Number,Hash,Array)) call(request)
Perform routing on a specific request, serving the request as a static file through Commands::StaticFileCommand if no route is found.
-
- (Router) initialize(adapter)
constructor
Creates a new router for a specific adapter.
Methods included from StaticCaching
Constructor Details
- (Router) initialize(adapter)
Creates a new router for a specific adapter
43 44 45 |
# File 'lib/yard/server/router.rb', line 43 def initialize(adapter) self.adapter = adapter end |
Instance Attribute Details
- (Adapter) adapter
The adapter used by the router
38 39 40 |
# File 'lib/yard/server/router.rb', line 38 def adapter @adapter end |
- (Adapter Dependent) request
The request data coming in with the routing
35 36 37 |
# File 'lib/yard/server/router.rb', line 35 def request @request end |
Instance Method Details
- (Array(Number,Hash,Array)) call(request)
Perform routing on a specific request, serving the request as a static file through Commands::StaticFileCommand if no route is found.
52 53 54 55 56 57 58 59 |
# 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.).call(request) end end |
- (String) docs_prefix
The URI prefix for all object documentation requests
64 |
# File 'lib/yard/server/router.rb', line 64 def docs_prefix; 'docs' end |
- (Hash) final_options(library, paths) (protected)
Adds extra :library/:path option keys to the adapter options. Use this method when passing options to a command.
175 176 177 |
# File 'lib/yard/server/router.rb', line 175 def (library, paths) adapter..merge(:library => library, :path => paths.join('/')) end |
- (String) list_prefix
The URI prefix for all class/method/file list requests
67 |
# File 'lib/yard/server/router.rb', line 67 def list_prefix; 'list' end |
- (Array(LibraryVersion, Array<String>)) parse_library_from_path(paths)
The library followed by the rest of the path components in the request path. LibraryVersion will be nil if no matching library was found.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/yard/server/router.rb', line 77 def parse_library_from_path(paths) return [adapter.libraries.values.first.first, paths] if adapter.[:single_library] library, paths = nil, paths.dup if libs = adapter.libraries[paths.first] paths.shift if library = libs.find {|l| l.version == paths.first } paths.shift else # use the last lib in the list library = libs.last end end [library, paths] end |
- (Array(Numeric,Hash,Array<String>)?) route (protected)
Performs routing algorithm to find which prefix is called, first parsing out library name/version information.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/yard/server/router.rb', line 98 def route path = request.path.gsub(%r{//+}, '/').gsub(%r{^/|/$}, '') return route_index if path.empty? || path == docs_prefix case path when /^(#{docs_prefix}|#{list_prefix}|#{search_prefix})(\/.*|$)/ prefix = $1 paths = $2.gsub(%r{^/|/$}, '').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 |
- (Array(Numeric,Hash,Array<String>)?) route_docs(library, paths) (protected)
Routes requests from #docs_prefix and calls the appropriate command
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/yard/server/router.rb', line 120 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((library, paths)) cmd.call(request) end |
- (Array(Numeric,Hash,Array<String>)?) route_index (protected)
Routes for the index of a library / multiple libraries
138 139 140 141 142 143 144 |
# File 'lib/yard/server/router.rb', line 138 def route_index if adapter.[:single_library] route_docs(adapter.libraries.values.first.first, []) else LibraryIndexCommand.new(adapter..merge(:path => '')).call(request) end end |
- (Array(Numeric,Hash,Array<String>)?) route_list(library, paths) (protected)
Routes requests from #list_prefix and calls the appropriate command
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/yard/server/router.rb', line 149 def route_list(library, paths) return if paths.empty? case paths.shift when "class"; cmd = ListClassesCommand when "methods"; cmd = ListMethodsCommand when "files"; cmd = ListFilesCommand else; return end cmd.new((library, paths)).call(request) end |
- (Array(Numeric,Hash,Array<String>)?) route_search(library, paths) (protected)
Routes requests from #search_prefix and calls the appropriate command
163 164 165 166 |
# File 'lib/yard/server/router.rb', line 163 def route_search(library, paths) return unless paths.empty? SearchCommand.new((library, paths)).call(request) end |
- (String) search_prefix
The URI prefix for all search requests
70 |
# File 'lib/yard/server/router.rb', line 70 def search_prefix; 'search' end |