Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are a few ways you could do this (these aren't in any order, they're all good):</p> <h3>Namespaces with a before block and a helper</h3> <p><a href="http://rubydoc.info/gems/sinatra-contrib/1.3.2/Sinatra/Namespace">http://rubydoc.info/gems/sinatra-contrib/1.3.2/Sinatra/Namespace</a></p> <pre><code>require 'sinatra/namespace' class App &lt; Sinatra::Base register Sinatra::Namespace namespace "/base" do helpers do # this helper is now namespaced too authenticate! # funky auth stuff # but no need for `pass` end end before do authenticate! end get "/anything" do end get "/you" do end get "/like/here" do end end </code></pre> <h3>Namespace with a condition</h3> <pre><code>require 'sinatra/namespace' class App &lt; Sinatra::Base register Sinatra::Namespace set(:auth) do |*roles| # &lt;- notice the splat here condition do unless logged_in? &amp;&amp; roles.any? {|role| current_user.in_role? role } redirect "/login/", 303 end end end namespace "/base", :auth =&gt; [:user] do # routes… end namespace "/admin", :auth =&gt; [:admin] do # routes… end </code></pre> <h3>Helpers and a before filter</h3> <pre><code>helpers do authenticate! # funky auth stuff # but no need for `pass` end end before '/base/*' do authenticate! end </code></pre> <h3>mapped Apps</h3> <pre><code>class MyBase &lt; Sinatra::Base helpers do authenticate! # funky auth stuff # but no need for `pass` end end before do authenticate! end get "/" do end get "/another" do end end # in rackup file map "/" do run App1 end map "/base" do # every route in MyBase will now be accessed by prepending "/base" # e.g. "/base/" and "/base/another" run MyBase end #… </code></pre> <p>I'm not sure about the need to DRY up routes using a case statement. If the routes each do something different then I'd just write them out separately as it's a lot clearer and you're duplicating the work Sinatra does in matching routes.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload