Class: Grape::Middleware::Versioner::Param
- Defined in:
- lib/grape/middleware/versioner/param.rb
Overview
This middleware sets various version related rack environment variables based on the request parameters and removes that parameter from the request parameters for subsequent middleware and API. If the version substring does not match any potential initialized versions, a 404 error is thrown. If the version substring is not passed the version (highest mounted) version will be used.
Example: For a uri path /resource?apiver=v1
The following rack env variables are set and path is rewritten to '/resource':
env['api.version'] => 'v1'
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#after, #call, #call!, #content_type, #content_type_for, #content_types, #initialize, #mime_types, #response
Constructor Details
This class inherits a constructor from Grape::Middleware::Base
Instance Method Details
#before ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/grape/middleware/versioner/param.rb', line 28 def before paramkey = [:parameter] potential_version = Rack::Utils.parse_nested_query(env['QUERY_STRING'])[paramkey] unless potential_version.nil? if [:versions] && ![:versions].find { |v| v.to_s == potential_version } throw :error, status: 404, message: "404 API Version Not Found", headers: { 'X-Cascade' => 'pass' } end env['api.version'] = potential_version env['rack.request.query_hash'].delete(paramkey) if env.key? 'rack.request.query_hash' end end |
#default_options ⇒ Object
22 23 24 25 26 |
# File 'lib/grape/middleware/versioner/param.rb', line 22 def { parameter: "apiver" } end |