class Grape::Router::Pattern

Constants

DEFAULT_PATTERN_OPTIONS
DEFAULT_SUPPORTED_CAPTURE

Attributes

origin[R]
path[R]
pattern[R]
to_regexp[R]

Public Class Methods

new(pattern, **options) click to toggle source
# File lib/grape/router/pattern.rb, line 20
def initialize(pattern, **options)
  @origin  = pattern
  @path    = build_path(pattern, **options)
  @pattern = Mustermann::Grape.new(@path, **pattern_options(options))
  @to_regexp = @pattern.to_regexp
end

Private Instance Methods

build_path(pattern, anchor: false, suffix: nil, **_options) click to toggle source
# File lib/grape/router/pattern.rb, line 36
def build_path(pattern, anchor: false, suffix: nil, **_options)
  unless anchor || pattern.end_with?('*path')
    pattern = +pattern
    pattern << '/' unless pattern.end_with?('/')
    pattern << '*path'
  end

  pattern = -pattern.split('/').tap do |parts|
    parts[parts.length - 1] = "?#{parts.last}"
  end.join('/') if pattern.end_with?('*path')

  PatternCache[[pattern, suffix]]
end
extract_capture(requirements: {}, **options) click to toggle source
# File lib/grape/router/pattern.rb, line 50
def extract_capture(requirements: {}, **options)
  requirements = {}.merge(requirements)
  DEFAULT_SUPPORTED_CAPTURE.each_with_object(requirements) do |field, capture|
    option = Array(options[field])
    capture[field] = option.map(&:to_s) if option.present?
  end
end
pattern_options(options) click to toggle source
# File lib/grape/router/pattern.rb, line 29
def pattern_options(options)
  capture = extract_capture(**options)
  options = DEFAULT_PATTERN_OPTIONS.dup
  options[:capture] = capture if capture.present?
  options
end