Class: YARD::Server::Commands::Base

### Home Classes Methods

### In Files

- lib/yard/server/commands/base.rb

### Parent

Object

### Methods

- ::new
- #cache
- #call
- #not_found
- #redirect
- #render
- #run

### Files

- LICENSE.txt
- MIT.txt

### Class/Module Index

Quicksearch

- YARD
- YARD::CLI
- YARD::CLI::Command
- YARD::CLI::CommandParser
- YARD::CLI::Config
- YARD::CLI::Diff
- YARD::CLI::Display
- YARD::CLI::Gems
- YARD::CLI::Graph
- YARD::CLI::GraphOptions
- YARD::CLI::Help
- YARD::CLI::I18n
- YARD::CLI::List
- YARD::CLI::MarkupTypes
- YARD::CLI::Server
- YARD::CLI::Stats
- YARD::CLI::YRI
- YARD::CLI::Yardoc
- YARD::CLI::YardocOptions
- YARD::CLI::YardoptsCommand
- YARD::CodeObjects
- YARD::CodeObjects::Base
- YARD::CodeObjects::ClassObject
- YARD::CodeObjects::ClassVariableObject
- YARD::CodeObjects::CodeObjectList
- YARD::CodeObjects::ConstantObject
- YARD::CodeObjects::ExtendedMethodObject
- YARD::CodeObjects::ExtraFileObject
- YARD::CodeObjects::MacroObject
- YARD::CodeObjects::MethodObject
- YARD::CodeObjects::ModuleObject
- YARD::CodeObjects::NamespaceObject
- YARD::CodeObjects::Proxy
- YARD::CodeObjects::ProxyMethodError
- YARD::CodeObjects::RootObject
- YARD::Config
- YARD::Docstring
- YARD::DocstringParser
- YARD::Handlers
- YARD::Handlers::Base
- YARD::Handlers::C
- YARD::Handlers::C::AliasHandler
- YARD::Handlers::C::AttributeHandler
- YARD::Handlers::C::Base
- YARD::Handlers::C::ClassHandler
- YARD::Handlers::C::ConstantHandler
- YARD::Handlers::C::HandlerMethods
- YARD::Handlers::C::InitHandler
- YARD::Handlers::C::MethodHandler
- YARD::Handlers::C::MixinHandler
- YARD::Handlers::C::ModuleHandler
- YARD::Handlers::C::OverrideCommentHandler
- YARD::Handlers::C::PathHandler
- YARD::Handlers::C::StructHandler
- YARD::Handlers::C::SymbolHandler
- YARD::Handlers::HandlerAborted
- YARD::Handlers::NamespaceMissingError
- YARD::Handlers::Processor
- YARD::Handlers::Ruby
- YARD::Handlers::Ruby::AliasHandler
- YARD::Handlers::Ruby::AttributeHandler
- YARD::Handlers::Ruby::Base
- YARD::Handlers::Ruby::ClassConditionHandler
- YARD::Handlers::Ruby::ClassHandler
- YARD::Handlers::Ruby::ClassVariableHandler
- YARD::Handlers::Ruby::CommentHandler
- YARD::Handlers::Ruby::ConstantHandler
- YARD::Handlers::Ruby::DSLHandler
- YARD::Handlers::Ruby::DSLHandlerMethods
- YARD::Handlers::Ruby::ExceptionHandler
- YARD::Handlers::Ruby::ExtendHandler
- YARD::Handlers::Ruby::HandlesExtension
- YARD::Handlers::Ruby::Legacy
- YARD::Handlers::Ruby::Legacy::AliasHandler
- YARD::Handlers::Ruby::Legacy::AttributeHandler
- YARD::Handlers::Ruby::Legacy::Base
- YARD::Handlers::Ruby::Legacy::ClassConditionHandler
- YARD::Handlers::Ruby::Legacy::ClassHandler
- YARD::Handlers::Ruby::Legacy::ClassVariableHandler
- YARD::Handlers::Ruby::Legacy::CommentHandler
- YARD::Handlers::Ruby::Legacy::ConstantHandler
- YARD::Handlers::Ruby::Legacy::DSLHandler
- YARD::Handlers::Ruby::Legacy::ExceptionHandler
- YARD::Handlers::Ruby::Legacy::ExtendHandler
- YARD::Handlers::Ruby::Legacy::MethodHandler
- YARD::Handlers::Ruby::Legacy::MixinHandler
- YARD::Handlers::Ruby::Legacy::ModuleFunctionHandler
- YARD::Handlers::Ruby::Legacy::ModuleHandler
- YARD::Handlers::Ruby::Legacy::PrivateClassMethodHandler
- YARD::Handlers::Ruby::Legacy::PrivateConstantHandler
- YARD::Handlers::Ruby::Legacy::VisibilityHandler
- YARD::Handlers::Ruby::Legacy::YieldHandler
- YARD::Handlers::Ruby::MethodCallWrapper
- YARD::Handlers::Ruby::MethodConditionHandler
- YARD::Handlers::Ruby::MethodHandler
- YARD::Handlers::Ruby::MixinHandler
- YARD::Handlers::Ruby::ModuleFunctionHandler
- YARD::Handlers::Ruby::ModuleHandler
- YARD::Handlers::Ruby::PrivateClassMethodHandler
- YARD::Handlers::Ruby::PrivateConstantHandler
- YARD::Handlers::Ruby::StructHandlerMethods
- YARD::Handlers::Ruby::TestNodeWrapper
- YARD::Handlers::Ruby::VisibilityHandler
- YARD::Handlers::Ruby::YieldHandler
- YARD::I18n
- YARD::I18n::Locale
- YARD::I18n::Message
- YARD::I18n::Messages
- YARD::I18n::POParser
- YARD::I18n::PotGenerator
- YARD::I18n::Text
- YARD::Logger
- YARD::Options
- YARD::Parser
- YARD::Parser::Base
- YARD::Parser::C
- YARD::Parser::C::BodyStatement
- YARD::Parser::C::CParser
- YARD::Parser::C::Comment
- YARD::Parser::C::CommentParser
- YARD::Parser::C::Statement
- YARD::Parser::C::ToplevelStatement
- YARD::Parser::OrderedParser
- YARD::Parser::ParserSyntaxError
- YARD::Parser::Ruby
- YARD::Parser::Ruby::AstNode
- YARD::Parser::Ruby::ClassNode
- YARD::Parser::Ruby::CommentNode
- YARD::Parser::Ruby::ConditionalNode
- YARD::Parser::Ruby::KeywordNode
- YARD::Parser::Ruby::Legacy
- YARD::Parser::Ruby::Legacy::RubyLex
- YARD::Parser::Ruby::Legacy::RubyLex::BufferedReader
- YARD::Parser::Ruby::Legacy::RubyParser
- YARD::Parser::Ruby::Legacy::RubyToken
- YARD::Parser::Ruby::Legacy::RubyToken::TkBlockContents
- YARD::Parser::Ruby::Legacy::RubyToken::TkError
- YARD::Parser::Ruby::Legacy::RubyToken::TkId
- YARD::Parser::Ruby::Legacy::RubyToken::TkKW
- YARD::Parser::Ruby::Legacy::RubyToken::TkNode
- YARD::Parser::Ruby::Legacy::RubyToken::TkOPASGN
- YARD::Parser::Ruby::Legacy::RubyToken::TkOp
- YARD::Parser::Ruby::Legacy::RubyToken::TkStatementEnd
- YARD::Parser::Ruby::Legacy::RubyToken::TkUnknownChar
- YARD::Parser::Ruby::Legacy::RubyToken::TkVal
- YARD::Parser::Ruby::Legacy::RubyToken::TkWhitespace
- YARD::Parser::Ruby::Legacy::RubyToken::Token
- YARD::Parser::Ruby::Legacy::Statement
- YARD::Parser::Ruby::Legacy::StatementList
- YARD::Parser::Ruby::Legacy::TokenList
- YARD::Parser::Ruby::LiteralNode
- YARD::Parser::Ruby::LoopNode
- YARD::Parser::Ruby::MethodCallNode
- YARD::Parser::Ruby::MethodDefinitionNode
- YARD::Parser::Ruby::ModuleNode
- YARD::Parser::Ruby::ParameterNode
- YARD::Parser::Ruby::ReferenceNode
- YARD::Parser::Ruby::RipperParser
- YARD::Parser::Ruby::RubyParser
- YARD::Parser::SourceParser
- YARD::Parser::UndocumentableError
- YARD::Rake
- YARD::Rake::YardocTask
- YARD::Registry
- YARD::RegistryStore
- YARD::Serializers
- YARD::Serializers::Base
- YARD::Serializers::FileSystemSerializer
- YARD::Serializers::ProcessSerializer
- YARD::Serializers::StdoutSerializer
- YARD::Serializers::YardocSerializer
- YARD::Server
- YARD::Server::Adapter
- YARD::Server::Commands
- YARD::Server::Commands::Base
- YARD::Server::Commands::DisplayFileCommand
- YARD::Server::Commands::DisplayObjectCommand
- YARD::Server::Commands::FramesCommand
- YARD::Server::Commands::LibraryCommand
- YARD::Server::Commands::LibraryIndexCommand
- YARD::Server::Commands::LibraryOptions
- YARD::Server::Commands::ListCommand
- YARD::Server::Commands::SearchCommand
- YARD::Server::Commands::StaticFileCommand
- YARD::Server::DocServerHelper
- YARD::Server::DocServerSerializer
- YARD::Server::FinishRequest
- YARD::Server::LibraryNotPreparedError
- YARD::Server::LibraryVersion
- YARD::Server::NotFoundError
- YARD::Server::RackAdapter
- YARD::Server::RackMiddleware
- YARD::Server::Router
- YARD::Server::StaticCaching
- YARD::Server::WebrickAdapter
- YARD::Server::WebrickServlet
- YARD::StubProxy
- YARD::Tags
- YARD::Tags::AttributeDirective
- YARD::Tags::DefaultFactory
- YARD::Tags::DefaultTag
- YARD::Tags::Directive
- YARD::Tags::EndGroupDirective
- YARD::Tags::GroupDirective
- YARD::Tags::Library
- YARD::Tags::MacroDirective
- YARD::Tags::MethodDirective
- YARD::Tags::OptionTag
- YARD::Tags::OverloadTag
- YARD::Tags::ParseDirective
- YARD::Tags::RefTag
- YARD::Tags::RefTagList
- YARD::Tags::ScopeDirective
- YARD::Tags::Tag
- YARD::Tags::TagFormatError
- YARD::Tags::VisibilityDirective
- YARD::Templates
- YARD::Templates::Engine
- YARD::Templates::ErbCache
- YARD::Templates::Helpers
- YARD::Templates::Helpers::BaseHelper
- YARD::Templates::Helpers::FilterHelper
- YARD::Templates::Helpers::HtmlHelper
- YARD::Templates::Helpers::HtmlSyntaxHighlightHelper
- YARD::Templates::Helpers::Markup
- YARD::Templates::Helpers::Markup::RDocMarkdown
- YARD::Templates::Helpers::Markup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::Markup::RDocMarkup::RDocMarkupToHtml
- YARD::Templates::Helpers::MarkupHelper
- YARD::Templates::Helpers::MethodHelper
- YARD::Templates::Helpers::ModuleHelper
- YARD::Templates::Helpers::TextHelper
- YARD::Templates::Helpers::UMLHelper
- YARD::Templates::Section
- YARD::Templates::Template
- YARD::Templates::Template::ClassMethods
- YARD::Templates::TemplateOptions
- YARD::Verifier
- Gem
- Gem::DocManager
- Gem::SourceIndex
- Gem::Specification
- Rack
- Rack::Request
- WEBrick
- WEBrick::HTTPRequest
- Array
- File
- Hash
- Insertion
- Module
- Object
- String
- SymbolHash

