Class: Grape::Middleware::Versioner::Path
- Defined in:
- lib/grape/middleware/versioner/path.rb
Overview
This middleware sets various version related rack environment variables based on the uri path and removes the version substring from the uri path. If the version substring does not match any potential initialized versions, a 404 error is thrown.
Example: For a uri path /v1/resource
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
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/grape/middleware/versioner/path.rb', line 26 def before path = env['PATH_INFO'].dup if prefix && path.index(prefix) == 0 path.sub!(prefix, '') path = Rack::Mount::Utils.normalize_path(path) end pieces = path.split('/') potential_version = pieces[1] if potential_version =~ [:pattern] if [:versions] && ![:versions].find { |v| v.to_s == potential_version } throw :error, status: 404, message: "404 API Version Not Found" end env['api.version'] = potential_version end end |
#default_options ⇒ Object
20 21 22 23 24 |
# File 'lib/grape/middleware/versioner/path.rb', line 20 def { pattern: /.*/i } end |