Changelog History
-
v3.2.0
February 28, 2020 -
v3.1.1
June 13, 2018 -
v3.1.0 Changes
February 01, 2018๐ This release adds status code handlers, a feature that will simplify many Syro applications.
0๏ธโฃ When a request doesn't match, Syro will return a
404
status code with an empty body. It's a fine default because it doesn't make any assumptions, but in a production environment you want to return a helpful error message to the users. Up until now, the way to refine the404
responses was to add a default block to each branch, for example:App = Syro.new do get do# ...end on "foo" do get do# ...end default do res.text "Not found!"endend default do res.text "Not found!"endend
In that application, only two requests will return a status code
200
:GET /
andGET /foo
. Any other request will return a404
error with the body"Not found!"
.0๏ธโฃ We had to define a default block for each branch, and if we want to deal with
404
status codes in a different way we can install a handler. The new version would look like this:App = Syro.new do handle 404 do res.text "Not found!"end get do# ...end on "foo" do get do# ...endendend
The external behavior will be the same, but the solution is perhaps more elegant. At any point, and in any branch, the handler can be replaced. For example, let's say you want a different error message when a user is not found:
App = Syro.new do handle 404 do res.text "Not found!"end# ... on "users" do on :id do handle 404 do res.text "User not found"end# ...endendend
You can think about handlers as filters that will get executed just before finishing the request. Within those handlers you can do anything you would do in a regular block, for instance:
App = Syro.new do handle 404 do res.redirect "/wiki"end on "wiki" do# ...endend
๐ In this last example, the
res.redirect
statement will change the status code to302
. You can also add handlers for other status codes besides404
. You could even add handlers for status200
, but in that case, as it involves an extra method call, you will get a slight performance penalty.๐ The documentation covers other use cases and some edge conditions that you can abuse. Let me know if you run into any issues or if you detect something that's not clear enough.
๐ Note: as the implementation uses the safe navigation operator
&.
, Ruby > 2.3 is required to run this version. -
v3.0.1
February 27, 2017 -
v3.0.0
February 24, 2017 -
v2.2.0 Changes
July 01, 2016๐ This release has no new features, but it relaxes the gemspec so that Syro can be used with Rack 2.
-
v2.1.2
May 20, 2016 -
v2.1.1
May 04, 2016 -
v2.1.0
December 12, 2015 -
v2.0.0 Changes
December 05, 20150๏ธโฃ This version adds the
default
matcher and two new primitives:consume
(match and consume a path segment) andcapture
(match and capture a path segment, then store the value in the inbox). The reason for providingconsume
andcapture
has to do with the fact that a framework using Syro can create new matchers, for example it could provide a matcher that behaves like path captures in Cuba.Normally, you can capture a path segment and access the value using the inbox:
on(:post\_id) do res.write inbox[:post\_id]end
A custom
Desk
could implement thewith
matcher:def with(arg) default { yield(inbox[arg]) } if capture(arg)end
Then, it could be used as follows:
with(:post\_id) do |post\_id| ...end
๐ The functionality to yield the capture to the block was included in version
1.0.0
, and it was removed in this release because the implementation performed hash lookups in all matches.Deck
creators can also redefine the on matcher as follows:def on(arg) default { yield(inbox[arg]) } if match(arg)end
This version encourages the use of the inbox instead, and encourages framework authors to create a
with
matcher if they want that behaviour. The code is production ready.