Class: Grape::Middleware::Versioner::Base

Inherits:
Base
  • Object
show all
Extended by:
Forwardable
Includes:
PrecomputedContentTypes
Defined in:
lib/grape/middleware/versioner/base.rb

Direct Known Subclasses

AcceptVersionHeader, Header, Param, Path

Defined Under Namespace

Classes: Options

Constant Summary collapse

DEFAULT_OPTIONS =
Deprecated.

Kept as a frozen Hash representation of the Options defaults for back-compat. Will be removed in a future release.

Options.new.to_h.freeze
CASCADE_PASS_HEADER =
{ 'X-Cascade' => 'pass' }.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#app, #config, #env, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PrecomputedContentTypes

#content_type, #content_type_for, #content_types, #mime_types

Methods inherited from Base

#after, #before, #call, #call!, #context, #query_params, #rack_request, #response

Methods included from DSL::Headers

#header

Constructor Details

#initialize(app, **options) ⇒ Base

Returns a new instance of Base.



39
40
41
42
43
44
# File 'lib/grape/middleware/versioner/base.rb', line 39

def initialize(app, **options)
  super
  @versions = config.versions&.map(&:to_s) # making sure versions are strings to ease potential match
  @error_headers = cascade ? CASCADE_PASS_HEADER : {}
  @available_media_types = build_available_media_types
end

Instance Attribute Details

#available_media_typesObject (readonly)

Returns the value of attribute available_media_types.



34
35
36
# File 'lib/grape/middleware/versioner/base.rb', line 34

def available_media_types
  @available_media_types
end

#error_headersObject (readonly)

Returns the value of attribute error_headers.



34
35
36
# File 'lib/grape/middleware/versioner/base.rb', line 34

def error_headers
  @error_headers
end

#versionsObject (readonly)

Returns the value of attribute versions.



34
35
36
# File 'lib/grape/middleware/versioner/base.rb', line 34

def versions
  @versions
end

Class Method Details

.inherited(klass) ⇒ Object



29
30
31
32
# File 'lib/grape/middleware/versioner/base.rb', line 29

def self.inherited(klass)
  super
  Versioner.register(klass)
end

Instance Method Details

#potential_version_match?(potential_version) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/grape/middleware/versioner/base.rb', line 46

def potential_version_match?(potential_version)
  versions.blank? || versions.include?(potential_version)
end

#version_not_found!Object



50
51
52
# File 'lib/grape/middleware/versioner/base.rb', line 50

def version_not_found!
  throw :error, Grape::Exceptions::ErrorResponse.new(status: 404, message: '404 API Version Not Found', headers: CASCADE_PASS_HEADER)
end