Class: YARD::Server::Router

Inherits:
Object
  • Object
show all
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.

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

Instance Attribute Summary collapse

Route Prefixes collapse

Routing Methods collapse

Utility Methods collapse

Instance Method Summary collapse

Methods included from StaticCaching

#check_static_cache

Constructor Details

#initialize(adapter) ⇒ Router

Creates a new router for a specific adapter

Parameters:

  • adapter (Adapter)

    the adapter to route requests to

Since:

  • 0.6.0


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

def initialize(adapter)
  self.adapter = adapter
end

Instance Attribute Details

#adapterAdapter

Returns the adapter used by the router.

Returns:

  • (Adapter)

    the adapter used by the router

Since:

  • 0.6.0


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

def adapter
  @adapter
end

#requestAdapter Dependent

Returns the request data coming in with the routing.

Returns:

  • (Adapter Dependent)

    the request data coming in with the routing

Since:

  • 0.6.0


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

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.

Parameters:

  • request (Adapter Dependent)

    the request object

Returns:

Since:

  • 0.6.0


53
54
55
56
57
58
59
60
# File 'lib/yard/server/router.rb', line 53

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

#docs_prefixString

Returns the URI prefix for all object documentation requests.

Returns:

  • (String)

    the URI prefix for all object documentation requests

Since:

  • 0.6.0


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

def docs_prefix; 'docs' end

#final_options(library, paths) ⇒ Hash (protected)

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

Parameters:

Returns:

  • (Hash)

    finalized options

Since:

  • 0.6.0


180
181
182
183
# File 'lib/yard/server/router.rb', line 180

def final_options(library, paths)
  path = File.cleanpath(paths.join('/')).gsub(%r{^(\.\./)+}, '')
  adapter.options.merge(:library => library, :path => path)
end

#list_prefixString

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

Returns:

  • (String)

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

Since:

  • 0.6.0


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

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.

Returns:

  • (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.

Since:

  • 0.6.0


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yard/server/router.rb', line 81

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

#route(path = request.path_info) ⇒ Array(Numeric,Hash,Array<String>)? (protected)

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

Returns:

Since:

  • 0.6.0


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/yard/server/router.rb', line 104

def route(path = request.path_info)
  path = path.gsub(%r{//+}, '/').gsub(%r{^/|/$}, '')
  return route_index if path.empty? || path == docs_prefix
  case path
  when /^(#{docs_prefix}|#{list_prefix}|#{search_prefix}|#{static_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)
    when static_prefix; route_static(library, paths)
    end
  end
  nil
end

#route_docs(library, paths) ⇒ Array(Numeric,Hash,Array<String>)? (protected)

Routes requests from #docs_prefix and calls the appropriate command

Parameters:

Returns:

Since:

  • 0.6.0


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/yard/server/router.rb', line 127

def route_docs(library, paths)
  return route_index if library.nil?
  case paths.first
  when "frames"
    paths.shift
    cmd = DisplayObjectCommand
  when "file"
    paths.shift
    cmd = DisplayFileCommand
  else
    cmd = DisplayObjectCommand
  end
  cmd = cmd.new(final_options(library, paths))
  cmd.call(request)
end

#route_indexArray(Numeric,Hash,Array<String>)? (protected)

Routes for the index of a library / multiple libraries

Returns:

Since:

  • 0.6.0


145
146
147
148
149
150
151
# File 'lib/yard/server/router.rb', line 145

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) ⇒ Array(Numeric,Hash,Array<String>)? (protected)

Routes requests from #list_prefix and calls the appropriate command

Parameters:

Returns:

Since:

  • 0.6.0


156
157
158
159
# File 'lib/yard/server/router.rb', line 156

def route_list(library, paths)
  return if paths.empty?
  ListCommand.new(final_options(library, paths)).call(request)
end

#route_search(library, paths) ⇒ Array(Numeric,Hash,Array<String>)? (protected)

Routes requests from #search_prefix and calls the appropriate command

Parameters:

Returns:

Since:

  • 0.6.0


164
165
166
167
# File 'lib/yard/server/router.rb', line 164

def route_search(library, paths)
  return unless paths.empty?
  SearchCommand.new(final_options(library, paths)).call(request)
end

#route_static(library, paths) ⇒ Object (protected)

Since:

  • 0.6.0


169
170
171
# File 'lib/yard/server/router.rb', line 169

def route_static(library, paths)
  StaticFileCommand.new(final_options(library, paths)).call(request)
end

#search_prefixString

Returns the URI prefix for all search requests.

Returns:

  • (String)

    the URI prefix for all search requests

Since:

  • 0.6.0


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

def search_prefix; 'search' end

#static_prefixString

Returns the URI prefix for all static assets (templates).

Returns:

  • (String)

    the URI prefix for all static assets (templates)

Since:

  • 0.6.0


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

def static_prefix; 'static' end