Class: Knot::Dispatch::Router

Inherits:
Object
  • Object
show all
Extended by:
API
Includes:
Context
Defined in:
lib/knot/dispatch/router.rb,
lib/knot/dispatch/router/api.rb,
lib/knot/dispatch/router/config.rb,
lib/knot/dispatch/router/context.rb

Defined Under Namespace

Modules: API, Context Classes: Config

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API

delete, filter, get, head, options, patch, post, put

Methods included from Context

#json, #params, #redirect, #render, #respond!, #status

Constructor Details

#initialize(request, route) ⇒ Router

Returns a new instance of Router.



29
30
31
32
33
# File 'lib/knot/dispatch/router.rb', line 29

def initialize(request, route)
  @request = request
  @response = Knot::Dispatch::Response.new
  @route = route
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



27
28
29
# File 'lib/knot/dispatch/router.rb', line 27

def request
  @request
end

#responseObject (readonly)

Returns the value of attribute response.



27
28
29
# File 'lib/knot/dispatch/router.rb', line 27

def response
  @response
end

#routeObject (readonly)

Returns the value of attribute route.



27
28
29
# File 'lib/knot/dispatch/router.rb', line 27

def route
  @route
end

Class Method Details

._call!(request) ⇒ Object



12
13
14
# File 'lib/knot/dispatch/router.rb', line 12

def self._call!(request)
  new(request, matching_route(request))._call!
end

.configObject



16
17
18
# File 'lib/knot/dispatch/router.rb', line 16

def self.config
  @config ||= Knot::Dispatch::Router::Config.new(self)
end

.matching_route(request) ⇒ Object



20
21
22
23
24
25
# File 'lib/knot/dispatch/router.rb', line 20

def self.matching_route(request)
  route = config.routes.find { |route| route.match?(request) }
  raise Knot::Dispatch::NoRouteError, "no route matches #{request.path}" if route.nil?

  return route
end

Instance Method Details

#_call!(error = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/knot/dispatch/router.rb', line 35

def _call!(error = nil)
  request.params.merge! Path.path_params(route.path, request)

  blocks = _config.applicable_filters(route).map(&:block)
  blocks << route.block

  catch(:halt) {
    blocks.map do |block|
      result = instance_exec error, &block
      response.body << result if result.is_a?(String)
    end.last
  }

  return response
end