Class: RSence::Plugins::Servlet__

Inherits:
Object
  • Object
show all
Includes:
PluginBase
Defined in:
lib/rsence/plugins/servlet.rb

Overview

Use the Servlet class to create responders for GET / POST urls.

A Servlet’s public API is accessible like the other plugins directly.

Responding to a URL consists of four phases:

  1. PluginManager calls every #match method

  2. The plugins that return true to the #match method are queried by their score -method

  3. The matched plugins are sorted by score, lowest score wins. If it’s a draw between equal scores, the choice is randomized.

  4. The #post or #get method is called, depending on the type of HTTP request.

Extension hooks for server events

These methods are provided as the basic server event hooks:

  • #init – Use instead of initialize

  • #open – Extend to open objects

  • #flush – Extend to write the state and to flush buffers

  • #close – Extend to close objects

Extension hooks for REST events

  • #match – Extend to return true for certain url and request_type conditions.

  • #score – Extend to return a numeric score. Lower scores are “better” than higher ones.

  • #get – Extend to handle a HTTP GET request and response.

  • #post – Extend to handle a HTTP POST request and response.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PluginBase

#bundle_path, #close, #file_read, #file_write, #flush, #httime, #init, #method_missing, #open, #yaml_read, #yaml_write

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RSence::Plugins::PluginBase

Instance Attribute Details

#infoHash (readonly)

Returns The meta-information of the plugin bundle.

Returns:



60
61
62
# File 'lib/rsence/plugins/servlet.rb', line 60

def info
  @info
end

#nameSymbol (readonly)

Returns The name of the plugin bundle.

Returns:

  • (Symbol)

    The name of the plugin bundle



54
55
56
# File 'lib/rsence/plugins/servlet.rb', line 54

def name
  @name
end

#pathString (readonly)

Returns The absolute path of the plugin bundle.

Returns:

  • (String)

    The absolute path of the plugin bundle.



57
58
59
# File 'lib/rsence/plugins/servlet.rb', line 57

def path
  @path
end

Instance Method Details

#get(req, res, ses) ⇒ Object

Extend to do any GET request processing. Not doing anything by default.

Parameters:

  • req (Request)

    The HTTP Request object.

  • res (Response)

    The HTTP Response object.

  • ses (Hash)

    The session object, not implemented yet.



97
# File 'lib/rsence/plugins/servlet.rb', line 97

def get( req, res, ses ); end

#match(uri, request_type = :get) ⇒ true, false Also known as: match?

Extend to return true for the certain uri and request_type conditions your servlet code handles.

Examples:

Handles :get requests that begin with /foo

def match( uri, request_type )
  request_type == :get and uri.start_with?( '/foo' )
end

Parameters:

  • uri (String)

    The request uri (full “path” of the request url).

  • request_type (:get, :post) (defaults to: :get)

    The type of request. Only :get and :post are handled yet.

Returns:

  • (true)

    to match

  • (false)

    to not match. Returns false to everything, if not extended.



84
# File 'lib/rsence/plugins/servlet.rb', line 84

def match( uri, request_type=:get ); false; end

#post(req, res, ses) ⇒ Object

Extend to do any POST request processing. Not doing anything by default.

Parameters:

  • req (Request)

    The HTTP Request object.

  • res (Response)

    The HTTP Response object.

  • ses (Hash)

    The session object, not implemented yet.



104
# File 'lib/rsence/plugins/servlet.rb', line 104

def post( req, res, ses ); end

#scoreNumber

If matched, returns score where lower is better. Score is needed for priority sorting, when several Servlet’s #match are returning true for the same request.

Returns:

  • (Number)


90
# File 'lib/rsence/plugins/servlet.rb', line 90

def score; 100; end