Syro directly supports matchers for the request methods
get, put, head,
post, patch, delete
and options.
Syro supporte directement des matchers pour les méthodes de requête
get, put, head,
post, patch, delete et
options.
The following example responds differently to each verb, even if the path remains the same:
L'exemple suivant répond de manière différente pour chaque verbe, même si le chemin reste invariable :
require "syro" App = Syro.new do on "foo" do get do res.text "GET /foo" end put do res.text "PUT /foo" end head do res.text "HEAD /foo" end post do res.text "POST /foo" end patch do res.text "PATCH /foo" end delete do res.text "DELETE /foo" end options do res.text "OPTIONS /foo" end end end env = { "REQUEST_METHOD" => "DELETE", "PATH_INFO" => "/foo", } p App.call(env)
Output:
Résultat:
[200,
{"Content-Length"=>"11",
"Content-Type"=>"text/plain"},
["DELETE /foo"]]
As explained in Chapter 2, we are not only
matching the REQUEST_METHOD, but we are also asserting
that there are no segments left to be matched.
Comme expliqué dans le Chapitre 2, on ne
fait pas que matcher le REQUEST_METHOD, mais on
affirme aussi qu'il ne reste aucun segment à matcher.
There are more HTTP methods than what we covered in Syro. If you
need some other method, you can always use on to
match a boolean (more information in Chapter
9):
Il existe davantage de méthodes HTTP que celles traité par Syro.
Si vous en avez besoin, vous pouvez toujours utiliser on
pour matcher un booléen (plus d'infos dans le Chapitre
9) :
require "syro" App = Syro.new do on "foo" do on req.trace? do root do res.text "TRACE /foo" end end end end env = { "REQUEST_METHOD" => "TRACE", "PATH_INFO" => "/foo", } p App.call(env)
Output:
Résultat:
[200,
{"Content-Length"=>"10",
"Content-Type"=>"text/plain"},
["TRACE /foo"]]
Note that we had to call root to make sure there are
no extra segments in the path.
Notez qu'on a dû appeler root pour être sûrs de ne
pas avoir de segments supplémentaires dans le chemin.
Remove the call to root but leave the call to
res.text. Do you get the same output? Now modify
PATH_INFO. What happens?
Retirez l'appel à root mais laissez l'appel à
res.text. Avez-vous obtenu le même résultat ?
Maintenant modifiez PATH_INFO. Que s'est-il passé ?