Methods

Class/Module Index [+]

Quicksearch

Grape::Middleware::Versioner::AcceptVersionHeader

This middleware sets various version related rack environment variables based on the HTTP Accept-Version header

Example: For request header

Accept-Version: v1

The following rack env variables are set:

env['api.version']  => 'v1'

If version does not match this route, then a 406 is raised with X-Cascade header to alert Rack::Mount to attempt the next matched route.

Public Instance Methods

before() click to toggle source
# File lib/grape/middleware/versioner/accept_version_header.rb, line 20
def before
  potential_version = (env['HTTP_ACCEPT_VERSION'] || '').strip

  if strict?
    # If no Accept-Version header:
    if potential_version.empty?
      throw :error, status: 406, headers: error_headers, message: 'Accept-Version header must be set.'
    end
  end

  unless potential_version.empty?
    # If the requested version is not supported:
    unless versions.any? { |v| v.to_s == potential_version }
      throw :error, status: 406, headers: error_headers, message: 'The requested version is not supported.'
    end

    env['api.version'] = potential_version
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.