Class: Cannon::Route

Inherits:
Object
  • Object
show all
Defined in:
lib/cannon/route.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, method:, path:, actions: nil, redirect: nil) ⇒ Route

Returns a new instance of Route.



5
6
7
8
9
10
# File 'lib/cannon/route.rb', line 5

def initialize(app, method:, path:, actions: nil, redirect: nil)
  @path = build_path(path)
  @method, @app, @redirect = method.to_s.upcase, app, redirect
  @actions = actions || []
  @route_action = build_route_action(@actions.dup)
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



3
4
5
# File 'lib/cannon/route.rb', line 3

def actions
  @actions
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/cannon/route.rb', line 3

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/cannon/route.rb', line 3

def path
  @path
end

#redirectObject (readonly)

Returns the value of attribute redirect.



3
4
5
# File 'lib/cannon/route.rb', line 3

def redirect
  @redirect
end

Instance Method Details

#add_route_action(action) ⇒ Object



12
13
14
# File 'lib/cannon/route.rb', line 12

def add_route_action(action)
  @route_action.last_action.callback = RouteAction.new(@app, action: action, callback: nil)
end

#handle(request, response, finish_proc) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cannon/route.rb', line 28

def handle(request, response, finish_proc)
  if redirect
    response.permanent_redirect(redirect)
  elsif @route_action
    begin
      @route_action.run(request, response, finish_proc)
    rescue => error
      Cannon.logger.error error.message
      Cannon.logger.error error.backtrace.join("\n")
      response.internal_server_error(title: error.message, content: error.backtrace.join('<br/>'))
      finish_proc.call
    end
  end
end

#matches?(request) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/cannon/route.rb', line 16

def matches?(request)
  return false unless method == 'ALL' || request.method == method

  matches = self.path.match(request.path)
  if matches.nil?
    false
  else
    @params.each_with_index { |key, index| request.params[key.to_sym] = matches.captures[index] }
    true
  end
end

#to_sObject



43
44
45
# File 'lib/cannon/route.rb', line 43

def to_s
  "Route: #{path}"
end