Class: YARD::Server::Commands::Base Abstract
- Inherits:
-
Object
- Object
- YARD::Server::Commands::Base
- Defined in:
- lib/yard/server/commands/base.rb
Overview
This is the base command class used to implement custom commands for a server. A command will be routed to by the Router class and return a Rack-style response.
Attribute Initializers
All attributes can be initialized via options passed into the #initialize method. When creating a custom command, the Adapter#options will automatically be mapped to attributes by the same name on your class.
class MyCommand < Base
attr_accessor :myattr
end
Adapter.new(libs, {:myattr => 'foo'}).start
# when a request comes in, cmd.myattr == 'foo'
Subclassing Notes
To implement a custom command, override the #run method, not #call. In your implementation, you should set the body and status for requests. See details in the #run method documentation.
Note that if your command deals directly with libraries, you should consider subclassing the more specific LibraryCommand class instead.
Direct Known Subclasses
Basic Command and Adapter Options (collapse)
-
- (Adapter) adapter
The server adapter.
-
- (Boolean) caching
Whether to cache.
-
- (Hash) command_options
The options passed to the command's constructor.
Attributes Set Per Request (collapse)
-
- (String) body
The response body.
-
- (Hash{String => String}) headers
Response headers.
-
- (String) path
The path after the command base URI.
-
- (Request) request
Request object.
-
- (Numeric) status
Status code.
Instance Method Summary (collapse)
-
- (Array(Numeric,Hash,Array<String>)) call(request)
The main method called by a router with a request object.
-
- (Base) initialize(opts = {})
constructor
Creates a new command object, setting attributes named by keys in the options hash.
Abstract Methods (collapse)
-
- (void) run
abstract
Subclass this method to implement a custom command.
Helper Methods (collapse)
-
- (String) cache(data)
protected
Override this method to implement custom caching mechanisms for.
-
- (void) not_found
protected
Sets the body and headers (but not status) for a 404 response.
-
- (Object) redirect(url)
protected
Sets the headers and status code for a redirection to a given URL.
-
- (String) render(object = nil)
protected
Renders a specific object if provided, or a regular template rendering if object is not provided.
Constructor Details
- (Base) initialize(opts = {})
Creates a new command object, setting attributes named by keys in the options hash. After initialization, the options hash is saved in #command_options for further inspection.
74 75 76 77 78 79 |
# File 'lib/yard/server/commands/base.rb', line 74 def initialize(opts = {}) opts.each do |key, value| send("#{key}=", value) if respond_to?("#{key}=") end self. = opts end |
Instance Attribute Details
- (Adapter) adapter
Returns the server adapter
40 41 42 |
# File 'lib/yard/server/commands/base.rb', line 40 def adapter @adapter end |
- (String) body
Returns the response body. Defaults to empty string.
60 61 62 |
# File 'lib/yard/server/commands/base.rb', line 60 def body @body end |
- (Boolean) caching
Returns whether to cache
43 44 45 |
# File 'lib/yard/server/commands/base.rb', line 43 def caching @caching end |
- (Hash) command_options
Returns the options passed to the command’s constructor
37 38 39 |
# File 'lib/yard/server/commands/base.rb', line 37 def @command_options end |
- (Hash{String => String}) headers
Returns response headers
54 55 56 |
# File 'lib/yard/server/commands/base.rb', line 54 def headers @headers end |
- (String) path
Returns the path after the command base URI
51 52 53 |
# File 'lib/yard/server/commands/base.rb', line 51 def path @path end |
- (Request) request
Returns request object
48 49 50 |
# File 'lib/yard/server/commands/base.rb', line 48 def request @request end |
- (Numeric) status
Returns status code. Defaults to 200 per request
57 58 59 |
# File 'lib/yard/server/commands/base.rb', line 57 def status @status end |
Instance Method Details
- (String) cache(data) (protected)
Override this method to implement custom caching mechanisms for
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/yard/server/commands/base.rb', line 159 def cache(data) if caching && adapter.document_root path = File.join(adapter.document_root, request.path.sub(/\.html$/, '') + '.html') path = path.sub(%r{/\.html$}, '.html') FileUtils.mkdir_p(File.dirname(path)) log.debug "Caching data to #{path}" File.open(path, 'wb') {|f| f.write(data) } end self.body = data end |
- (Array(Numeric,Hash,Array<String>)) call(request)
This command should not be overridden by subclasses. Implement the callback method #run instead.
The main method called by a router with a request object.
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yard/server/commands/base.rb', line 88 def call(request) self.request = request self.path ||= request.path[1..-1] self.headers = {'Content-Type' => 'text/html'} self.body = '' self.status = 200 begin run rescue FinishRequest rescue NotFoundError => e self.body = e. if e. != e.class.to_s self.status = 404 end not_found if status == 404 [status, headers, body.is_a?(Array) ? body : [body]] end |
- (void) not_found (protected)
This method returns an undefined value.
Sets the body and headers (but not status) for a 404 response. Does nothing if the body is already set.
174 175 176 177 178 179 |
# File 'lib/yard/server/commands/base.rb', line 174 def not_found return unless body.empty? self.body = "Not found: #{request.path}" self.headers['Content-Type'] = 'text/plain' self.headers['X-Cascade'] = 'pass' end |
- (Object) redirect(url) (protected)
Sets the headers and status code for a redirection to a given URL
184 185 186 187 188 |
# File 'lib/yard/server/commands/base.rb', line 184 def redirect(url) headers['Location'] = url self.status = 302 raise FinishRequest end |
- (String) render(object = nil) (protected)
This method is dependent on #options, it should be in LibraryCommand.
Renders a specific object if provided, or a regular template rendering if object is not provided.
138 139 140 141 142 143 144 145 146 147 |
# File 'lib/yard/server/commands/base.rb', line 138 def render(object = nil) case object when CodeObjects::Base cache object.format() when nil cache Templates::Engine.render() else cache object end end |
- (void) run
122 123 124 |
# File 'lib/yard/server/commands/base.rb', line 122 def run raise NotImplementedError end |