Class: DescribedRoutes::Middleware::Rails
- Defined in:
- lib/described_routes/middleware/rails.rb
Overview
Rack middleware that integrates described_routes with Rails.
In your environment.rb, add
require "described_routes/middleware/rails"
and include
config.middleware.use DescribedRoutes::Middleware::Rails
inside your Rails::Initializer.run do...end block
Your Rails application will then serve ResourceTemplate data at the configured descrbed_routes path (/described_routes by default and adds link headers to regular requests whose routing matches a ResourceTemplate.
Constant Summary
Constants inherited from Base
Instance Method Summary collapse
-
#get_resource_routing(req) ⇒ Object
Returns a ResourceTemplate matching the request and its parameter hash, otherwise a pair of nils.
-
#get_resource_templates(root) ⇒ Object
Get a ResourceTemplates object from Rails.
Methods inherited from Base
#call, #call_with_link_header, #init_from_first_req, #initialize, #make_link_header, #serve_resource_template_data
Constructor Details
This class inherits a constructor from DescribedRoutes::Middleware::Base
Instance Method Details
#get_resource_routing(req) ⇒ Object
Returns a ResourceTemplate matching the request and its parameter hash, otherwise a pair of nils. The parameter hash is normalized by removing ‘controller’ and ‘action’ members and renaming ‘id’ (when present) to something resource-specific.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/described_routes/middleware/rails.rb', line 30 def get_resource_routing(req) path_parameters = req.env["action_controller.request.path_parameters"] query_parameters = req.env["action_controller.request.query_parameters"] # Guess the route from the controller and action controller_name, action_name = path_parameters["controller"], path_parameters["action"] resource_template, id_name = @resource_templates.routing[[controller_name, action_name]] if resource_template params = path_parameters.merge(query_parameters).except("action", "controller") if id_name && params[:id] params[id_name] = params.delete(:id) end [resource_template, params] else [nil, nil] end end |
#get_resource_templates(root) ⇒ Object
Get a ResourceTemplates object from Rails. Override to suppress sensitive routes.
22 23 24 |
# File 'lib/described_routes/middleware/rails.rb', line 22 def get_resource_templates(root) RailsRoutes.get_resource_templates(root) end |