Sha256: a794f794a63eb7bc5cff699460e7c84f4c4fcfe5d5cb66927af48ee2eed502bd

Contents?: true

Size: 1.7 KB

Versions: 21

Compression:

Stored size: 1.7 KB

Contents

require 'grape/middleware/base'

module Grape
  module Middleware
    module Versioner
      # This middleware sets various version related rack environment variables
      # based on the request parameters and removes that parameter from the
      # request parameters for subsequent middleware and API.
      # If the version substring does not match any potential initialized
      # versions, a 404 error is thrown.
      # If the version substring is not passed the version (highest mounted)
      # version will be used.
      #
      # Example: For a uri path
      #   /resource?apiver=v1
      #
      # The following rack env variables are set and path is rewritten to
      # '/resource':
      #
      #   env['api.version'] => 'v1'
      class Param < Base
        def default_options
          {
            version_options: {
              parameter: 'apiver'.freeze
            }
          }
        end

        def before
          potential_version = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[paramkey]
          return if potential_version.nil?
          throw :error, status: 404, message: '404 API Version Not Found', headers: { Grape::Http::Headers::X_CASCADE => 'pass' } if options[:versions] && !options[:versions].find { |v| v.to_s == potential_version }
          env[Grape::Env::API_VERSION] = potential_version
          env[Grape::Env::RACK_REQUEST_QUERY_HASH].delete(paramkey) if env.key? Grape::Env::RACK_REQUEST_QUERY_HASH
        end

        private

        def paramkey
          version_options[:parameter] || default_options[:version_options][:parameter]
        end

        def version_options
          options[:version_options]
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 2 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/grape-1.2.5/lib/grape/middleware/versioner/param.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/grape-1.2.5/lib/grape/middleware/versioner/param.rb
grape-1.2.5 lib/grape/middleware/versioner/param.rb
grape-1.2.4 lib/grape/middleware/versioner/param.rb
grape-1.2.3 lib/grape/middleware/versioner/param.rb
grape-1.2.2 lib/grape/middleware/versioner/param.rb
grape-1.2.1 lib/grape/middleware/versioner/param.rb
grape-1.2.0 lib/grape/middleware/versioner/param.rb
grape-1.1.0 lib/grape/middleware/versioner/param.rb
grape-1.0.3 lib/grape/middleware/versioner/param.rb
grape-1.0.2 lib/grape/middleware/versioner/param.rb
grape-1.0.1 lib/grape/middleware/versioner/param.rb
grape-1.0.0 lib/grape/middleware/versioner/param.rb
grape-0.19.2 lib/grape/middleware/versioner/param.rb
grape-0.19.1 lib/grape/middleware/versioner/param.rb
grape-0.19.0 lib/grape/middleware/versioner/param.rb
grape-0.18.0 lib/grape/middleware/versioner/param.rb
grape-0.17.0 lib/grape/middleware/versioner/param.rb
grape-0.16.2 lib/grape/middleware/versioner/param.rb
grape-0.16.1 lib/grape/middleware/versioner/param.rb