Class: Signpost::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/signpost/builder.rb,
lib/signpost/builder/nested.rb,
lib/signpost/builder/namespace.rb

Direct Known Subclasses

Nested

Defined Under Namespace

Classes: Namespace, Nested

Constant Summary collapse

DEFAULT_OPTIONS =
{
  params_key: 'router.params',
  default_redirect_status: 303,
  default_redirect_additional_values: :ignore,
  style: :sinatra,
  middlewares: [],
  rack_params: false
}.freeze
SUBPATH_REG =
/^\//.freeze

Instance Method Summary collapse

Instance Method Details

#buildObject

Build router

Returns: Router



393
394
395
# File 'lib/signpost/builder.rb', line 393

def build
  Router.new(@builders, @options, true)
end

#delete(path, &block) ⇒ Object

Define route which accepts PATCH request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
delete('/users/:id').to(controller: 'Users', action: 'destroy')
delete('/users/:id').to(controller: UsersController, action: 'destroy')
delete('/users/:id').to('users#destroy')

# Single action controller (responds to .call)
delete('/users/:id').to(UsersDestroyController)

# Sinatra-style block action
delete('/echo').to do |env|
  [201, {}, [env['SERVER_NAME']]]
end
delete('/echo') do |env|
  [201, {}, [env['SERVER_NAME']]]
end

# Params constraints
delete('/users/:id').to('users#destroy').capture(:digit)
delete('/users/:id.:ext').to('pages#destroy').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
delete('/pages/*slug/destroy').to('pages#destroy').except('/pages/system/*/destroy')

# Default params
delete('/:controller/:action/:id').params(id: 1)


287
288
289
290
291
# File 'lib/signpost/builder.rb', line 287

def delete(path, &block)
  builder = Sign::Flat::Path::DELETE.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#get(path, &block) ⇒ Object

Define route which accepts GET request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
get('/users').to(controller: 'Users', action: 'index')
get('/users').to(controller: UsersController, action: 'index')
get('/users').to('users#index')

# Single action controller (responds to .call)
get('/').to(HomeController)

# Sinatra-style block action
get('/echo').to do |env|
  [200, {}, [env['SERVER_NAME']]]
end
get('/echo') do |env|
  [200, {}, [env['SERVER_NAME']]]
end

# Path params
get('/users/:id').to('users#show')

# Params constraints
get('/users/:id').to('users#show').capture(:digit)
get('/users/:id.:ext').to('pages#show').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
get('/pages/*slug/edit').to('pages#edit').except('/pages/system/*/edit')

# Default params
get('/:controller/:action/:id').params(id: 1)


68
69
70
71
72
# File 'lib/signpost/builder.rb', line 68

def get(path, &block)
  builder = Sign::Flat::Path::GET.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#match(path, &block) ⇒ Object

Define route which accepts any type (or specified list) of request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
match('/users').to(controller: 'Users', action: 'index')
match('/users').to(controller: UsersController, action: 'index')
match('/users').to('users#index')

# Specify types of requests
match('/users').to('users#create').via(:post, :put)

# Single action controller (responds to .call)
match('/').to(HomeController)

# Sinatra-style block action
match('/echo').to do |env|
  [200, {}, [env['SERVER_NAME']]]
end
match('/echo') do |env|
  [200, {}, [env['SERVER_NAME']]]
end

# Path params
match('/users/:id').to('users#show')

# Params constraints
match('/users/:id').to('users#show').capture(:digit)
match('/users/:id.:ext').to('pages#show').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
match('/pages/*slug/edit').to('pages#edit').except('/pages/system/*/edit')

# Default params
match('/:controller/:action/:id').params(id: 1)


335
336
337
338
339
# File 'lib/signpost/builder.rb', line 335

def match(path, &block)
  builder = Sign::Flat::Path::Any.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#namespace(name, &block) ⇒ Object



377
378
379
380
# File 'lib/signpost/builder.rb', line 377

def namespace(name, &block)
  options = @options.merge(namespace: [@options[:namespace], name].compact)
  @builders << Sign::Namespace.new(absolute(name.to_s), options, &block)
end

#options(path, &block) ⇒ Object

Define route which accepts OPTIONS request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
options('/users').to(controller: 'Users', action: 'usage')
options('/users').to(controller: UsersController, action: 'usage')
options('/users').to('users#usage')

# Single action controller (responds to .call)
options('/').to(HomeController)

# Sinatra-style block action
options('/echo').to do |env|
  [200, {}, [env['SERVER_NAME']]]
end
options('/echo') do |env|
  [200, {}, [env['SERVER_NAME']]]
end

# Path params
options('/users/:id').to('users#show')

# Params constraints
options('/users/:id').to('users#usage').capture(:digit)
options('/users/:id.:ext').to('pages#usage').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
options('/pages/*slug/usage').to('pages#usage').except('/pages/private/*/usage')

