class Builder

Builds an immutable route table.

Nested Classes and Modules

each

Definitions

def initialize

Initialize a route builder.

Implementation

def initialize
	@routes = {}
end

def routes

A frozen snapshot of the configured routes.

Signature

returns Hash

The route table.

Implementation

def routes
	@routes.transform_values do |handlers|
		handlers.dup.freeze
	end.freeze
end

def route(path, handler = nil, methods: nil, &block)

Add a route.

When methods is omitted, the handler accepts every HTTP method. Otherwise, it may be a single method or an array of methods.

Signature

parameter path String

The absolute path to match.

parameter handler Interface(:call) | Nil

A callable route handler.

parameter methods String | Symbol | Array(String | Symbol) | Nil

The accepted HTTP methods.

yields {|request| ...}

The route handler.

parameter request Protocol::HTTP::Request

The original request.

returns Builder

The builder.

Implementation

def route(path, handler = nil, methods: nil, &block)
	raise ArgumentError, "Provide a route handler or block, not both!" if handler && block
	handler ||= block
	raise ArgumentError, "A route handler is required!" unless handler
	
	path = route_path(path)
	methods = route_methods(methods)
	handlers = (@routes[path] ||= {})
	
	methods.each do |method|
		if handlers.key?(method)
			raise ArgumentError, "Route already defined for #{method || "any method"} #{path}!"
		end
		
		handlers[method] = handler
	end
	
	return self
end