Class: Grape::Middleware::Versioner::Path
- Includes:
- Grape::Middleware::VersionerHelpers
- 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'
Constant Summary
Constants included from Grape::Middleware::VersionerHelpers
Grape::Middleware::VersionerHelpers::DEFAULT_PARAMETER, Grape::Middleware::VersionerHelpers::DEFAULT_PATTERN
Constants inherited from Base
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods included from Grape::Middleware::VersionerHelpers
#cascade?, #default_options, #error_headers, #mount_path, #parameter_key, #pattern, #potential_version_match?, #prefix, #strict?, #vendor, #version_not_found!, #version_options, #versions
Methods inherited from Base
#after, #call, #call!, #content_type, #content_type_for, #content_types, #default_options, #initialize, #mime_types, #response
Methods included from DSL::Headers
Methods included from Helpers
Constructor Details
This class inherits a constructor from Grape::Middleware::Base
Instance Method Details
#before ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/grape/middleware/versioner/path.rb', line 22 def before path_info = Grape::Router.normalize_path(env[Rack::PATH_INFO]) return if path_info == '/' [mount_path, Grape::Router.normalize_path(prefix)].each do |path| path_info.delete_prefix!(path) if path.present? && path != '/' && path_info.start_with?(path) end slash_position = path_info.index('/', 1) # omit the first one return unless slash_position potential_version = path_info[1..slash_position - 1] return unless potential_version.match?(pattern) version_not_found! unless potential_version_match?(potential_version) env[Grape::Env::API_VERSION] = potential_version end |