Socket
Book a DemoInstallSign in
Socket

sinatra-named-routes

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sinatra-named-routes

0.1.3
bundlerRubygems
Version published
Maintainers
1
Created
Source

Sinatra Named Routes Build Status

This gem allows the use of named routes in Sinatra applications.

Usage

To use this gem you must register it in your Sinatra application.

require 'sinatra/base'
require 'sinatra/named_routes'

class MyApp < Sinatra::Base
  register Sinatra::NamedRoutes
end

The you use the map method to map a route to a name, and use that name when defining your routes.

require 'sinatra/base'
require 'sinatra/named_routes'

class MyApp < Sinatra::Base
  register Sinatra::NamedRoutes
  
  map :article, '/article/:id'
  
  get :article do
    # get article bla bla ...
  end
end

To generate urls in extends Sinatras built-in methods like url and to but it does not break them. They work like before except that now you can also pass the route name and paramters. The parameters have to be always passed as the last argument. Otherwise the url work the same.

# in your route or view you can write something like this
url :article, false, :id => 123 # /article/123

The map method supports the same routes as Sinatra does.

# named parameters
map :article, '/article/:id'

url :article, false, :id => 123 # /article/123
  
# splats
map :article, '/article/*.*'

url :article, false, [123, 'json'] # /article/123.json

# regular expressions
map :article, %r{/article/([\w]+).([\w]+)}

url :article, false, [123, 'json'] # /article/123.json
  
# named captures
map :article, %r{/article/(?<slug>[^/?#]+)}

url :article, false, :slug => 'hello_world' # /article/hello_world

# optional named captures
map(:articles, %r{/articles(?<format>.[^/?#]+)?})

url :articles, false, :format => '.html' # /articles.html

FAQs

Package last updated on 16 Oct 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.