class Grape::Router::Pattern

Constants

DEFAULT_PATTERN_OPTIONS
DEFAULT_SUPPORTED_CAPTURE

Attributes

capture[R]
origin[R]
path[R]
pattern[R]

Public Class Methods

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

Public Instance Methods

to_regexp() click to toggle source
# File lib/grape/router/pattern.rb, line 25
def to_regexp
  @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 37
def build_path(pattern, anchor: false, suffix: nil, **_options)
  unless anchor || pattern.end_with?('*path')
    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')

  pattern + suffix.to_s
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)
  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() click to toggle source
# File lib/grape/router/pattern.rb, line 31
def pattern_options
  options = DEFAULT_PATTERN_OPTIONS.dup
  options[:capture] = capture if capture.present?
  options
end
supported_capture() click to toggle source
# File lib/grape/router/pattern.rb, line 58
def supported_capture
  DEFAULT_SUPPORTED_CAPTURE
end