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

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.



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

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.



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

def available_media_types
  @available_media_types
end

#error_headersObject (readonly)

Returns the value of attribute error_headers.



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

def error_headers
  @error_headers
end

#versionsObject (readonly)

Returns the value of attribute versions.



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

def versions
  @versions
end

Class Method Details

.inherited(klass) ⇒ Object



23
24
25
26
# File 'lib/grape/middleware/versioner/base.rb', line 23

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

Instance Method Details

#potential_version_match?(potential_version) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#version_not_found!Object



44
45
46
# File 'lib/grape/middleware/versioner/base.rb', line 44

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