Every Syro application we have developed so far is evaluated in
the context of an instance of Syro::Deck
. That class
provides all the methods we have seen so far (i.e., on
,
root
, run
, inbox
,
get
, put
, head
,
post
, patch
, delete
,
options
, res
and req
).
Chaque application Syro qu'on a développé jusqu'ici est évaluée
dans le contexte d'une instance de Syro::Deck
. Cette
classe fournit toutes les méthodes que nous avons vues jusqu'ici
(ex. on
, root
, run
,
inbox
, get
, put
,
head
, post
, patch
,
delete
, options
, res
et
req
).
You can create your own Decks and add some methods of your own:
Vous pouvez créer vos propres Decks et ajouter vos propres méthodes :
require "syro" require "json" class API < Syro::Deck def json(object) res.json JSON.dump(object) end end App = Syro.new(API) do get do json(result: true) end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[200, {"Content-Type"=>"application/json", "Content-Length"=>"15"}, ["{\"result\":true}"]]
If you need a JSON API, that's more or less the way to go.
Si vous avez besoin d'une API JSON, c'est plus ou moins le chemin à suivre.
You can configure the default headers for your application by
redefining the default_headers
method. Here's a new
version of the API
Deck.
Vous pouvez configurer les entêtes par défaut pour vos applications
en redéfinissant la méthode default_headers
. Voici
une nouvelle version du Deck API
.
require "syro" require "json" class API < Syro::Deck def default_headers { "X-Content-Type-Options" => "nosniff", "X-Download-Options" => "noopen", "X-Frame-Options" => "SAMEORIGIN", "X-Permitted-Cross-Domain-Policies" => "none", "X-XSS-Protection" => "1; mode=block", "Strict-Transport-Security" => "max-age=2628000" } end def json(object) res.json JSON.dump(object) end end App = Syro.new(API) do get do json(result: true) end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[200, {"Content-Type"=>"application/json", "Content-Length"=>"15", "X-Content-Type-Options" => "nosniff", "X-Download-Options" => "noopen", "X-Frame-Options" => "SAMEORIGIN", "X-Permitted-Cross-Domain-Policies" => "none", "X-XSS-Protection" => "1; mode=block", "Strict-Transport-Security" => "max-age=2628000"}, ["{\"result\":true}"]]
Now the security related headers are included in all responses.
Maintenant, les entêtes liés à la sécurité sont inclus dans toutes les réponses.
The res
and req
objects are instances
of Syro::Response
and Rack::Request
respectively. If you want to provide your own classes, you can
redefine the response_class
and request_class
methods:
Les objets res
et req
sont des instances
de Syro::Response
et Rack::Request
respectivement. Si vous voulez fournir vos propres classes, vous
pouvez redefinir les méthodes response_class
et
request_class
:
require "syro" class MyResponse < Syro::Response end class MyRequest < Rack::Request end class MyDeck < Syro::Deck def response_class MyResponse end def request_class MyRequest end end App = Syro.new(MyDeck) do get do res.text req.class end end env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/", } p App.call(env)
Output:
Résultat :
[200, {"Content-Type"=>"text/plain", "Content-Length"=>"9"}, ["MyRequest"]]
You can reuse the same Deck in different applications and combine functionality by mixing in several modules. There are no limits really.
Vous pouvez réutiliser le même Deck dans differentes applications et combiner des fonctionnalités en mélangeant differents modules. Il n'y a vraiment pas de limites.