### GET /about Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests to the /about path. ```APIDOC ## GET /about ### Description Handles GET requests to the /about path. ### Method GET ### Endpoint /about ### Example ```ruby r.get "about" do "About" end ``` ``` -------------------------------- ### Hash Matcher Example (:foo) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Hash matchers allow calling specialized match methods. This example shows defining and using a custom hash matcher. ```ruby class App < Roda plugin :hash_matcher hash_matcher(:foo) do |v| # ... end route do |r| r.on foo: 'bar' do # ... end end end ``` -------------------------------- ### GET /search?q=:query Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests to /search with a query parameter 'q'. ```APIDOC ## GET /search ### Description Handles GET requests to the /search endpoint, accepting a query parameter 'q' to perform a search. ### Method GET ### Endpoint /search ### Parameters #### Query Parameters - **q** (String) - Required - The search query string. ### Example ```ruby r.get "search" do "Searched for #{r.params['q']}" end ``` ``` -------------------------------- ### Route with Multiple Methods (GET and POST) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles requests to '/login' for both GET and POST methods, demonstrating how to define different handlers for the same path based on the HTTP method. ```ruby r.is "login" do # GET /login r.get do "Login" end # POST /login?user=foo&password=baz r.post do "#{r.params['user']}:#{r.params['password']}" #=> "foo:baz" end end ``` -------------------------------- ### Install Roda Gem Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Install the Roda gem using the RubyGems package manager. ```bash $ gem install roda ``` -------------------------------- ### Simple Roda Plugin Example Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc A basic Roda plugin that adds an instance method for markdown conversion. This module should be included in the Roda class. ```ruby module MarkdownHelper module InstanceMethods def markdown(str) BlueCloth.new(str).to_html end end end Roda.plugin MarkdownHelper ``` -------------------------------- ### Matching GET Requests Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match any GET request. This is a shorthand for matching based on HTTP method. ```ruby r.get do end ``` -------------------------------- ### Basic Routing Tree Example Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Demonstrates a basic routing tree structure using r.on, r.is, r.get, and r.post to define nested routes and handle different HTTP methods. ```APIDOC ## Routing Tree Structure ### Description This example illustrates how to build a routing tree by branching on paths and then handling specific HTTP methods. ### Example ```ruby r.on "a" do # /a branch r.on "b" do # /a/b branch r.is "c" do # /a/b/c request r.get do end # GET /a/b/c request r.post do end # POST /a/b/c request end r.get "d" do end # GET /a/b/d request r.post "e" do end # POST /a/b/e request end end ``` ``` -------------------------------- ### Hash Matcher Example (:all) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc The `:all` hash matcher matches if all entries in a given array match. It can be used within other array matchers. ```ruby r.on all: [String, String] do # ... end ``` ```ruby r.on ['foo', {all: ['foos', Integer]}] do end ``` -------------------------------- ### GET /login and POST /login Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles both GET and POST requests to the /login endpoint for login functionality. ```APIDOC ## /login Endpoint ### Description Handles both GET requests to display the login form and POST requests with user credentials to authenticate. ### GET /login #### Method GET #### Endpoint /login #### Example ```ruby r.is "login" do r.get do "Login" end # ... POST handler below ... end ``` ### POST /login #### Method POST #### Endpoint /login #### Parameters #### Query Parameters - **user** (String) - Required - The username for login. - **password** (String) - Required - The password for login. #### Example ```ruby r.is "login" do # ... GET handler above ... r.post do "#{r.params['user']}:#{r.params['password']}" end end ``` ``` -------------------------------- ### Handling Optional Segments Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc This example demonstrates handling optional segments by defining separate routes with a shared branch, allowing for multiple URL patterns. ```ruby r.on "items", Integer do |item_id| # ... end ``` -------------------------------- ### Routing Tree by Request Method First Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Illustrates structuring the routing tree by first branching on the request method (GET or POST) before handling path segments. ```ruby r.get do # GET r.on "a" do # GET /a branch r.on "b" do # GET /a/b branch r.is "c" do end # GET /a/b/c request r.is "d" do end # GET /a/b/d request end end end r.post do # POST r.on "a" do # POST /a branch r.on "b" do # POST /a/b branch r.is "c" do end # POST /a/b/c request r.is "e" do end # POST /a/b/e request end end end ``` -------------------------------- ### Matching Specific GET Request to Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match only GET requests where the current path is '/a/b'. ```ruby r.get "a/b" do end ``` -------------------------------- ### Exact GET Request Match on Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match GET requests only if the current path is exactly '/foo'. Does not match '/foo/.*'. ```ruby r.on "foo" do r.get true do # Matches GET /foo, not GET /foo/.* end end ``` -------------------------------- ### Class Matcher Examples (String and Integer) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Class matchers like String and Integer are recommended for handling arbitrary segments. String matches non-empty segments, yielding the segment text. Integer matches numeric segments, returning them as integers. ```ruby String # matches "/foo", yields "foo" String # matches "/1", yields "1" String # does not match "/" ``` ```ruby Integer # does not match "/foo" Integer # matches "/1", yields 1 Integer # does not match "/" ``` -------------------------------- ### Matching GET Request with Method Hash Matcher Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match GET requests for '/foo' and any subpaths using the :method hash matcher. ```ruby r.on "foo", method: :get do # Matches GET /foo(/.*)? end ``` -------------------------------- ### Hash Matcher Example (:method) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc The `:method` hash matcher matches the request method. It can accept a single method or an array of methods to match any of them. ```ruby {method: :post} # matches POST {method: ['post', 'patch']} # matches POST and PATCH ``` -------------------------------- ### Routing by Method First Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Shows an alternative routing structure where the tree is first branched by HTTP method (GET, POST) before branching by path. ```APIDOC ## Routing by Method First ### Description This structure first branches on the request method and then on the path, which can be useful for separating GET and POST request handling. ### Example ```ruby r.get do # GET r.on "a" do # GET /a branch r.on "b" do # GET /a/b branch r.is "c" do end # GET /a/b/c request r.is "d" do end # GET /a/b/d request end end end r.post do # POST r.on "a" do # POST /a branch r.on "b" do # POST /a/b branch r.is "c" do end # POST /a/b/c request r.is "e" do end # POST /a/b/e request end end end ``` ``` -------------------------------- ### GET Route with Query Parameter Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests to '/search' and accesses query parameters using r.params. ```ruby # /search?q=barbaz r.get "search" do "Searched for #{r.params['q']}" #=> "Searched for barbaz" end ``` -------------------------------- ### Route with Optional Data Parameter Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Example of using a parameter for optional data, accessed via r.params. ```ruby r.is "items", Integer do |item_id| optional_data = r.params['opt'].to_s end ``` -------------------------------- ### GET Route with String Matcher Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests to a specific path segment like '/about'. ```ruby # GET /about r.get "about" do "About" end ``` -------------------------------- ### GET /username/:username Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests for a specific username, with nested routes for posts and following. ```APIDOC ## GET /username/:username ### Description Handles requests for a specific username, with nested routes for accessing user-specific information like posts and following. ### Method GET ### Endpoint /username/:username ### Parameters #### Path Parameters - **username** (String) - The username to retrieve information for. ### Nested Routes #### GET /username/:username/posts ##### Description Retrieves the total number of posts for the specified user. ##### Endpoint /username/:username/posts #### GET /username/:username/following ##### Description Retrieves the number of users the specified user is following. ##### Endpoint /username/:username/following ### Example ```ruby r.on "username", String, method: :get do |username| user = User.find_by_username(username) r.is "posts" do "Total Posts: #{user.posts.size}" end r.is "following" do user.following.size.to_s end end ``` ``` -------------------------------- ### Root Method for GET Requests to Root Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match GET requests where the current path is '/'. This method does not consume the '/' from the path. ```ruby r.root do end ``` -------------------------------- ### Basic Roda Application Structure Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc A simple Roda application demonstrating the routing tree structure for handling different HTTP requests and paths. It includes root redirection, a '/hello' branch with GET and POST methods, and nested routes. ```ruby require "roda" class App < Roda route do |r| # GET / request r.root do r.redirect "/hello" end # /hello branch r.on "hello" do # Set variable for all routes in /hello branch @greeting = 'Hello' # GET /hello/world request r.get "world" do "#{@greeting} world!" end # /hello request r.is do # GET /hello request r.get do "#{@greeting}!" end # POST /hello request r.post do puts "Someone said #{@greeting}!" r.redirect end end end end end run App.freeze.app ``` -------------------------------- ### GET /post/:year/:month/:day/:slug Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests to a post URL with dynamic segments for year, month, day, and slug. ```APIDOC ## GET /post/:year/:month/:day/:slug ### Description Handles GET requests for blog post URLs, extracting year, month, day, and slug as parameters. ### Method GET ### Endpoint /post/:year/:month/:day/:slug ### Parameters #### Path Parameters - **year** (Integer) - Description of the year parameter - **month** (Integer) - Description of the month parameter - **day** (Integer) - Description of the day parameter - **slug** (String) - Description of the slug parameter ### Example ```ruby r.get "post", Integer, Integer, Integer, String do |year, month, day, slug| "#{year}-#{month}-#{day} #{slug}" end ``` ``` -------------------------------- ### Boolean Matcher Examples (true, false, nil) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc The `true` matcher always matches. `false` and `nil` matchers never match anything. ```ruby true # always matches ``` ```ruby false # does not match anything nil # does not match anything ``` -------------------------------- ### Handling Empty Path or Root Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match either the empty path or '/' for GET requests. Requires the slash_path_empty plugin for this behavior. ```ruby r.get ["", true] ``` -------------------------------- ### Registering a Roda Plugin from a Gem Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Example of how to register a Roda plugin, typically stored in a gem, allowing it to be loaded via Roda.plugin :plugin_name. It demonstrates using the Roda::RodaPlugins namespace and the register_plugin method. ```ruby class Roda module RodaPlugins module Markdown module InstanceMethods def markdown(str) BlueCloth.new(str).to_html end end end register_plugin :markdown, Markdown end end ``` -------------------------------- ### Set Matcher Examples Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Sets match if the next segment matches any of the string elements in the set. They offer better performance than Array matchers for a large number of elements. ```ruby Set['page1', 'page2'] # matches "/page1", "/page2" Set[] # does not match anything ``` -------------------------------- ### Proc Matcher Examples Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Proc matchers match unless they return false or nil. They can capture text by adding it to `r.captures`. ```ruby proc{true} # matches anything proc{false} # does not match anything ``` -------------------------------- ### GET Route with Multiple Type Matchers Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles GET requests with multiple path segments that include different data types like Integer and String, capturing them as block arguments. ```ruby # GET /post/2011/02/16/hello r.get "post", Integer, Integer, Integer, String do |year, month, day, slug| "#{year}-#{month}-#{day} #{slug}" #=> "2011-02-16 hello" end ``` -------------------------------- ### Route with String and Method Matcher Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handles requests to a path segment like '/username/:username' only for the GET method, capturing the username and allowing nested routes. ```ruby # GET /username/foobar branch r.on "username", String, method: :get do |username| user = User.find_by_username(username) # GET /username/foobar/posts r.is "posts" do # You can access user here, because the blocks are closures. "Total Posts: #{user.posts.size}" #=> "Total Posts: 6" end # GET /username/foobar/following r.is "following" do user.following.size.to_s #=> "1301" end end ``` -------------------------------- ### Regexp Matcher Examples Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Regexp matchers match one or more segments based on a pattern. They can capture and yield matched groups. ```ruby /foo\w+/ # matches "/foobar" /foo\w+/ # does not match "/foo/bar" /foo/i # matches "/foo", "/Foo/" /foo/i # does not match "/food" ``` ```ruby /foo\w+/ # matches "/foobar", yields nothing /foo(\w+)/ # matches "/foobar", yields "bar" ``` -------------------------------- ### Array Matcher Examples (OR Condition) Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Arrays can be used as matchers to specify multiple possible segments. If an array of matchers is given, only one needs to match (OR condition). If the matched object is a String, it is yielded. ```ruby ['page1', 'page2'] # matches "/page1", "/page2" [] # does not match anything ``` -------------------------------- ### Symbol Matcher Example Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Symbol matchers operate similarly to the String class matcher, matching any non-empty segment and yielding its text. They are a historical way to match arbitrary segments. ```ruby :id # matches "/foo" yields "foo" :id # does not match "/" ``` -------------------------------- ### String Matcher Examples Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc String matchers match segments of the URL path. If a string contains no slashes, it matches a single segment. If it contains slashes, it matches one additional segment per slash. ```ruby "" # matches "/" "foo" # matches "/foo" "foo" # does not match "/food" ``` ```ruby "foo/bar" # matches "/foo/bar" "foo/bar" # does not match "/foo/bard" ``` -------------------------------- ### Basic Rendering with Instance and Local Variables Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Demonstrates rendering a template with instance variables and local variables. The `render` method processes the specified template, making instance variables and locals available. The `view` method additionally renders the output within a layout template. ```ruby class App < Roda plugin :render route do |r| @var = '1' r.get "render" do # Renders the views/home.erb template, which will have access to # the instance variable @var, as well as local variable content. render("home", locals: {content: "hello, world"}) end r.get "view" do @var2 = '1' # Renders the views/home.erb template, which will have access to the # instance variables @var and @var2, and takes the output of that and # renders it inside views/layout.erb (which should yield where the # content should be inserted). view("home") end end end ``` -------------------------------- ### Basic Routing Tree Structure Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Demonstrates a basic routing tree structure using r.on, r.is, r.get, and r.post to define routes based on URL path and HTTP method. ```ruby r.on "a" do # /a branch r.on "b" do # /a/b branch r.is "c" do # /a/b/c request r.get do end # GET /a/b/c request r.post do end # POST /a/b/c request end r.get "d" do end # GET /a/b/d request r.post "e" do end # POST /a/b/e request end end ``` -------------------------------- ### Roda Sessions Plugin Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Illustrates how to enable Roda's built-in session support using the `sessions` plugin. Requires a secret key for secure session management. ```ruby require 'roda' class App < Roda plugin :sessions, secret: ENV['SESSION_SECRET'] end ``` -------------------------------- ### Configuring Render Plugin Options Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Shows how to configure the `render` plugin with custom options for escaping, views directory, layout, and template settings. This allows for fine-grained control over template rendering behavior. ```ruby class App < Roda plugin :render, escape: true, # Automatically escape output in erb templates using Erubi's escaping support views: 'admin_views', # Default views directory layout_opts: {template: 'admin_layout', engine: 'html.erb'}, # Default layout options template_opts: {default_encoding: 'UTF-8'} # Default template options end ``` -------------------------------- ### Accessing Request at Any Point Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Demonstrates how to perform actions, like checking permissions, at any point within the routing tree. ```APIDOC ## Accessing Request at Any Point ### Description This example shows how to execute logic, such as permission checks, at different levels of the routing tree, allowing for granular control. ### Example ```ruby r.on "a" do # /a branch check_perm(:A) r.on "b" do # /a/b branch check_perm(:B) r.is "c" do # /a/b/c request r.get do end # GET /a/b/c request r.post do end # POST /a/b/c request end r.get "d" do end # GET /a/b/d request r.post "e" do end # POST /a/b/e request end end ``` ``` -------------------------------- ### Routing Tree with Access Permissions Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Shows how to apply access permissions or other logic at different levels of the routing tree, demonstrating Roda's ability to operate on the request at any point during routing. ```ruby r.on "a" do # /a branch check_perm(:A) r.on "b" do # /a/b branch check_perm(:B) r.is "c" do # /a/b/c request r.get do end # GET /a/b/c request r.post do end # POST /a/b/c request end r.get "d" do end # GET /a/b/d request r.post "e" do end # POST /a/b/e request end end ``` -------------------------------- ### Mounting a Rack App with Roda Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Use `r.run` to mount any Rack application, including another Roda app, within a Roda app. This directs requests with a specific path prefix to the mounted app. ```ruby class API < Roda route do |r| r.is do # ... end end end class App < Roda route do |r| r.on "api" do r.run API end end end run App.app ``` -------------------------------- ### Small Application Roda File Layout Source: https://github.com/jeremyevans/roda/blob/master/doc/conventions.rdoc This is the conventional layout for a small Roda application's main file. It includes necessary requires, subclassing Roda, and common plugin usage. ```ruby require 'roda' require_relative 'models' class AppName < Roda SOME_CONSTANT = 1 use SomeMiddleware plugin :render, escape: true plugin :assets route do |r| # ... end def view_method 'foo' end end ``` -------------------------------- ### Define Large Roda Application Structure Source: https://github.com/jeremyevans/roda/blob/master/doc/conventions.rdoc Use this structure for large Roda applications. It includes defining constants, using middleware, loading plugins, requiring route files, and setting up the main route block. Instance methods for routes and views should be required after the route block. ```ruby require 'roda' require_relative 'models' class AppName < Roda SOME_CONSTANT = 1 use SomeMiddleware plugin :render, escape: true, layout: './layout' plugin :assets plugin :hash_branch_view_subdir Dir['routes/*.rb'].each{|f| require_relative f} route do |r| r.hash_branches('') r.root do # ... end end Dir['helpers/*.rb'].each{|f| require_relative f} end ``` -------------------------------- ### RodaSessionMiddleware for External Session Access Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Demonstrates using `RodaSessionMiddleware` when other middleware needs access to sessions. This middleware provides session support that can be accessed by both Roda applications and other Rack middleware. ```ruby require 'roda' require 'roda/session_middleware' class App < Roda use RodaSessionMiddleware, secret: ENV['SESSION_SECRET'] end ``` -------------------------------- ### Roda Application Settings Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Roda apps can store settings in the `opts` hash, which are inherited by subclasses. Settings are shallowly cloned upon subclassing. ```ruby Roda.opts[:layout] = "guest" class Users < Roda; end class Admin < Roda opts[:layout] = "admin" end Users.opts[:layout] # => 'guest' Admin.opts[:layout] # => 'admin' ``` -------------------------------- ### Matching POST Requests Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match any POST request. This is a shorthand for matching based on HTTP method. ```ruby r.post do end ``` -------------------------------- ### Configure Content Security Policy Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Configure a restrictive Content Security Policy using the `content_security_policy` plugin to control the sources from which content can be loaded, enhancing security against cross-site scripting (XSS) and other injection attacks. ```ruby class App < Roda plugin :content_security_policy do |csp| csp.default_src :none # deny everything by default csp.style_src :self csp.script_src :self csp.connect_src :self csp.img_src :self csp.font_src :self csp.form_action :self csp.base_uri :none csp.frame_ancestors :none csp.block_all_mixed_content csp.report_uri 'CSP_REPORT_URI' end end ``` -------------------------------- ### Root Route Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Defines the handler for the root path ('/'). ```APIDOC ## GET / ### Description Handles requests to the root path of the application. ### Method GET ### Endpoint / ### Example ```ruby r.root do "Home" end ``` ``` -------------------------------- ### Handling POST Request to Root Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Handle POST requests to the root path using r.post ''. ```ruby r.post '' ``` -------------------------------- ### Root Route Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Defines the handler for the root path ('/'). ```ruby # GET / r.root do "Home" end ``` -------------------------------- ### Matching Specific POST Request to Root Path Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Match only POST requests where the current path is '/'. ```ruby r.post "" do end ``` -------------------------------- ### Set Default Security HTTP Headers Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Configure common security-related HTTP headers using the `default_headers` plugin to enforce SSL/TLS, prevent content type sniffing, and protect against click-jacking attacks. ```ruby class App < Roda plugin :default_headers, 'Content-Type'=>'text/html', 'Strict-Transport-Security'=>'max-age=63072000; includeSubDomains', 'X-Content-Type-Options'=>'nosniff', 'X-Frame-Options'=>'deny' end ``` -------------------------------- ### Optional Segment Matching with Regexp Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc An alternative way to implement optional segments using a regular expression. ```ruby r.is "items", /(\d+)(?:\/(\d+))?/ do |item_id, optional_data| end ``` -------------------------------- ### Using the hash_branches Plugin in Roda Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc The `hash_branches` plugin allows splitting the main route block by branches while maintaining the current scope. Instance variables from the main `route` block are accessible within the branched `route` blocks. ```ruby class App < Roda plugin :hash_branches hash_branch "api" do |r| r.is do # ... end end route do |r| r.hash_branches end end run App.app ``` -------------------------------- ### CSRF Protection with route_csrf Plugin Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Shows how to implement Cross-Site Request Forgery (CSRF) protection using the `route_csrf` plugin. The `check_csrf!` method must be called within the route to enforce token validation. ```ruby route do |r| r.public r.assets check_csrf! # Must call this to check for valid CSRF tokens # ... end ``` -------------------------------- ### Setting Response Status Code Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Manually set the HTTP status code for a response. Defaults to 200 if not set and content is written, otherwise 404. ```ruby route do |r| r.get "hello" do response.status = 200 end end ``` -------------------------------- ### Optional Segment Matching with Array Matcher Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Use an array matcher where the last element is true to handle optional segments in routes. Note that this yields one argument if the segment is not provided. ```ruby r.is "items", Integer, [String, true] do |item_id, optional_data| end ``` -------------------------------- ### XSS Prevention with Render Plugin Escaping Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Illustrates how to prevent Cross-Site Scripting (XSS) by enabling output escaping in ERB templates via the `render` plugin's `:escape` option. Escaped output is rendered as HTML entities. ```ruby <%= '<>' %> # outputs <> <%== '<>' %> # outputs <> ``` -------------------------------- ### Custom Redirect Status Code Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Set a custom status code for redirects. Defaults to 302. ```ruby route do |r| r.get "hello" do r.redirect "/other", 301 # use 301 Moved Permanently end end ``` -------------------------------- ### Convert Parameter to Integer Source: https://github.com/jeremyevans/roda/blob/master/README.rdoc Explicitly convert a request parameter to an integer to prevent potential errors and security vulnerabilities when the parameter might be submitted as a hash or array. ```ruby # Convert foo_id parameter to an integer request.params['foo_id'].to_i ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.