# Default params
options('/:controller/:action/:id').params(id: 1)


245
246
247
248
249
# File 'lib/signpost/builder.rb', line 245

def options(path, &block)
  builder = Sign::Flat::Path::OPTIONS.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#patch(path, &block) ⇒ Object

Define route which accepts PATCH request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
patch('/users/:id').to(controller: 'Users', action: 'update')
patch('/users/:id').to(controller: UsersController, action: 'update')
patch('/users/:id').to('users#update')

# Single action controller (responds to .call)
patch('/users/:id').to(UsersUpdateController)

# Sinatra-style block action
patch('/echo').to do |env|
  [201, {}, [env['SERVER_NAME']]]
end
patch('/echo') do |env|
  [201, {}, [env['SERVER_NAME']]]
end

# Params constraints
patch('/users/:id').to('users#update').capture(:digit)
patch('/users/:id.:ext').to('pages#show').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
patch('/pages/*slug/update').to('pages#update').except('/pages/system/*/update')

# Default params
patch('/:controller/:action/:id').params(id: 1)


200
201
202
203
204
# File 'lib/signpost/builder.rb', line 200

def patch(path, &block)
  builder = Sign::Flat::Path::PATCH.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#post(path, &block) ⇒ Object

Define route which accepts POST request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
post('/users').to(controller: 'Users', action: 'create')
post('/users').to(controller: UsersController, action: 'create')
post('/users').to('users#create')

# Single action controller (responds to .call)
post('/search').to(SearchController)

# Sinatra-style block action
post('/echo').to do |env|
  [201, {}, [env['SERVER_NAME']]]
end
post('/echo') do |env|
  [201, {}, [env['SERVER_NAME']]]
end

# Path params
post('/users/:id').to('users#update')

# Params constraints
post('/users/:id').to('users#update').capture(:digit)
post('/users/:id.:ext').to('pages#show').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
post('/pages/*slug/update').to('pages#update').except('/pages/system/*/update')

# Default params
post('/:controller/:action/:id').params(id: 1)


113
114
115
116
117
# File 'lib/signpost/builder.rb', line 113

def post(path, &block)
  builder = Sign::Flat::Path::POST.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#put(path, &block) ⇒ Object

Define route which accepts PUT request for the given pattern

Params:

  • path String|RegExp pattern of the request path which should be matched

Yields: the Rack compatible block

Example:

# Most usual case: with controller and action. Will resolve Controller#action or Controller::Action
put('/users').to(controller: 'Users', action: 'create')
put('/users').to(controller: UsersController, action: 'create')
put('/users').to('users#create')

# Single action controller (responds to .call)
put('/search').to(SearchController)

# Sinatra-style block action
put('/echo').to do |env|
  [201, {}, [env['SERVER_NAME']]]
end
put('/echo') do |env|
  [201, {}, [env['SERVER_NAME']]]
end

# Path params
put('/users/:id').to('users#update')

# Params constraints
put('/users/:id').to('users#update').capture(:digit)
put('/users/:id.:ext').to('pages#show').capture({ id: /\d+/, ext: ['json', 'html'] })

# Exclude pattern
put('/pages/*slug/update').to('pages#update').except('/pages/system/*/update')

# Default params
put('/:controller/:action/:id').params(id: 1)


158
159
160
161
162
# File 'lib/signpost/builder.rb', line 158

def put(path, &block)
  builder = Sign::Flat::Path::PUT.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#redirect(path, &block) ⇒ Object



382
383
384
385
386
# File 'lib/signpost/builder.rb', line 382

def redirect(path, &block)
  builder = Sign::Flat::Redirect.new(absolute(path), @options, &block)
  @builders << builder
  builder
end

#root(&block) ⇒ Object

Define root route

Root route will always be in top of routes Also this is named route with name ‘root`

Example:

root.to(HomeController)


23
24
25
26
27
# File 'lib/signpost/builder.rb', line 23

def root(&block)
  builder = Sign::Flat::Path::GET.new(absolute('/'), @options, &block)
  @builders.unshift(builder)
  builder.as(root_name)
end

#within(path, &block) ⇒ Object

Define nested routes

Params:

  • path String subpath

Yields: routes dsl

Example:

within('/admin') do
  get('/pages').to('Admin::Pages#index')     # /admin/pages
  put('/users/:id').to('admin/users#create') # /admin/users/2
end

# Nested routes can has their own routes
within('/admin') do
  get('/').to('admin#index')

  within('/pages') do
    get('/').to('admin/pages#index')     # /admin/pages
    put('/:id').to('admin/pages#create') # /admin/pages/2
  end
end

# You can also build a middleware stack for subroutes
within('/admin') do
  use AuthMiddleware

  get('/').to('admin#index')
end


373
374
375
# File 'lib/signpost/builder.rb', line 373

def within(path, &block)
  @builders << Sign::Nested.new(absolute(path), @options, &block)
end