Skip to content

Instantly share code, notes, and snippets.

@clintpachl
Last active January 28, 2018 02:20
Show Gist options
  • Save clintpachl/51aa748759ebb82edbfac9d614a11e1f to your computer and use it in GitHub Desktop.
Save clintpachl/51aa748759ebb82edbfac9d614a11e1f to your computer and use it in GitHub Desktop.
All apps should only match page[123], else return 404. Shows new Syro route match method `at`.
App = Syro.new do
on :token do
at 'page1' do
get { res.text 'page1' }
end
at 'page2' do
get { res.text 'page2' }
end
at 'page3' do
get { res.text 'page3' }
end
res.text '404' # :token branch 404
end
res.text '404' # app 404
end
env = { # page1
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/page1",
}
p App.call(env)
env = { # 404
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/pageX",
}
p App.call(env)
env = { # 404
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/page1/nonexistent",
}
p App.call(env)
env = { # 404
"REQUEST_METHOD" => "GET",
"PATH_INFO" => "/",
}
p App.call(env)
App = Syro.new do
on :token do
root do
on inbox[:token] == 'page1' do
get { res.text 'page1' }
end
on inbox[:token] == 'page2' do
get { res.text 'page2' }
end
on inbox[:token] == 'page3' do
get { res.text 'page3' }
end
res.text '404' # :token branch root-only 404
end
res.text '404' # :token branch 404
end
res.text '404' # app 404
end
App = Syro.new do
on 'page1' do
get { res.text 'page1' }
res.text '404' # page1 404
end
on 'page2' do
get { res.text 'page2' }
res.text '404' # page2 404
end
on 'page3' do
get { res.text 'page3' }
res.text '404' # page3 404
end
res.text '404' # app 404
end
[200, {"Content-Type"=>"text/plain", "Content-Length"=>"5"}, ["page1"]]
[404, {"Content-Type"=>"text/plain", "Content-Length"=>"3"}, ["404"]]
[404, {"Content-Type"=>"text/plain", "Content-Length"=>"3"}, ["404"]]
[404, {"Content-Type"=>"text/plain", "Content-Length"=>"3"}, ["404"]]
--- syro-3.0.1/lib/syro.rb Thu Jan 25 15:24:37 2018
+++ /tmp/syro.rb Thu Jan 25 15:24:20 2018
@@ -302,6 +302,7 @@
end
def capture(arg)
+ @syro_last_capture = arg
@syro_path.capture(arg, inbox)
end
@@ -328,6 +329,10 @@
def root
default { yield } if root?
+ end
+
+ def at(arg)
+ root { yield } if arg == inbox[@syro_last_capture]
end
def get
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment