Top Level Namespace

Defined Under Namespace

Modules: Anorexic

Constant Summary collapse

AN =

Set a shortcut for the Anorexic module.

Anorexic

Instance Method Summary collapse

Instance Method Details

#host(host_name = false, params = {}) ⇒ Object

adds a virtul host to the current service (the last `listen` call) or switches to an existing host within the active service.

accepts: host_name: a String with the full host name (i.e. “www.google.com” / “mail.google.com”)

params

any of the parameters accepted by the `listen` command, except `protocol`, `handler`, and `ssl` parameters.


108
109
110
# File 'lib/anorexic/base/dsl.rb', line 108

def host(host_name = false, params = {})
	Anorexic::DSL.host host_name, params
end

#listen(port = nil, params = {}) ⇒ Object

creates a server object and waits for routes to be set.

port

the port to listen to. the first port defaults to 3000 and increments by 1 with every `listen` call. it's possible to set the first port number by running the app with the -p paramater.

params

a Hash of serever paramaters, as listed in the Anorexic#add_service documentation.

The different keys in the params hash control the server's behaviour, as follows:

host

the host name. defaults to any host not explicitly defined (a catch-all).

alias

a String or an Array of Strings which represent alternative host names (i.e. `alias: [“admin.google.com”, “admin.gmail.com”]`).

root

the public root folder. if this is defined, static files will be served from the location.

assets

the assets root folder. defaults to nil (no assets support). if the path is defined, assets will be served from `/assets/…` (or the public_asset path defined) before any static files. assets will not be served if the file in the /public/assets folder if up to date (a rendering attempt will be made for systems that allow file writing).

assets_public

the assets public uri location (uri format, NOT a file path). defaults to `/assets`. assets will be saved (or rendered) to the assets public folder and served as static files.

assets_callback

a method that accepts one parameters: `request` and renders any custom assets. the method should return `false` unless it has created a response object (`response = Anorexic::HTTPResponse.new(request)`) and sent a response to the client using `response.finish`.

save_assets

saves the rendered assets to the filesystem, under the public folder. defaults to false.

templates

the templates root folder. defaults to nil (no template support). templates can be rendered by a Controller class, using the `render` method.

ssl

if true, an SSL service will be attempted. if no certificate is defined, an attempt will be made to create a self signed certificate.

ssl_key

the public key for the SSL service.

ssl_cert

the certificate for the SSL service.


99
100
101
# File 'lib/anorexic/base/dsl.rb', line 99

def listen(port = nil, params = {})
	Anorexic::DSL.listen port, params
end

#route(path, controller = nil, &block) ⇒ Object

adds a route to the last server object

path

the path for the route

controller

The controller class which will accept the route.

`path` paramaters has a few options:

  • `path` can be a Regexp object, forcing the all the logic into controller (typically using the before method).

  • simple String paths are assumed to be basic RESTful paths:

    route "/users", Controller => route "/users/(:id)", Controller
    
  • routes can define their own parameters, for their own logic:

    route "/path/:required_paramater/:required_paramater{with_format}/(:optional_paramater)/(:optional){with_format}"
    
  • routes can define optional or required routes with regular expressions in them:

    route "(:locale){en|ru}/path"
    
  • routes which use the special '/' charecter within a parameter's format, must escape this charecter using the '' charecter. **Notice the single quotes** in the following example:

    route '(:math){[\d\+\-\*\^\%\.\/]}'
    
    • or, with double quotes:

      route “(:math)[\d+\-*\^\%\[\d+\-*\^\%\.\/]”

magic routes make for difficult debugging - the smarter the routes, the more difficult the debugging. use with care and avoid complex routes when possible. RESTful routes are recommended when possible. json serving apps are advised to use required paramaters, empty sections indicating missing required paramaters (i.e. /path///foo/bar///).


145
146
147
# File 'lib/anorexic/base/dsl.rb', line 145

def route(path, controller = nil, &block)
	Anorexic::DSL.route(path, controller, &block)
end

#shared_route(path, controller = nil, &block) ⇒ Object

adds a route to the all the existing servers and hosts.

accepts same options as route.


152
153
154
# File 'lib/anorexic/base/dsl.rb', line 152

def shared_route(path, controller = nil, &block)
	Anorexic::DSL.shared_route(path, controller, &block)
end

#start_servicesObject

finishes setup of the servers and starts them up. This will hange the proceess.

this method is called automatically by the Anorexic framework.

it is recommended that you DO NOT CALL this method. if any post shut-down actions need to be performed, use Anorexic.on_shutdown instead.


162
163
164
165
166
167
168
169
170
171
# File 'lib/anorexic/base/dsl.rb', line 162

def start_services
	return 0 if ( defined?(NO_ANOREXIC_AUTO_START) || defined?(BUILDING_ANOREXIC_TEMPLATE) || defined?(ANOREXIC_ON_RACK) )
	Object.const_set "NO_ANOREXIC_AUTO_START", true
	undef listen
	undef host
	undef route
	undef shared_route
	undef start_services
	Anorexic.start_services
end