Module: Regal::RoutesDsl

Included in:
Route
Defined in:
lib/regal/app.rb

Handlers collapse

Instance Method Summary collapse

Instance Method Details

#after {|request, response| ... } ⇒ void

This method returns an undefined value.

Register an after block for this route.

After blocks run after the request handler and have access to the request and response.

A route can have any number of after blocks and they will be called in the order that they are declared with the innermost route's after blocks being called first, followed by the parent route.

Yields:

  • (request, response)

Yield Parameters:



154
155
156
157
# File 'lib/regal/app.rb', line 154

def after(&block)
  @afters << block
  nil
end

#any {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for any request method

any handlers are called when there is no specific handler in this route for the request method.

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



266
267
268
# File 'lib/regal/app.rb', line 266

def any(&block)
  @handlers.default = block
end

#before {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a before block for this route.

Before blocks run before the request handler and have access to the request and response.

A route can have any number of before blocks and they will be called in the order that they are declared with the outermost route's before blocks being called first, followed by child routes.

Yields:

  • (request, response)

Yield Parameters:



136
137
138
139
# File 'lib/regal/app.rb', line 136

def before(&block)
  @befores << block
  nil
end

#delete {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for DELETE requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 220

#get {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for GET requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 193

#head {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for HEAD requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 202

#mount(app) ⇒ void

This method returns an undefined value.

Mount a child app.

Mounting a child app makes that app's routes available as if they were defined in this route.

Parameters:



107
108
109
110
# File 'lib/regal/app.rb', line 107

def mount(app)
  @mounted_apps << app
  nil
end

#options {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for OPTIONS requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 211

#patch {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for PATCH requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 247

#post {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for POST requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 229

#put {|request, response| ... } ⇒ void

This method returns an undefined value.

Register a handler for PUT requests to this route

Yields:

  • (request, response)

Yield Parameters:

Yield Returns:

  • (Object)

    the response body



# File 'lib/regal/app.rb', line 238

#rescue_from(type) {|error, request, response| ... } ⇒ void

This method returns an undefined value.

Register a rescue block for this route.

Rescue blocks run when a before or after block, or a handler raises an error that matches the block's error type (compared using #===).

A route can have any number of rescue blocks, but the order they are declared in is important. When an error is raised the blocks are searched in reverse order for a match, so the last declared rescue block with a matching type will be the one to handle the error.

Once an error handler has been called the error is assumed to have been handled. If the error handler raises an error, the next matching handler will be found, or the error will bubble up outside the app.

Parameters:

  • type (Class<Error>)

Yields:

  • (error, request, response)

Yield Parameters:



179
180
181
182
# File 'lib/regal/app.rb', line 179

def rescue_from(type, &block)
  @rescuers << [type, block]
  nil
end

#route(s) { ... } ⇒ void

This method returns an undefined value.

Defines a route.

A route is either static or dynamic. Static routes match request path components verbatim, whereas dynamic routes captures their value. A static route is defined by a string and a dynamic route by a symbol.

When routes are matched during request handling a static route will match if it is exactly equal to the next path component. A dynamic route will always match. All static routes are tried before any dynamic route.

A route can only have a single dynamic child route. If you declare multiple dynamic routes only the last one is kept.

Parameters:

  • s (String, Symbol)

    either a string, which creates a static route that matches a path component exactly, or a symbol, which captures the value of the path component.

Yields:

  • []



90
91
92
93
94
95
96
97
98
# File 'lib/regal/app.rb', line 90

def route(s, &block)
  r = Class.new(self).create(s, &block)
  if s.is_a?(Symbol)
    @routes.default = r
  else
    @routes[s] = r
  end
  nil
end

#scope { ... } ⇒ void

This method returns an undefined value.

Wrap the child routes and handlers in a scope.

Scopes can have before, after and rescue blocks that are not shared with sibling scopes. They work more or less like mounting apps, but inline.

Yields:

  • []



119
120
121
# File 'lib/regal/app.rb', line 119

def scope(&block)
  mount(Class.new(self).create(&block))
end