Module: Anorexic::DSL

Defined in:
lib/anorexic/base/dsl.rb

Overview

this module contains the methods that are used as a DSL and sets up easy access to the Anorexic framework.

use the`listen`, `host` and `route` functions rather then accessing this object.

Class Method Summary collapse

Class Method Details

.host(host_name, params) ⇒ Object

adds a host to the last server created

accepts the same parameter(s) as the `listen` command (see Anorexic.add_service), except :protocol and :handler are ignored:

alias

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


68
69
70
# File 'lib/anorexic/base/dsl.rb', line 68

def host(host_name, params)
	@active_router.add_host host_name, params
end

.listen(port, params = {}) ⇒ Object

adds a server (performs the action required by the listen method).

accepts:

port

(optional) the port number for the service. if not defined, defaultes to the -p runtime argument (i.e. `./app.rb -p 8080`) or to 3000 if argument is missing.

params

any parameter accepted by the Anorexic.add_service method. defaults to: `:protocol=>Anorexic::HTTPProtocol, :handler => HTTPRouter.new`


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/anorexic/base/dsl.rb', line 19

def listen(port, params = {})
	# set port and arguments
	if port.is_a?(Hash)
		params = port
		port = nil
	end
	if !port && defined? ARGV
		if ARGV.find_index('-p')
			port_index = ARGV.find_index('-p') + 1
			port ||= ARGV[port_index].to_i
			ARGV[port_index] = (port + 1).to_s
		else
			ARGV << '-p'
			ARGV << '3000'
			return listen nil, params
		end
	end
	port ||= 3000

	# create new service or choose existing
	if @servers[port]
		puts "WARNING: port aleady in use! returning existing service and attemptin to add host (maybe multiple hosts? use `host` instead)." unless params[:host]
		@active_router = @servers[port][:handler]
		@active_router.add_host params[:host], params
		return @active_router
	end
	params[:protocol] ||= HTTPProtocol
	@active_router = params[:handler] ||=  HTTPRouter.new # HTTPEcho #
	@active_router.add_host params[:host], params
	return false unless Anorexic.add_service(port, params)
	@servers[port] = params
	@active_router
end

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

adds a route to the last server created


54
55
56
# File 'lib/anorexic/base/dsl.rb', line 54

def route(path, controller = nil, &block)
	@active_router.add_route path, controller, &block
end

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

adds a shared route to all existing services and hosts.


60
61
62
# File 'lib/anorexic/base/dsl.rb', line 60

def shared_route(path, controller = nil, &block)
	@servers.values.each {|p| p[:handler].add_shared_route path, controller, &block }
end