Sha256: 6ac68d72ba44a1aa3121d40f9907361b30570f787754efbd3e1f920da76a95c6

Contents?: true

Size: 1.23 KB

Versions: 10

Compression:

Stored size: 1.23 KB

Contents

require 'grape/middleware/base'

module Grape
  module Middleware
    module Versioner
      # 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'
      # 
      class Path < Base
        def default_options
          {
            :pattern => /.*/i
          }
        end

        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
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 2 rubygems

Version Path
grape-0.2.1.1 lib/grape/middleware/versioner/path.rb
grape-0.2.3 lib/grape/middleware/versioner/path.rb
fragrant-0.0.5 vendor/bundle/ruby/1.9.1/gems/grape-0.2.2/lib/grape/middleware/versioner/path.rb
fragrant-0.0.4 vendor/grape/lib/grape/middleware/versioner/path.rb
grape-0.2.2 lib/grape/middleware/versioner/path.rb
fragrant-0.0.3 vendor/grape/lib/grape/middleware/versioner/path.rb
fragrant-0.0.2 vendor/grape/lib/grape/middleware/versioner/path.rb
fragrant-0.0.1 vendor/grape/lib/grape/middleware/versioner/path.rb
grape-0.2.1 lib/grape/middleware/versioner/path.rb
grape-0.2.0 lib/grape/middleware/versioner/path.rb