Class: Cannon::Route
- Inherits:
-
Object
- Object
- Cannon::Route
- Defined in:
- lib/cannon/route.rb
Instance Attribute Summary collapse
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#redirect ⇒ Object
readonly
Returns the value of attribute redirect.
Instance Method Summary collapse
- #add_route_action(action) ⇒ Object
- #handle(request, response, finish_proc) ⇒ Object
-
#initialize(app, method:, path:, actions: nil, redirect: nil) ⇒ Route
constructor
A new instance of Route.
- #matches?(request) ⇒ Boolean
- #to_s ⇒ Object
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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
3 4 5 |
# File 'lib/cannon/route.rb', line 3 def actions @actions end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
3 4 5 |
# File 'lib/cannon/route.rb', line 3 def method @method end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/cannon/route.rb', line 3 def path @path end |
#redirect ⇒ Object (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. Cannon.logger.error error.backtrace.join("\n") response.internal_server_error(title: error., content: error.backtrace.join('<br/>')) finish_proc.call end end end |
#matches?(request) ⇒ 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_s ⇒ Object
43 44 45 |
# File 'lib/cannon/route.rb', line 43 def to_s "Route: #{path}" end |