I have a Ruby Sinatra app and I have some code which I need to execute on all routes except for a few exceptions. How do I do this?
If I wanted to execute the code on selected routes (whitelist style) I'd do this:
['/join', "/join/*", "/payment/*"].each do |path| before path do #some code end end
How do I do it the other way round though (blacklist style)? I want to match all routes except '/join'
, '/join/*'
and '/payment/*'
Sinatra is powerful enough to develop a functioning web application with just a single file. Sinatra is recognized to be a good way for novice developers to get started in web application development in Ruby and can help prepare in learning for larger frameworks, including Rails.
Sinatra::Base is the Sinatra without delegation. Consider the following code with delegation: # app.rb require 'sinatra' get '/' do render :template end.
With negative look-ahead:
before /^(?!\/(join|payment))/ do # ... end
With pass
:
before do pass if %w[join payment].include? request.path_info.split('/')[1] # ... end
Or you could create a custom matcher.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With