Class: Grape::Middleware::Versioner

Inherits:
Base
  • Object
show all
Defined in:
lib/grape/middleware/versioner.rb

Instance Attribute Summary

Attributes inherited from Base

#app, #env, #options

Instance Method Summary collapse

Methods inherited from Base

#after, #call, #call!, #initialize, #request, #response

Constructor Details

This class inherits a constructor from Grape::Middleware::Base

Instance Method Details

#beforeObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/grape/middleware/versioner.rb', line 12

def before
  pieces = env['PATH_INFO'].split('/')
  potential_version = pieces[1]
  if potential_version =~ options[:pattern]
    if options[:versions] && !options[:versions].include?(potential_version)
      throw :error, :status => 404, :message => "404 API Version Not Found"
    end
    
    truncated_path = "/#{pieces[2..-1].join('/')}"
    env['api.version'] = potential_version
    env['PATH_INFO'] = truncated_path
  end
end

#default_optionsObject



6
7
8
9
10
# File 'lib/grape/middleware/versioner.rb', line 6

def default_options
  {
    :pattern => /.*/i
  }
end