class Grape::Middleware::Versioner::Path

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'

Public Instance Methods

before() click to toggle source
# File lib/grape/middleware/versioner/path.rb, line 26
def before
  path = env[Grape::Http::Headers::PATH_INFO].dup

  if prefix && path.index(prefix) == 0
    path.sub!(prefix, '')
    path = Rack::Mount::Utils.normalize_path(path)
  end

  pieces = path.split('/')
  potential_version = pieces[1]
  return unless potential_version =~ options[:pattern]
  throw :error, status: 404, message: '404 API Version Not Found' if options[:versions] && !options[:versions].find { |v| v.to_s == potential_version }
  env[Grape::Env::API_VERSION] = potential_version
end
default_options() click to toggle source
# File lib/grape/middleware/versioner/path.rb, line 20
def default_options
  {
    pattern: /.*/i
  }
end

Private Instance Methods

prefix() click to toggle source
# File lib/grape/middleware/versioner/path.rb, line 43
def prefix
  Rack::Mount::Utils.normalize_path(options[:prefix].to_s) if options[:prefix]
end