class Grape::Router::Pattern

Constants

DEFAULT_CAPTURES

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 16
def initialize(pattern, **options)
  @origin = pattern
  @path = build_path(pattern, anchor: options[:anchor], suffix: options[:suffix])
  @pattern = build_pattern(@path, options)
  @to_regexp = @pattern.to_regexp
end

Public Instance Methods

captures_default() click to toggle source
# File lib/grape/router/pattern.rb, line 23
def captures_default
  to_regexp.names
           .delete_if { |n| DEFAULT_CAPTURES.include?(n) }
           .to_h { |k| [k, ''] }
end

Private Instance Methods

build_path(pattern, anchor: false, suffix: nil) click to toggle source
# File lib/grape/router/pattern.rb, line 40
def build_path(pattern, anchor: false, suffix: nil)
  PatternCache[[build_path_from_pattern(pattern, anchor: anchor), suffix]]
end
build_path_from_pattern(pattern, anchor: false) click to toggle source
# File lib/grape/router/pattern.rb, line 54
def build_path_from_pattern(pattern, anchor: false)
  if pattern.end_with?('*path')
    pattern.dup.insert(pattern.rindex('/') + 1, '?')
  elsif anchor
    pattern
  elsif pattern.end_with?('/')
    "#{pattern}?*path"
  else
    "#{pattern}/?*path"
  end
end
build_pattern(path, options) click to toggle source
# File lib/grape/router/pattern.rb, line 31
def build_pattern(path, options)
  Mustermann::Grape.new(
    path,
    uri_decode: true,
    params: options[:params],
    capture: extract_capture(**options)
  )
end
extract_capture(**options) click to toggle source
# File lib/grape/router/pattern.rb, line 44
def extract_capture(**options)
  sliced_options = options
                   .slice(:format, :version)
                   .delete_if { |_k, v| v.blank? }
                   .transform_values { |v| Array.wrap(v).map(&:to_s) }
  return sliced_options if options[:requirements].blank?

  options[:requirements].merge(sliced_options)
end