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
- #hash_user_email(email) ⇒ Object
- #ignore_janus? ⇒ Boolean
-
#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
- #process_call(app, request) ⇒ Object
- #to_hash ⇒ Object
- #try_yabeda(request, &block) ⇒ Object
Constructor Details
#initialize(method, route, options, &block) ⇒ Route
Returns a new instance of Route.
8 9 10 11 12 13 14 15 16 |
# File 'lib/etna/route.rb', line 8 def initialize(method, route, , &block) @method = method @action = [:action] @auth = [:auth] @name = route_name() @route = route.gsub(/\A(?=[^\/])/, '/') @block = block @match_ext = [:match_ext] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/etna/route.rb', line 6 def name @name end |
Class Method Details
.path(route, params = nil) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/etna/route.rb', line 38 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
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/etna/route.rb', line 63 def call(app, request) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) try_yabeda(request) do || Yabeda.etna.visits.increment() end begin process_call(app, request) ensure try_yabeda(request) do || dur = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start Yabeda.etna.response_time.measure(, dur) end end end |
#hash_user_email(email) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/etna/route.rb', line 80 def hash_user_email(email) secret = Etna::Application.instance.config(:user_hash_secret) || 'notsosecret' digest = email + secret + Date.today.to_s if @name digest += @name.to_s else digest += @route.to_s end Digest::MD5.hexdigest(digest) end |
#ignore_janus? ⇒ Boolean
154 155 156 |
# File 'lib/etna/route.rb', line 154 def ignore_janus? @auth && @auth[:ignore_janus] end |
#matches?(request) ⇒ Boolean
27 28 29 |
# File 'lib/etna/route.rb', line 27 def matches?(request) @method == request.request_method && request.path.match(route_regexp) end |
#noauth? ⇒ Boolean
the route does not require authorization
150 151 152 |
# File 'lib/etna/route.rb', line 150 def noauth? @auth && @auth[:noauth] end |
#parts ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/etna/route.rb', line 54 def parts part_list = PARAM_TYPES.map do |pat| "(?:#{pat.source})" end @route.scan( /(?:#{part_list.join('|')})/ ).flatten.compact end |
#path(params = nil) ⇒ Object
50 51 52 |
# File 'lib/etna/route.rb', line 50 def path(params=nil) self.class.path(@route, params) end |
#process_call(app, request) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/etna/route.rb', line 120 def process_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| [ key, redact(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 |
#to_hash ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/etna/route.rb', line 18 def to_hash { method: @method, route: @route, name: @name.to_s, params: parts }.compact end |
#try_yabeda(request, &block) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/etna/route.rb', line 93 def try_yabeda(request, &block) if @action controller, action = @action.split('#') elsif @name controller = "none" action = @name else controller = "none" action = @route end params = request.env['rack.request.params'] user = request.env['etna.user'] user_hash = user ? hash_user_email(user.email) : 'unknown' project_name = "unknown" if params && (params.include?(:project_name) || params.include?('project_name')) project_name = params[:project_name] || params['project_name'] end begin block.call({ controller: controller, action: action, user_hash: user_hash, project_name: project_name }) rescue => e raise e unless Etna::Application.instance.environment == :production end end |