Class: Grape::Middleware::Versioner::Header
- Defined in:
- lib/grape/middleware/versioner/header.rb
Overview
This middleware sets various version related rack environment variables based on the HTTP Accept header with the pattern: application/vnd.:vendor-:version+:format
Example: For request header
Accept: application/vnd.mycompany.a-cool-resource-v1+json
The following rack env variables are set:
env['api.type'] => 'application'
env['api.subtype'] => 'vnd.mycompany.a-cool-resource-v1+json'
env['api.vendor] => 'mycompany.a-cool-resource'
env['api.version] => 'v1'
env['api.format] => 'json'
If version does not match this route, then a 406 is raised with X-Cascade header to alert Grape::Router to attempt the next matched route.
Constant Summary
Constants inherited from Base
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, #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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/grape/middleware/versioner/header.rb', line 25 def before handler = Grape::Util::AcceptHeaderHandler.new( accept_header: env[Grape::Http::Headers::HTTP_ACCEPT], versions: [:versions], **.fetch(:version_options) { {} } ) handler.match_best_quality_media_type!( content_types: content_types, allowed_methods: env[Grape::Env::GRAPE_ALLOWED_METHODS] ) do |media_type| env.update( Grape::Env::API_TYPE => media_type.type, Grape::Env::API_SUBTYPE => media_type.subtype, Grape::Env::API_VENDOR => media_type.vendor, Grape::Env::API_VERSION => media_type.version, Grape::Env::API_FORMAT => media_type.format ) end end |