Method: Grape::DSL::Routing::ClassMethods#version

Defined in:
lib/grape/dsl/routing.rb

#version(*args, &block) ⇒ Object

Specify an API version.

Examples:

API with legacy support.

class MyAPI < Grape::API
  version 'v2'

  get '/main' do
    {some: 'data'}
  end

  version 'v1' do
    get '/main' do
      {legacy: 'data'}
    end
  end
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/grape/dsl/routing.rb', line 31

def version(*args, &block)
  if args.any?
    options = args.extract_options!
    options = options.reverse_merge(using: :path)
    requested_versions = args.flatten

    raise Grape::Exceptions::MissingVendorOption.new if options[:using] == :header && !options.key?(:vendor)

    @versions = versions | requested_versions

    if block_given?
      within_namespace do
        namespace_inheritable(:version, requested_versions)
        namespace_inheritable(:version_options, options)

        instance_eval(&block)
      end
    else
      namespace_inheritable(:version, requested_versions)
      namespace_inheritable(:version_options, options)
    end
  end

  @versions.last if instance_variable_defined?(:@versions) && @versions
end