No matching classes.

# YARD::Server::Commands::Base

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.

@abstract @see run

### Attributes

adapter[RW]

@return [Adapter] the server adapter

body[RW]

@return [String] the response body. Defaults to empty string.

caching[RW]

@return [Boolean] whether to cache

command_options[RW]

@return [Hash] the options passed to the command's constructor

headers[RW]

@return [Hash{String => String}] response headers

path[RW]

@return [String] the path after the command base URI

request[RW]

@return [Request] request object

status[RW]

@return [Numeric] status code. Defaults to 200 per request

### Public Class Methods

new(opts = {}) click to toggle source

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.

@example Creating a Command

    cmd = DisplayObjectCommand.new(:caching => true, :library => mylib)
    cmd.library # => mylib
    cmd.command_options # => {:caching => true, :library => mylib}

@param [Hash] opts the options hash, saved to {command_options}

    after initialization.

    # 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.command_options = opts
    end

### Public Instance Methods

call(request) click to toggle source

The main method called by a router with a request object.

@note This command should not be overridden by subclasses. Implement

    the callback method {#run} instead.

@param [Adapter Dependent] request the request object @return [Array(Numeric,Hash,Array<String>)] a Rack-style response

    of status, headers, and body wrapped in an array.

    # 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.message if e.message != e.class.to_s
        self.status = 404
      end
      not_found if status == 404
      [status, headers, body.is_a?(Array) ? body : [body]]
    end

run() click to toggle source

Subclass this method to implement a custom command. This method should set the {status} and {body}, and optionally modify the {headers}. Note that `#status` defaults to 200.

@example A custom command

    class ErrorCommand < Base
      def run
        self.body = 'ERROR! The System is down!'
        self.status = 500
        self.headers['Conten-Type'] = 'text/plain'
      end
    end

@abstract @return [void]

    # File lib/yard/server/commands/base.rb, line 122
    def run
      raise NotImplementedError
    end

### Protected Instance Methods

cache(data) click to toggle source

Override this method to implement custom caching mechanisms for

@example Caching to memory

    $memory_cache = {}
    def cache(data)
      $memory_cache[path] = data
    end

@param [String] data the data to cache @return [String] the same cached data (for chaining) @see StaticCaching

    # 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(%{/\.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

not_found() click to toggle source

Sets the body and headers (but not status) for a 404 response. Does nothing if the body is already set.

@return [void]

    # 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

redirect(url) click to toggle source

Sets the headers and status code for a redirection to a given URL @param [String] url the URL to redirect to @raise [FinishRequest] causes the request to terminate.

    # File lib/yard/server/commands/base.rb, line 184
    def redirect(url)
      headers['Location'] = url
      self.status = 302
      raise FinishRequest
    end

render(object = nil) click to toggle source

Renders a specific object if provided, or a regular template rendering if object is not provided.

@todo This method is dependent on `#options`, it should be in {LibraryCommand}. @param [CodeObjects::Base, nil] object calls {CodeObjects::Base#format} if

    an object is provided, or {Templates::Engine.render} if object is nil. Both
    receive +#options+ as an argument.

@return [String] the resulting output to display

    # File lib/yard/server/commands/base.rb, line 138
    def render(object = nil)
      case object
      when CodeObjects::Base
        cache object.format(options)
      when nil
        cache Templates::Engine.render(options)
      else
        cache object
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.