Module: Grape::Middleware::VersionerHelpers

Constant Summary collapse

DEFAULT_PATTERN =
/.*/i.freeze
DEFAULT_PARAMETER =
'apiver'

Instance Method Summary collapse

Instance Method Details

#cascade?Boolean

By default those errors contain an ‘X-Cascade` header set to `pass`, which allows nesting and stacking of routes (see Grape::Router) for more information). To prevent this behavior, and not add the `X-Cascade` header, one can set the `:cascade` option to `false`.

Returns:

  • (Boolean)


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

def cascade?
  version_options[:cascade]
end

#default_optionsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/grape/middleware/versioner_helpers.rb', line 9

def default_options
  {
    versions: nil,
    prefix: nil,
    mount_path: nil,
    pattern: DEFAULT_PATTERN,
    version_options: {
      strict: false,
      cascade: true,
      parameter: DEFAULT_PARAMETER
    }
  }
end

#error_headersObject



62
63
64
# File 'lib/grape/middleware/versioner_helpers.rb', line 62

def error_headers
  cascade? ? { Grape::Http::Headers::X_CASCADE => 'pass' } : {}
end

#mount_pathObject



31
32
33
# File 'lib/grape/middleware/versioner_helpers.rb', line 31

def mount_path
  options[:mount_path]
end

#parameter_keyObject



54
55
56
# File 'lib/grape/middleware/versioner_helpers.rb', line 54

def parameter_key
  version_options[:parameter]
end

#patternObject



35
36
37
# File 'lib/grape/middleware/versioner_helpers.rb', line 35

def pattern
  options[:pattern]
end

#potential_version_match?(potential_version) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/grape/middleware/versioner_helpers.rb', line 66

def potential_version_match?(potential_version)
  versions.blank? || versions.any? { |v| v.to_s == potential_version }
end

#prefixObject



27
28
29
# File 'lib/grape/middleware/versioner_helpers.rb', line 27

def prefix
  options[:prefix]
end

#strict?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/grape/middleware/versioner_helpers.rb', line 43

def strict?
  version_options[:strict]
end

#vendorObject



58
59
60
# File 'lib/grape/middleware/versioner_helpers.rb', line 58

def vendor
  version_options[:vendor]
end

#version_not_found!Object



70
71
72
# File 'lib/grape/middleware/versioner_helpers.rb', line 70

def version_not_found!
  throw :error, status: 404, message: '404 API Version Not Found', headers: { Grape::Http::Headers::X_CASCADE => 'pass' }
end

#version_optionsObject



39
40
41
# File 'lib/grape/middleware/versioner_helpers.rb', line 39

def version_options
  options[:version_options]
end

#versionsObject



23
24
25
# File 'lib/grape/middleware/versioner_helpers.rb', line 23

def versions
  options[:versions]
end