Sha256: f7550a36f02c6fa14aa264e69e87efbc8b72b996e645865e5b598424f5657261

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

# frozen_string_literal: true

require 'forwardable'
require 'mustermann/grape'

module Grape
  class Router
    class Pattern
      DEFAULT_PATTERN_OPTIONS   = { uri_decode: true, type: :grape }.freeze
      DEFAULT_SUPPORTED_CAPTURE = %i[format version].freeze

      attr_reader :origin, :path, :capture, :pattern

      extend Forwardable
      def_delegators :pattern, :named_captures, :params
      def_delegators :@regexp, :===
      alias match? ===

      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

      def to_regexp
        @to_regexp ||= @pattern.to_regexp
      end

      private

      def pattern_options
        options = DEFAULT_PATTERN_OPTIONS.dup
        options[:capture] = capture if capture.present?
        options
      end

      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')

        "#{pattern}#{suffix}"
      end

      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

      def supported_capture
        DEFAULT_SUPPORTED_CAPTURE
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
grape-1.3.0 lib/grape/router/pattern.rb