Class: Grape::Middleware::Versioner::Param

Inherits:
Base
  • Object
show all
Includes:
Grape::Middleware::VersionerHelpers
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'

Constant Summary

Constants included from Grape::Middleware::VersionerHelpers

Grape::Middleware::VersionerHelpers::DEFAULT_PARAMETER, Grape::Middleware::VersionerHelpers::DEFAULT_PATTERN

Constants inherited from Base

Base::TEXT_HTML

Instance Attribute Summary

Attributes inherited from Base

#app, #env, #options

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

#header

Methods included from Helpers

#context

Constructor Details

This class inherits a constructor from Grape::Middleware::Base

Instance Method Details

#beforeObject



24
25
26
27
28
29
30
31
# File 'lib/grape/middleware/versioner/param.rb', line 24

def before
  potential_version = Rack::Utils.parse_nested_query(env[Rack::QUERY_STRING])[parameter_key]
  return if potential_version.blank?

  version_not_found! unless potential_version_match?(potential_version)
  env[Grape::Env::API_VERSION] = potential_version
  env[Rack::RACK_REQUEST_QUERY_HASH].delete(parameter_key) if env.key? Rack::RACK_REQUEST_QUERY_HASH
end