Class: Grape::Middleware::Versioner::Header

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



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/grape/middleware/versioner/header.rb', line 27

def before
  match_best_quality_media_type! 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