Module: Kawaii::RoutingMethods

Included in:
Base, RouteContext
Defined in:
lib/kawaii/routing_methods.rb

Overview

Core route-building and matching.

These functions can be used both in a class inheriting from Base and in file scope.

Examples:

Using a class deriving from Base

class MyApp < Kawaii::Base
  get '/' do
    'Hello, world'
  end
end

Using top-level (source file scope) route definitions

get '/' do
  'Hello, world'
end

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) add_http_method(meth)

Note:

Supported HTTP verbs based on github.com/rack/rack/blob/master/lib/rack.rb#L48



27
28
29
30
31
32
# File 'lib/kawaii/routing_methods.rb', line 27

def self.add_http_method(meth)
  define_method(meth) do |path, mapping = nil, &block|
    handler = RouteMapping.new(mapping, &block).resolve
    add_route!(meth.to_s.upcase, Route.new(self, path, &handler))
  end
end

Instance Method Details

- (Object) context(path, &block)

Create a context for route nesting.

@param path [String, Regexp, Matcher] any path specification which can
       be consumed by {Matcher.compile}
@param block the route handler
@yield to the given block

Examples:

A simple context

context '/foo' do
  get '/bar' do
  end
end


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kawaii/routing_methods.rb', line 80

def context(path, &block)
  ctx = RouteContext.new(self, path)
  # @todo Is there a better way to keep ordering of routes?
  # An alternative would be to enter each route in a context only once
  # (with 'prefix' based on containing contexts).
  # On the other hand, we're only doing that when compiling routes, further
  # processing is faster this way.
  ctx.instance_eval(&block)
  ctx.methods_used.each do |meth|
    add_route!(meth, ctx)
  end
end

- (Object) delete(path, &block) { ... }

Route handler for delete HTTP method

Parameters:

Yields:

  • to the given block



38
# File 'lib/kawaii/routing_methods.rb', line 38

add_http_method :delete

- (Object) get(path, &block) { ... }

Route handler for get HTTP method

Parameters:

Yields:

  • to the given block



34
# File 'lib/kawaii/routing_methods.rb', line 34

add_http_method :get

- (Object) head(path, &block) { ... }

Route handler for head HTTP method

Parameters:

Yields:

  • to the given block



39
# File 'lib/kawaii/routing_methods.rb', line 39

add_http_method :head

Route handler for link HTTP method

Parameters:

Yields:

  • to the given block



41
# File 'lib/kawaii/routing_methods.rb', line 41

add_http_method :link

- (Route) match(env)

Tries to match against a Rack environment.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Route)

    matching route. Can be nil if no match found.



96
97
98
99
100
101
# File 'lib/kawaii/routing_methods.rb', line 96

def match(env)
  routes[env[Rack::REQUEST_METHOD]]
    .lazy # Lazy to avoid unnecessary calls to #match.
    .map { |r| r.match(env) }
    .find { |r| !r.nil? }
end

- (Array<String>) methods_used

Returns a list of HTTP methods used by the routes (incl. nested routes).

Returns:

  • (Array<String>)

    example [“GET”, “POST”]



105
106
107
# File 'lib/kawaii/routing_methods.rb', line 105

def methods_used
  routes.keys
end

- (Object) options(path, &block) { ... }

Route handler for options HTTP method

Parameters:

Yields:

  • to the given block



40
# File 'lib/kawaii/routing_methods.rb', line 40

add_http_method :options

- (Object) patch(path, &block) { ... }

Route handler for patch HTTP method

Parameters:

Yields:

  • to the given block



37
# File 'lib/kawaii/routing_methods.rb', line 37

add_http_method :patch

- (Object) post(path, &block) { ... }

Route handler for post HTTP method

Parameters:

Yields:

  • to the given block



35
# File 'lib/kawaii/routing_methods.rb', line 35

add_http_method :post

- (Object) put(path, &block) { ... }

Route handler for put HTTP method

Parameters:

Yields:

  • to the given block



36
# File 'lib/kawaii/routing_methods.rb', line 36

add_http_method :put

- (Object) route(path, controller)

Insert routes corresponding to REST actions (similar to Rails `resource`).

Examples:

REST resource routes

route '/users/', 'hello_world'

# Will insert routes corresponding to:
# GET /users/? -> Controller#index
# GET /users/:id/? -> Controller#show
# POST /users/? -> Controller#create
# PATCH /users/:id/? -> Controller#update
# DELETE /users/:id/? -> Controller#destroy

Parameters:

  • path (String)

    path prefix (e.g. “/users/”)

  • controller (String)

    snakecase controller name (e.g. “hello_world” corresponds to HelloWorld).



60
61
62
63
64
65
66
# File 'lib/kawaii/routing_methods.rb', line 60

def route(path, controller)
  get(File.join(path, '?'), "#{controller}#index")
  get(File.join(path, '/:id/?'), "#{controller}#show")
  post(File.join(path, '?'), "#{controller}#create")
  patch(File.join(path, '/:id/?'), "#{controller}#update")
  delete(File.join(path, '/:id/?'), "#{controller}#destroy")
end

- (Object) trace(path, &block) { ... }

Route handler for trace HTTP method

Parameters:

Yields:

  • to the given block



43
# File 'lib/kawaii/routing_methods.rb', line 43

add_http_method :trace

Route handler for unlink HTTP method

Parameters:

Yields:

  • to the given block



42
# File 'lib/kawaii/routing_methods.rb', line 42

add_http_method :unlink