123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/yard/server/commands/base.rb

Overview

This class is abstract.

This is the base command class used to implement custom commands for a server. A command will be routed to by the ::YARD::Server::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, => '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.

See Also:

Since:

  • 0.6.0

Basic Command and Adapter Options

Attributes Set Per Request

Instance Method Summary

Abstract Methods

  • #run ⇒ void abstract

    Subclass this method to implement a custom command.

Helper Methods

  • #add_cache_control private

    Add a conservative cache control policy to reduce load on requests served with "?1234567890" style timestamp query strings.

Constructor Details

.new(opts = {}) ⇒ Base

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.

Examples:

Creating a Command

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

Parameters:

  • opts (Hash) (defaults to: {})

    the options hash, saved to #command_options after initialization.

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 75

def initialize(opts = {})
  opts.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
  self.command_options = opts
end

Instance Attribute Details

#adapterAdapter (rw)

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 41

attr_accessor :adapter

#bodyString (rw)

Returns:

  • (String)

    the response body. Defaults to empty string.

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 61

attr_accessor :body

#cachingBoolean (rw)

Returns:

  • (Boolean)

    whether to cache

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 44

attr_accessor :caching

#command_optionsHash (rw)

Returns:

  • (Hash)

    the options passed to the command's constructor

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 38

attr_accessor :command_options

#headersHash{String => String} (rw)

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 55

attr_accessor :headers

#pathString (rw)

Returns:

  • (String)

    the path after the command base URI

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 52

attr_accessor :path

#requestRack::Request (rw)

Returns:

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 49

attr_accessor :request

#statusNumeric (rw)

Returns:

  • (Numeric)

    status code. Defaults to 200 per request

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 58

attr_accessor :status

Instance Method Details

#add_cache_control (private)

Add a conservative cache control policy to reduce load on requests served with "?1234567890" style timestamp query strings.

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 202

def add_cache_control
  return if request.query_string.to_i == 0
  headers['Cache-Control'] ||= 'public, max-age=300'
end

#call(request) ⇒ Array(Numeric,Hash,Array<String>)

Note:

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.

Parameters:

  • request (Adapter Dependent)

    the request object

Returns:

  • (Array(Numeric,Hash,Array<String>))

    a Rack-style response of status, headers, and body wrapped in an array.

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 89

def call(request)
  self.request = request
  self.path ||= request.path_info[1..-1]
  self.headers = {'Content-Type' => 'text/html'}
  self.body = ''
  self.status = 200
  add_cache_control
  begin
    run
  rescue FinishRequest
    nil # noop
  rescue NotFoundError => e
    self.body = e.message if e.message != e.class.to_s
    not_found
  end

  # keep this to support commands setting status manually.
  not_found if status == 404

  [status, headers, body.is_a?(Array) ? body : [body]]
end

#runvoid

This method is abstract.

This method returns an undefined value.

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.

Examples:

A custom command

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

Raises:

  • (NotImplementedError)

Since:

  • 0.6.0

[ GitHub ]

  
# File 'lib/yard/server/commands/base.rb', line 128

def run
  raise NotImplementedError
end