Class: Etna::Route
- Inherits:
-
Object
- Object
- Etna::Route
- Defined in:
- lib/etna/route.rb
Constant Summary collapse
- NAMED_PARAM =
/:([\w]+)/
- GLOB_PARAM =
/\*([\w]+)$/
- PARAM_TYPES =
[ NAMED_PARAM, GLOB_PARAM ]
- UNSAFE =
/[^\-_.!~*'()a-zA-Z\d;\/?:@&=+$,]/
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #call(app, request) ⇒ Object
-
#initialize(method, route, options, &block) ⇒ Route
constructor
A new instance of Route.
- #matches?(request) ⇒ Boolean
-
#noauth? ⇒ Boolean
the route does not require authorization.
- #parts ⇒ Object
- #path(params = nil) ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(method, route, options, &block) ⇒ Route
Returns a new instance of Route.
5 6 7 8 9 10 11 12 |
# File 'lib/etna/route.rb', line 5 def initialize(method, route, , &block) @method = method @action = [:action] @auth = [:auth] @name = route_name() @route = route.gsub(/\A(?=[^\/])/, '/') @block = block end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/etna/route.rb', line 3 def name @name end |
Class Method Details
.path(route, params = nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/etna/route.rb', line 34 def self.path(route, params=nil) if params PARAM_TYPES.reduce(route) do |path,pat| path.gsub(pat) do URI.encode( params[$1.to_sym], UNSAFE) end end else route end end |
Instance Method Details
#call(app, request) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/etna/route.rb', line 59 def call(app, request) update_params(request) unless (request) return [ 403, { 'Content-Type' => 'application/json' }, [ { error: 'You are forbidden from performing this action.' }.to_json ] ] end if @action controller, action = @action.split('#') controller_class = Kernel.const_get( :"#{controller.camel_case}Controller" ) logger = request.env['etna.logger'] user = request.env['etna.user'] params = request.env['rack.request.params'].map do |key,value| value = value.to_s value = value[0..500] + "..." + value[-100..-1] if value.length > 600 [ key, value ] end.to_h logger.warn("User #{user ? user.email : :unknown} calling #{controller}##{action} with params #{params}") return controller_class.new(request, action).response elsif @block application = Etna::Application.find(app.class).class controller_class = application.const_defined?(:Controller) ? application.const_get(:Controller) : Etna::Controller controller_class.new(request).response(&@block) end end |
#matches?(request) ⇒ Boolean
23 24 25 |
# File 'lib/etna/route.rb', line 23 def matches?(request) @method == request.request_method && request.path.match(route_regexp) end |
#noauth? ⇒ Boolean
the route does not require authorization
91 92 93 |
# File 'lib/etna/route.rb', line 91 def noauth? @auth && @auth[:noauth] end |
#parts ⇒ Object
50 51 52 53 54 55 56 57 |
# File 'lib/etna/route.rb', line 50 def parts part_list = PARAM_TYPES.map do |pat| "(?:#{pat.source})" end @route.scan( /(?:#{part_list.join('|')})/ ).flatten.compact end |
#path(params = nil) ⇒ Object
46 47 48 |
# File 'lib/etna/route.rb', line 46 def path(params=nil) self.class.path(@route, params) end |
#to_hash ⇒ Object
14 15 16 17 18 19 20 21 |
# File 'lib/etna/route.rb', line 14 def to_hash { method: @method, route: @route, name: @name.to_s, params: parts }.compact end |